sensu-cli 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZWUwZDFkN2IxNjBjNzJkMmRhMDM0NTYxMjcyMGExODg0M2U0Zjc3Mg==
4
+ OWQ3MTA4MWI1YTNjM2U3OWNlMmY0MGI5ZmQ4ZmMyYzZiMGE4ZDQ2NQ==
5
5
  data.tar.gz: !binary |-
6
- ZGMyNDBjMjkwYmJlYzVhZmE5OWM2NmRjN2JmMThmM2YxZTZkMWI1Nw==
6
+ YzA2OWJkYmUxYzUxNzBmY2MzNTA3Y2EzZDIzZjJmNWEwMmM4NzRlNg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YmE4OThmNTNlNTU4Yjk0ODRmMDQ0NWI2M2IxMTYxYzBlZDM5MjVhYWYyYjVi
10
- Y2ZlNTk5YmFjOTQxNTc4MWQzYzYwOWE0NjQ3YjM2Y2ZjZTI3YzUyNTIwZGJm
11
- NjQ0OGUzMjNmOWI1MjA3NWI2MzNlMmRkZWQ1MmM0ZTkzZGFkNzQ=
9
+ ZmNjODVjNGUzZmYwNDUwMDc2ZmY0ODA3NTAyMmZmMmNhMDljZjMxNmViZDRk
10
+ OTFlYjBjNDU5YmU5MzA0MmEyOTc1ZWVmYmRhNDVjZGE4MTVlNDg1NTU3NDY0
11
+ NTlhNzA2ODM2NzFiMmUyNzAxYTJiMjZhNzQwYjg5MGE5YTYxZjg=
12
12
  data.tar.gz: !binary |-
13
- MzAwZDI5NjFhNjczMDVhYzQyY2U5NDQ4ZWNjOTFkMjE4ZDNkOTBkZjk2ZGM4
14
- ZWFjNTlmZmY2Nzk5YmNhOGYyYzNlM2Y2YTA1ZmVlYzA1ZDQ0ZjNhMjU3ZmI5
15
- ZDZlZGJlM2RlMjRlZTcwZWQ4MjI5MWQ5ZTBlNWQ5MjlmNzg3NzY=
13
+ MDRlNmU5ODk0ZGIxY2Y4ZDdiZTExZGFjNmExMGY4MTg3NTU3NDU5YTM4N2Zk
14
+ YzNjMTIxNGVjMGYwNDljZDE2NjNkMzBkYzg4NmEyYmIwMmI0MGJhNmFkMDE5
15
+ MDE5ZTZkZGY1NmI0ODc5MDc4NTNlMzMwN2ZiODFlNDFhNjg0ODc=
data/bin/sensu CHANGED
@@ -3,6 +3,6 @@ unless $:.include?(File.dirname(__FILE__) + '/../lib/')
3
3
  $: << File.dirname(__FILE__) + '/../lib'
4
4
  end
5
5
 
6
- require 'sensu-cli/sensu'
7
- sensu = SensuCli::Core.new
6
+ require 'sensu-cli/base'
7
+ sensu = SensuCli::Base.new
8
8
  sensu.setup
data/bin/sensu-cli CHANGED
@@ -3,6 +3,6 @@ unless $:.include?(File.dirname(__FILE__) + '/../lib/')
3
3
  $: << File.dirname(__FILE__) + '/../lib'
4
4
  end
5
5
 
6
- require 'sensu-cli/sensu'
7
- sensu = SensuCli::Core.new
6
+ require 'sensu-cli/base'
7
+ sensu = SensuCli::Base.new
8
8
  sensu.setup
@@ -0,0 +1,102 @@
1
+ require 'rubygems' if RUBY_VERSION < '1.9.0'
2
+ require 'net/https'
3
+ require 'json'
4
+ require 'sensu-cli/settings'
5
+ require 'sensu-cli/cli'
6
+ require 'sensu-cli/editor'
7
+ require 'sensu-cli/pretty'
8
+ require 'sensu-cli/path.rb'
9
+ require 'rainbow'
10
+
11
+ module SensuCli
12
+ class Base
13
+
14
+ def setup
15
+ clis = Cli.new
16
+ cli = clis.global
17
+ settings
18
+ @command = cli[:command]
19
+ api_path(cli)
20
+ make_call
21
+ end
22
+
23
+ def settings
24
+ directory = "#{Dir.home}/.sensu"
25
+ file = "#{directory}/settings.rb"
26
+ alt = "/etc/sensu/sensu-cli/settings.rb"
27
+ settings = Settings.new
28
+ if settings.is_file?(file)
29
+ SensuCli::Config.from_file(file)
30
+ elsif settings.is_file?(alt)
31
+ SensuCli::Config.from_file(alt)
32
+ else
33
+ settings.create(directory,file)
34
+ end
35
+ end
36
+
37
+ def api_path(cli)
38
+ p = PathCreator.new
39
+ p.respond_to?(@command) ? path = p.send(@command, cli) : (puts "Something Bad Happened";exit)
40
+ @api = {:path => path[:path], :method => cli[:method], :command => cli[:command], :payload => (path[:payload] || false)}
41
+ end
42
+
43
+ def http_request
44
+ http = Net::HTTP.new(Config.host, Config.port)
45
+ http.read_timeout = 15
46
+ http.open_timeout = 5
47
+ if Config.ssl
48
+ http.use_ssl = true
49
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
50
+ end
51
+ case @api[:method]
52
+ when 'Get'
53
+ req = Net::HTTP::Get.new(@api[:path])
54
+ when 'Delete'
55
+ req = Net::HTTP::Delete.new(@api[:path])
56
+ when 'Post'
57
+ req = Net::HTTP::Post.new(@api[:path],initheader = {'Content-Type' => 'application/json'})
58
+ req.body = @api[:payload]
59
+ end
60
+ req.basic_auth(Config.user, Config.password) if Config.user && Config.password
61
+ begin
62
+ http.request(req)
63
+ rescue Timeout::Error
64
+ puts "HTTP request has timed out.".color(:red)
65
+ exit
66
+ rescue StandardError => e
67
+ puts "An HTTP error occurred. Check your settings. #{e}".color(:red)
68
+ exit
69
+ end
70
+ end
71
+
72
+ def make_call
73
+ res = http_request
74
+ msg = response_codes(res.code,res.body)
75
+ res.code != '200' ? exit : Pretty.print(msg)
76
+ Pretty.count(msg)
77
+ end
78
+
79
+ def response_codes(code,body)
80
+ case code
81
+ when '200'
82
+ JSON.parse(body)
83
+ when '201'
84
+ puts "The stash has been created." if @command == "stashes" || @command == "silence"
85
+ when '202'
86
+ puts "The item was submitted for processing."
87
+ when '204'
88
+ puts "Sensu is healthy" if @command == 'health'
89
+ puts "The item was successfully deleted." if @command == 'aggregates' || @command == 'stashes'
90
+ when '400'
91
+ puts "The payload is malformed.".color(:red)
92
+ when '401'
93
+ puts "The request requires user authentication.".color(:red)
94
+ when '404'
95
+ puts "The item did not exist.".color(:cyan)
96
+ else
97
+ (@command == 'health') ? (puts "Sensu is not healthy.".color(:red)) : (puts "There was an error while trying to complete your request. Response code: #{code}".color(:red))
98
+ end
99
+ end
100
+
101
+ end
102
+ end
@@ -0,0 +1,95 @@
1
+ module SensuCli
2
+ class PathCreator
3
+
4
+ def clients(cli)
5
+ path = "/clients"
6
+ path << "/#{cli[:fields][:name]}" if cli[:fields][:name]
7
+ path << "/history" if cli[:fields][:history]
8
+ path << pagination(cli)
9
+ respond(path)
10
+ end
11
+
12
+ def info(cli)
13
+ path = "/info"
14
+ respond(path)
15
+ end
16
+
17
+ def health(cli)
18
+ path = "/health?consumers=#{cli[:fields][:consumers]}&messages=#{cli[:fields][:messages]}"
19
+ respond(path)
20
+ end
21
+
22
+ def stashes(cli)
23
+ if cli[:fields][:create]
24
+ e = Editor.new
25
+ payload = e.create_stash(cli[:fields][:create_path]).to_json
26
+ end
27
+ path = "/stashes"
28
+ path << "/#{cli[:fields][:path]}" if cli[:fields][:path]
29
+ path << pagination(cli)
30
+ respond(path,payload)
31
+ end
32
+
33
+ def checks(cli)
34
+ if cli[:fields][:name]
35
+ path = "/check/#{cli[:fields][:name]}"
36
+ elsif cli[:fields][:subscribers]
37
+ payload = {:check => cli[:fields][:check],:subscribers => cli[:fields][:subscribers]}.to_json
38
+ path = "/check/request"
39
+ else
40
+ path = "/checks"
41
+ end
42
+ respond(path,payload)
43
+ end
44
+
45
+ def events(cli)
46
+ path = "/events"
47
+ path << "/#{cli[:fields][:client]}" if cli[:fields][:client]
48
+ path << "/#{cli[:fields][:check]}" if cli[:fields][:check]
49
+ respond(path)
50
+ end
51
+
52
+ def resolve(cli)
53
+ payload = {:client => cli[:fields][:client], :check => cli[:fields][:check]}.to_json
54
+ path = "/event/resolve"
55
+ respond(path,payload)
56
+ end
57
+
58
+ def silence(cli)
59
+ payload = {:timestamp => Time.now.to_i}
60
+ payload.merge!({:reason => cli[:fields][:reason]}) if cli[:fields][:reason]
61
+ if cli[:fields][:expires]
62
+ expires = Time.now.to_i + (cli[:fields][:expires] * 60)
63
+ payload.merge!({:expires => expires})
64
+ end
65
+ payload = payload.to_json
66
+ path = "/stashes/silence"
67
+ path << "/#{cli[:fields][:client]}" if cli[:fields][:client]
68
+ path << "/#{cli[:fields][:check]}" if cli[:fields][:check]
69
+ respond(path,payload)
70
+ end
71
+
72
+ def aggregates(cli)
73
+ path = "/aggregates"
74
+ path << "/#{cli[:fields][:check]}" if cli[:fields][:check]
75
+ path << "/#{cli[:fields][:id]}" if cli[:fields][:id]
76
+ path << pagination(cli)
77
+ respond(path)
78
+ end
79
+
80
+ def pagination(cli)
81
+ if cli[:fields].has_key?(:limit) && cli[:fields].has_key?(:offset)
82
+ page = "?limit=#{cli[:fields][:limit]}&offset=#{cli[:fields][:offset]}"
83
+ elsif cli[:fields].has_key?(:limit)
84
+ page = "?limit=#{cli[:fields][:limit]}"
85
+ else
86
+ page = ""
87
+ end
88
+ end
89
+
90
+ def respond(path,payload=false)
91
+ {:path => path, :payload => payload}
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,33 @@
1
+ module SensuCli
2
+ class Pretty
3
+
4
+ def self.print(res)
5
+ if !res.empty?
6
+ if res.is_a?(Hash)
7
+ res.each do |key,value|
8
+ puts "#{key}: ".color(:cyan) + "#{value}".color(:green)
9
+ end
10
+ elsif res.is_a?(Array)
11
+ res.each do |item|
12
+ puts "-------".color(:yellow)
13
+ if item.is_a?(Hash)
14
+ item.each do |key,value|
15
+ puts "#{key}: ".color(:cyan) + "#{value}".color(:green)
16
+ end
17
+ else
18
+ puts item.to_s.color(:cyan)
19
+ end
20
+ end
21
+ end
22
+ else
23
+ puts "no values for this request".color(:cyan)
24
+ end
25
+ end
26
+
27
+ def self.count(res)
28
+ res.is_a?(Hash) ? count = res.length : count = res.count
29
+ puts "#{count} total items".color(:yellow) if count
30
+ end
31
+
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module SensuCli
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Brandau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-21 00:00:00.000000000 Z
11
+ date: 2013-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rainbow
@@ -62,9 +62,11 @@ extra_rdoc_files: []
62
62
  files:
63
63
  - bin/sensu
64
64
  - bin/sensu-cli
65
+ - lib/sensu-cli/base.rb
65
66
  - lib/sensu-cli/cli.rb
66
67
  - lib/sensu-cli/editor.rb
67
- - lib/sensu-cli/sensu.rb
68
+ - lib/sensu-cli/path.rb
69
+ - lib/sensu-cli/pretty.rb
68
70
  - lib/sensu-cli/settings.rb
69
71
  - lib/sensu-cli/version.rb
70
72
  - lib/sensu-cli.rb
@@ -1,206 +0,0 @@
1
- require 'rubygems' if RUBY_VERSION < '1.9.0'
2
- require 'net/https'
3
- require 'json'
4
- require 'sensu-cli/settings'
5
- require 'sensu-cli/cli'
6
- require 'sensu-cli/editor'
7
- require 'rainbow'
8
-
9
- module SensuCli
10
- class Core
11
-
12
- def setup
13
- clis = Cli.new
14
- cli = clis.global
15
- settings
16
- @command = cli[:command]
17
- api_path(cli)
18
- make_call
19
- end
20
-
21
- def settings
22
- directory = "#{Dir.home}/.sensu"
23
- file = "#{directory}/settings.rb"
24
- alt = "/etc/sensu/sensu-cli/settings.rb"
25
- settings = Settings.new
26
- if settings.is_file?(file)
27
- SensuCli::Config.from_file(file)
28
- elsif settings.is_file?(alt)
29
- SensuCli::Config.from_file(alt)
30
- else
31
- settings.create(directory,file)
32
- end
33
- end
34
-
35
- def api_path(cli)
36
- self.respond_to?(@command) ? path = send(@command, cli) : (puts "Something Bad Happened";exit)
37
- @api = {:path => path, :method => cli[:method], :command => cli[:command], :payload => (@payload || false)}
38
- end
39
-
40
- def clients(cli)
41
- path = "/clients"
42
- path << "/#{cli[:fields][:name]}" if cli[:fields][:name]
43
- path << "/history" if cli[:fields][:history]
44
- path << pagination(cli)
45
- end
46
-
47
- def info(cli)
48
- path = "/info"
49
- end
50
-
51
- def health(cli)
52
- path = "/health?consumers=#{cli[:fields][:consumers]}&messages=#{cli[:fields][:messages]}"
53
- end
54
-
55
- def stashes(cli)
56
- if cli[:fields][:create]
57
- e = Editor.new
58
- @payload = e.create_stash(cli[:fields][:create_path]).to_json
59
- end
60
- path = "/stashes"
61
- path << "/#{cli[:fields][:path]}" if cli[:fields][:path]
62
- path << pagination(cli)
63
- end
64
-
65
- def checks(cli)
66
- if cli[:fields][:name]
67
- path = "/check/#{cli[:fields][:name]}"
68
- elsif cli[:fields][:subscribers]
69
- @payload = {:check => cli[:fields][:check],:subscribers => cli[:fields][:subscribers]}.to_json
70
- path = "/check/request"
71
- else
72
- path = "/checks"
73
- end
74
- end
75
-
76
- def events(cli)
77
- path = "/events"
78
- path << "/#{cli[:fields][:client]}" if cli[:fields][:client]
79
- path << "/#{cli[:fields][:check]}" if cli[:fields][:check]
80
- path
81
- end
82
-
83
- def resolve(cli)
84
- @payload = {:client => cli[:fields][:client], :check => cli[:fields][:check]}.to_json
85
- path = "/event/resolve"
86
- end
87
-
88
- def silence(cli)
89
- payload = {:timestamp => Time.now.to_i}
90
- payload.merge!({:reason => cli[:fields][:reason]}) if cli[:fields][:reason]
91
- if cli[:fields][:expires]
92
- expires = Time.now.to_i + (cli[:fields][:expires] * 60)
93
- payload.merge!({:expires => expires})
94
- end
95
- @payload = payload.to_json
96
- path = "/stashes/silence"
97
- path << "/#{cli[:fields][:client]}" if cli[:fields][:client]
98
- path << "/#{cli[:fields][:check]}" if cli[:fields][:check]
99
- path
100
- end
101
-
102
- def aggregates(cli)
103
- path = "/aggregates"
104
- path << "/#{cli[:fields][:check]}" if cli[:fields][:check]
105
- path << "/#{cli[:fields][:id]}" if cli[:fields][:id]
106
- path << pagination(cli)
107
- end
108
-
109
- def pagination(cli)
110
- if cli[:fields].has_key?(:limit) && cli[:fields].has_key?(:offset)
111
- page = "?limit=#{cli[:fields][:limit]}&offset=#{cli[:fields][:offset]}"
112
- elsif cli[:fields].has_key?(:limit)
113
- page = "?limit=#{cli[:fields][:limit]}"
114
- else
115
- page = ""
116
- end
117
- end
118
-
119
- def http_request
120
- http = Net::HTTP.new(Config.host, Config.port)
121
- http.read_timeout = 15
122
- http.open_timeout = 5
123
- if Config.ssl
124
- http.use_ssl = true
125
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
126
- end
127
- case @api[:method]
128
- when 'Get'
129
- req = Net::HTTP::Get.new(@api[:path])
130
- when 'Delete'
131
- req = Net::HTTP::Delete.new(@api[:path])
132
- when 'Post'
133
- req = Net::HTTP::Post.new(@api[:path],initheader = {'Content-Type' => 'application/json'})
134
- req.body = @api[:payload]
135
- end
136
- req.basic_auth(Config.user, Config.password) if Config.user && Config.password
137
- begin
138
- http.request(req)
139
- rescue Timeout::Error
140
- puts "HTTP request has timed out.".color(:red)
141
- exit
142
- rescue StandardError => e
143
- puts "An HTTP error occurred. Check your settings. #{e}".color(:red)
144
- exit
145
- end
146
- end
147
-
148
- def make_call
149
- res = http_request
150
- msg = response_codes(res.code,res.body)
151
- res.code != '200' ? exit : pretty(msg)
152
- count(msg)
153
- end
154
-
155
- def response_codes(code,body)
156
- case code
157
- when '200'
158
- JSON.parse(body)
159
- when '201'
160
- puts "The stash has been created." if @command == "stashes"
161
- when '202'
162
- puts "The item was submitted for processing."
163
- when '204'
164
- puts "Sensu is healthy" if @command == 'health'
165
- puts "The item was successfully deleted." if @command == 'aggregates' || @command == 'stashes'
166
- when '400'
167
- puts "The payload is malformed.".color(:red)
168
- when '401'
169
- puts "The request requires user authentication.".color(:red)
170
- when '404'
171
- puts "The item did not exist.".color(:cyan)
172
- else
173
- (@command == 'health') ? (puts "Sensu is not healthy.".color(:red)) : (puts "There was an error while trying to complete your request. Response code: #{code}".color(:red))
174
- end
175
- end
176
-
177
- def pretty(res)
178
- if !res.empty?
179
- if res.is_a?(Hash)
180
- res.each do |key,value|
181
- puts "#{key}: ".color(:cyan) + "#{value}".color(:green)
182
- end
183
- elsif res.is_a?(Array)
184
- res.each do |item|
185
- puts "-------".color(:yellow)
186
- if item.is_a?(Hash)
187
- item.each do |key,value|
188
- puts "#{key}: ".color(:cyan) + "#{value}".color(:green)
189
- end
190
- else
191
- puts item.to_s.color(:cyan)
192
- end
193
- end
194
- end
195
- else
196
- puts "no values for this request".color(:cyan)
197
- end
198
- end
199
-
200
- def count(res)
201
- res.is_a?(Hash) ? count = res.length : count = res.count
202
- puts "#{count} total items".color(:yellow) if count
203
- end
204
-
205
- end
206
- end