papertrail 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/papertrail.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Papertrail
2
- VERSION = "0.9.5"
2
+ VERSION = "0.9.6"
3
3
  end
4
4
 
5
5
  require 'papertrail/search_client'
@@ -8,6 +8,8 @@ module Papertrail
8
8
  class CliAddSystem
9
9
  include Papertrail::CliHelpers
10
10
 
11
+ attr_reader :program_name
12
+
11
13
  def run
12
14
  # Let it slide if we have invalid JSON
13
15
  if JSON.respond_to?(:default_options)
@@ -24,26 +26,53 @@ module Papertrail
24
26
  end
25
27
 
26
28
  OptionParser.new do |opts|
27
- opts.banner = "papertrail-add-system"
29
+ @program_name = opts.program_name
30
+
31
+ opts.banner = "Usage: #{opts.program_name} [OPTION]..."
32
+
33
+ opts.separator ''
34
+
35
+ opts.separator "Options:"
28
36
 
29
- opts.on("-h", "--help", "Show usage") do |v|
30
- puts opts
31
- exit
32
- end
33
37
  opts.on("-c", "--configfile PATH", "Path to config (~/.papertrail.yml)") do |v|
34
38
  options[:configfile] = File.expand_path(v)
35
39
  end
36
40
  opts.on("-s", "--system SYSTEM", "Name of system to add") do |v|
37
41
  options[:system] = v
38
42
  end
39
- opts.on("-n", "--hostname HOSTNAME", "Optional hostname which can be used to filter events from the same IP by syslog hostname") do |v|
43
+ opts.on("-n", "--hostname HOSTNAME", "Hostname which can be used to filter",
44
+ "events from the same IP by syslog hostname") do |v|
40
45
  options[:hostname] = v
41
46
  end
47
+
48
+ opts.separator ''
49
+ opts.separator 'Host Settings:'
50
+
42
51
  opts.on("-i", "--ip-address IP_ADDRESS", "IP address of system") do |v|
43
- options[:ip] = v
52
+ options[:ip_address] = v
53
+ end
54
+
55
+ opts.on("--destination-port PORT", "Destination port") do |v|
56
+ options[:destination_port] = v
57
+ end
58
+
59
+ opts.separator ''
60
+ opts.separator " Note: only one of --ip-address or --destination-port must be specified"
61
+
62
+
63
+ opts.separator ''
64
+ opts.separator "Common options:"
65
+
66
+ opts.on("-h", "--help", "Show usage") do |v|
67
+ puts opts
68
+ exit
44
69
  end
45
70
 
46
- opts.separator usage
71
+ opts.separator ''
72
+ opts.separator 'Example:'
73
+ opts.separator " $ #{opts.program_name} --system mysystemname --destination-port 39273"
74
+ opts.separator " $ #{opts.program_name} --system mysystemname --ip-address 1.2.3.4"
75
+
47
76
  end.parse!
48
77
 
49
78
  if options[:configfile]
@@ -51,8 +80,13 @@ module Papertrail
51
80
  options.merge!(configfile_options)
52
81
  end
53
82
 
54
- raise OptionParser::MissingArgument, 'system' if options[:system].nil?
55
- raise OptionParser::MissingArgument, 'ip' if options[:ip].nil?
83
+ unless options[:system]
84
+ error "The --system argument must be specified"
85
+ end
86
+
87
+ unless options[:ip_address] || options[:destination_port]
88
+ error 'Either --ip-address or --destination-port most be provided'
89
+ end
56
90
 
57
91
  connection = Papertrail::Connection.new(options)
58
92
 
@@ -61,27 +95,26 @@ module Papertrail
61
95
  exit 0
62
96
  end
63
97
 
64
- if connection.register_source(options[:system], options[:ip], options[:hostname])
98
+ if options[:destination_port] && !options[:hostname]
99
+ options[:hostname] = options[:system]
100
+ end
101
+
102
+ if connection.register_source(options[:system], options)
65
103
  exit 0
66
104
  end
67
105
 
68
106
  exit 1
69
107
  rescue OptionParser::ParseError => e
70
- puts "Error: #{e}"
71
- puts usage
108
+ error(e, true)
72
109
  exit 1
73
110
  end
74
111
 
75
- def usage
76
- <<-EOF
77
-
78
- Usage:
79
- papertrail-add-system [-s system] [-i ip-address] [-n hostname] [-c papertrail.yml]
80
-
81
- Example:
82
- papertrail-add-system -s mysystemname -i 1.2.3.4
83
-
84
- EOF
112
+ def error(message, try_help = false)
113
+ puts "#{program_name}: #{message}"
114
+ if try_help
115
+ puts "Try `#{program_name} --help' for more information."
116
+ end
117
+ exit(1)
85
118
  end
86
119
  end
87
120
  end
@@ -96,10 +96,37 @@ module Papertrail
96
96
  end
97
97
  end
98
98
 
99
- def register_source(name, ip_address, hostname = nil)
100
- opts = { :name => name, :ip_address => ip_address }
101
- opts.merge! :hostname => hostname unless hostname.nil?
102
- @connection.post("systems.json", :system => opts)
99
+ def register_source(name, *args)
100
+ options = args.last.is_a?(Hash) ? args.pop.dup : {}
101
+
102
+ if ip_address = args.shift
103
+ options[:ip_address] = ip_address
104
+ end
105
+
106
+ if hostname = args.shift
107
+ options[:hostname] = hostname
108
+ end
109
+
110
+ request = {
111
+ :system => {
112
+ :name => name
113
+ }
114
+ }
115
+
116
+ if options[:ip_address]
117
+ request[:system][:ip_address] = options[:ip_address]
118
+ end
119
+
120
+ if options[:hostname]
121
+ request[:system][:hostname] = options[:hostname]
122
+ end
123
+
124
+ if options[:destination_port]
125
+ request[:destination_port] = options[:destination_port]
126
+ end
127
+
128
+ response = @connection.post("systems.json", request)
129
+ raise response.body.inspect unless response.success?
103
130
  end
104
131
 
105
132
  def unregister_source(name)
data/papertrail.gemspec CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'papertrail'
16
- s.version = '0.9.5'
17
- s.date = '2012-09-27'
16
+ s.version = '0.9.6'
17
+ s.date = '2012-10-05'
18
18
  s.rubyforge_project = 'papertrail'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 9
8
- - 5
9
- version: 0.9.5
8
+ - 6
9
+ version: 0.9.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Papertrail
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-09-27 00:00:00 -07:00
17
+ date: 2012-10-05 00:00:00 -07:00
18
18
  default_executable: papertrail
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency