Class: Papertrail::CliAddSystem

Inherits:
Object
  • Object
show all
Includes:
CliHelpers
Defined in:
lib/papertrail/cli_add_system.rb

Instance Attribute Summary collapse

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_nameObject (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(message, try_help = false)
  puts "#{program_name}: #{message}"
  if try_help
    puts "Try `#{program_name} --help' for more information."
  end
  exit(1)
end

#runObject



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
  options = {
    :configfile => nil,
    :token => ENV['PAPERTRAIL_API_TOKEN'],
  }

  if configfile = find_configfile
    configfile_options = load_configfile(configfile)
    options.merge!(configfile_options)
  end

  OptionParser.new do |opts|
    @program_name = opts.program_name

    opts.banner = "Usage: #{opts.program_name} [OPTION]..."

    opts.separator ''

    opts.separator "Options:"

    opts.on("-c", "--configfile PATH", "Path to config (~/.papertrail.yml)") do |v|
      options[:configfile] = File.expand_path(v)
    end
    opts.on("-s", "--system SYSTEM", "Name of system to add") do |v|
      options[: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|
      options[:hostname] = v
    end

    opts.separator ''
    opts.separator 'Host Settings:'

    opts.on("-i", "--ip-address IP_ADDRESS", "IP address of system") do |v|
      options[:ip_address] = v
    end

    opts.on("--destination-port PORT", "Destination port") do |v|
      options[: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 options[:configfile]
    configfile_options = load_configfile(options[:configfile])
    options.merge!(configfile_options)
  end

  unless options[:system]
    error "The --system argument must be specified"
  end

  unless options[:ip_address] || options[:destination_port]
    error 'Either --ip-address or --destination-port most be provided'
  end

  Papertrail::Connection.new(options).start do |connection|
    # Bail if system already exists
    existing = connection.show_source(options[:system])
    if existing && existing['name'].upcase == options[:system].upcase
      exit 0
    end

    if options[:destination_port] && !options[:hostname]
      options[:hostname] = options[:system]
    end

    if connection.register_source(options[:system], options)
      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