Class: Papertrail::CliAddSystem
- Inherits:
-
Object
- Object
- Papertrail::CliAddSystem
- Includes:
- CliHelpers
- Defined in:
- lib/papertrail/cli_add_system.rb
Instance Attribute Summary collapse
-
#program_name ⇒ Object
readonly
Returns the value of attribute program_name.
Instance Method Summary collapse
Methods included from CliHelpers
#find_configfile, #load_configfile, #output_http_error, #parse_time, #set_min_max_time!, #symbolize_keys
Instance Attribute Details
#program_name ⇒ Object (readonly)
Returns the value of attribute program_name.
11 12 13 |
# File 'lib/papertrail/cli_add_system.rb', line 11 def program_name @program_name end |
Instance Method Details
#error(message, try_help = false) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/papertrail/cli_add_system.rb', line 112 def error(, try_help = false) puts "#{program_name}: #{}" if try_help puts "Try `#{program_name} --help' for more information." end exit(1) end |
#run ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/papertrail/cli_add_system.rb', line 13 def run = { :configfile => nil, :token => ENV['PAPERTRAIL_API_TOKEN'], } if configfile = find_configfile = load_configfile(configfile) .merge!() end OptionParser.new do |opts| @program_name = opts.program_name opts. = "Usage: #{opts.program_name} [OPTION]..." opts.separator '' opts.separator "Options:" opts.on("-c", "--configfile PATH", "Path to config (~/.papertrail.yml)") do |v| [:configfile] = File.(v) end opts.on("-s", "--system SYSTEM", "Name of system to add") do |v| [:system] = v end opts.on("-n", "--hostname HOSTNAME", "Hostname which can be used to filter", "events from the same IP by syslog hostname") do |v| [:hostname] = v end opts.separator '' opts.separator 'Host Settings:' opts.on("-i", "--ip-address IP_ADDRESS", "IP address of system") do |v| [:ip_address] = v end opts.on("--destination-port PORT", "Destination port") do |v| [:destination_port] = v end opts.separator '' opts.separator " Note: only one of --ip-address or --destination-port must be specified" opts.separator '' opts.separator "Common options:" opts.on("-h", "--help", "Show usage") do |v| puts opts exit end opts.separator '' opts.separator 'Example:' opts.separator " $ #{opts.program_name} --system mysystemname --destination-port 39273" opts.separator " $ #{opts.program_name} --system mysystemname --ip-address 1.2.3.4" end.parse! if [:configfile] = load_configfile([:configfile]) .merge!() end unless [:system] error "The --system argument must be specified" end unless [:ip_address] || [:destination_port] error 'Either --ip-address or --destination-port most be provided' end Papertrail::Connection.new().start do |connection| # Bail if system already exists existing = connection.show_source([:system]) if existing && existing['name'].upcase == [:system].upcase exit 0 end if [:destination_port] && ![:hostname] [:hostname] = [:system] end if connection.register_source([:system], ) exit 0 end end exit 1 rescue OptionParser::ParseError => e error(e, true) exit 1 rescue Net::HTTPServerException => e output_http_error(e) exit 1 end |