sensu-cli 0.4.0 → 0.5.0

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
- MjIwNTc1ZDI4NDJiZjU2ODJlYWU0MDk2NTIxYWQyZTY4NWZjMzJjZg==
4
+ Mjk0ZjhhMjdkYjBjMzE1ODQwZTdhNmU3NWEyZDU1MGU5YWRkZTdiOQ==
5
5
  data.tar.gz: !binary |-
6
- ZGZiYWRmM2QwMmZkODc3MTRmNDMxMjI3NjBhNjM1MTdlZmNiYWJmMA==
6
+ ODgyZTA5OThlMjlmYWFlODVkY2Y4MjRkYTBmNmMwZjM5MThkNGJjMQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MDA0MTcyMmUyZGVkMDdmYjE0MTIyZjZkZWM2NTU1OGI0YzJiYzQyNjM1NTIz
10
- OGU4MGFiN2NlMTRkMGQ5NmY4NmRlZjY5ZDUxYWE2OGQ3Y2JjMjQxMWJjNGJj
11
- NTE3ZDNkNDI1YTM0MjJjZjkyOWFkZWVkNjdjMWFiMTYzYjkxZDM=
9
+ M2U4NzY3MTlkNmI4YzM0MjYxNzRhZTQwZjNiMGZlZTg1YWU1MzU1ODY1Nzk0
10
+ NjY5YWMyZWU2MzNmOTVjMGJmZTMyMjQ4NzBiMGQ5NGFjN2Y0OTg4ODc3YmUy
11
+ ODUzOGY1M2RiMDc5OTc4MmJiMWI2OTE3ZWZmYTY5MzQxY2IwZDc=
12
12
  data.tar.gz: !binary |-
13
- MTA5YmM2MWI5NzU1NzMyMzdlYTRlMTRiNjRhMWM1NDAyZTc3MGRmOTlmM2Mw
14
- NTJkZmYwNzFhOTJjNTZlNTM3YThjYjZlNzkzZGVlMzA0MzVhODA5NTVlOGZh
15
- ZWQ1OWM1NDNjMWFlMTM5YjhlYThlMTRhM2I4YmMzOGI3MDRjNmU=
13
+ YmYyNTU5MzUwYzQ5N2E4OTNiODRmY2Q2NjMyY2ZmNTBmNTJjYjcwZmI4YTky
14
+ Yzk2M2NhNTliNzdmZTkzMzQ5NTAyN2EwZTU5MjEyYTkwNDBjZmRmYjliYWZm
15
+ NzU4NDlmNDVmYmFiNzk0ODgzYTBkOTU4YmZlY2U1YTJkZWIxMWM=
data/README.md CHANGED
@@ -41,13 +41,13 @@ open_timeout 20
41
41
  ````
42
42
  This format was chosen so you can do some ENV magic via your profile and setting up an alias. For details see the [wiki](https://github.com/agent462/sensu-cli/wiki)
43
43
 
44
- * All Configuration Settings
45
- `host` String - Required
46
- `port` String/Integer - Required
47
- `ssl` Boolean - Optional - Defaults False
48
- `read_timeout` Integer - Optional - Default 15 (seconds)
49
- `open_timeout` Integer - Optional - Default 5 (seconds)
50
- `pretty_colors` Boolean - Optional - Default True
44
+ * All Configuration Settings
45
+ `host` String - Required
46
+ `port` String/Integer - Required
47
+ `ssl` Boolean - Optional - Defaults False
48
+ `read_timeout` Integer - Optional - Default 15 (seconds)
49
+ `open_timeout` Integer - Optional - Default 5 (seconds)
50
+ `pretty_colors` Boolean - Optional - Default True
51
51
 
52
52
  Examples
53
53
  -----------
@@ -9,4 +9,8 @@ require 'sensu-cli/version.rb'
9
9
  require 'sensu-cli/filter.rb'
10
10
 
11
11
  module SensuCli
12
+ def self.die(code = 0, msg = nil)
13
+ at_exit { puts msg }
14
+ Kernel.exit(code)
15
+ end
12
16
  end
@@ -26,11 +26,9 @@ module SensuCli
26
26
  begin
27
27
  http.request(req)
28
28
  rescue Timeout::Error
29
- puts 'HTTP request has timed out.'.color(:red)
30
- exit
29
+ SensuCli::die(1, 'HTTP request has timed out.'.color(:red))
31
30
  rescue StandardError => e
32
- puts "An HTTP error occurred. Check your settings. #{e}".color(:red)
33
- exit
31
+ SensuCli::die(1, "An HTTP error occurred. Check your settings. #{e}".color(:red))
34
32
  end
35
33
  end
36
34
 
@@ -1,8 +1,7 @@
1
1
  module SensuCli
2
2
  class Base
3
3
  def setup
4
- clis = Cli.new
5
- @cli = clis.global
4
+ @cli = Cli.new.global
6
5
  settings
7
6
  api_path(@cli)
8
7
  make_call
@@ -25,7 +24,7 @@ module SensuCli
25
24
 
26
25
  def api_path(cli)
27
26
  p = PathCreator.new
28
- p.respond_to?(cli[:command]) ? path = p.send(cli[:command], cli) : (puts 'Something Bad Happened'; exit) # rubocop:disable Semicolon
27
+ p.respond_to?(cli[:command]) ? path = p.send(cli[:command], cli) : SensuCli::die(1, 'Something Bad Happened')
29
28
  @api = { :path => path[:path], :method => cli[:method], :command => cli[:command], :payload => (path[:payload] || false) }
30
29
  end
31
30
 
@@ -45,9 +44,9 @@ module SensuCli
45
44
  api = Api.new
46
45
  res = api.request(opts)
47
46
  msg = api.response(res.code, res.body, @api[:command])
48
- msg = Filter.filter_json(msg, @cli[:fields][:filter]) if @cli[:fields][:filter]
47
+ msg = Filter.new(@cli[:fields][:filter]).process(msg) if @cli[:fields][:filter]
49
48
  if res.code != '200'
50
- exit
49
+ SensuCli::die(0)
51
50
  elsif @cli[:fields][:format] == 'single'
52
51
  Pretty.single(msg)
53
52
  elsif @cli[:fields][:format] == 'table'
@@ -180,14 +180,18 @@ module SensuCli
180
180
  opts = parser('CHECK')
181
181
  command = next_argv
182
182
  explode_if_empty(opts, command)
183
- p = Trollop::options
184
183
  item = next_argv
185
184
  case command
186
185
  when 'list'
186
+ p = Trollop::options do
187
+ opt :filter, 'Field and value to filter on: command,procs', :type => :string
188
+ end
187
189
  { :command => 'checks', :method => 'Get', :fields => p }
188
190
  when 'show'
191
+ p = Trollop::options
189
192
  deep_merge({ :command => 'checks', :method => 'Get', :fields => { :name => item } }, { :fields => p })
190
193
  when 'request'
194
+ p = Trollop::options
191
195
  ARGV.empty? ? explode(opts) : subscribers = next_argv.split(',')
192
196
  deep_merge({ :command => 'checks', :method => 'Post', :fields => { :check => item, :subscribers => subscribers } }, { :fields => p })
193
197
  else
@@ -202,6 +206,7 @@ module SensuCli
202
206
  case command
203
207
  when 'list'
204
208
  p = Trollop::options do
209
+ opt :filter, 'Field and value to filter on: client,graphite', :type => :string
205
210
  opt :format, 'Available formats; single, table, json', :short => 'f', :type => :string
206
211
  end
207
212
  Trollop::die :format, 'Available optional formats: single, table, json'.color(:red) if p[:format] != 'table' && p[:format] != 'single' && p[:format] != 'json' && p[:format]
@@ -233,6 +238,7 @@ module SensuCli
233
238
  opt :limit, 'The number of stashes to return', :short => 'l', :type => :string
234
239
  opt :offset, 'The number of stashes to offset before returning', :short => 'o', :type => :string
235
240
  opt :format, 'Available formats; single, table, json', :short => 'f', :type => :string
241
+ opt :filter, 'Field and value to filter on: path,graphite', :type => :string
236
242
  end
237
243
  Trollop::die :offset, 'Offset depends on the limit option --limit ( -l )'.color(:red) if p[:offset] && !p[:limit]
238
244
  Trollop::die :format, 'Available optional formats: single, table, json'.color(:red) if p[:format] != 'table' && p[:format] != 'single' && p[:format] != 'json' && p[:format]
@@ -260,7 +266,9 @@ module SensuCli
260
266
  explode_if_empty(opts, command)
261
267
  case command
262
268
  when 'list'
263
- p = Trollop::options
269
+ p = Trollop::options do
270
+ opt :filter, 'Field and value to filter on: issued,1399505890', :type => :string
271
+ end
264
272
  { :command => 'aggregates', :method => 'Get', :fields => p }
265
273
  when 'show'
266
274
  p = Trollop::options do
@@ -14,8 +14,7 @@ module SensuCli
14
14
  begin
15
15
  JSON.parse(File.read(file))
16
16
  rescue JSON::ParserError
17
- puts 'The stash you created has Invalid JSON.'
18
- exit
17
+ SensuCli::die(1, 'The stash you created has invalid JSON.')
19
18
  end
20
19
  end
21
20
 
@@ -1,25 +1,38 @@
1
1
  module SensuCli
2
2
  class Filter
3
- class << self
4
- def filter_split(filter)
5
- filter.sub(' ', '').split(',')
6
- end
3
+ attr_reader :filter
4
+
5
+ def initialize(filter_data)
6
+ filter_split(filter_data)
7
+ end
7
8
 
8
- def filter_json(res, filter)
9
- filter = filter_split(filter)
10
- if !res.empty?
11
- res.select do |item|
12
- if item.key? filter[0]
13
- if item[filter[0]].is_a?(Array)
14
- item unless item[filter[0]].select { |value| value.include? filter[1] }.empty?
15
- else
16
- item if item[filter[0]].include? filter[1]
17
- end
18
- end
19
- end
9
+ def filter_split(filter)
10
+ @filter = filter.sub(' ', '').split(',')
11
+ end
12
+
13
+ def match?(data)
14
+ data.to_s.include? filter[1]
15
+ end
16
+
17
+ def inspect_hash(data)
18
+ data.any? do |key, value|
19
+ if value.is_a?(Array)
20
+ match?(value) if key == filter[0]
21
+ elsif value.is_a?(Hash)
22
+ process(value)
20
23
  else
21
- res
24
+ match?(value) if key == filter[0]
25
+ end
26
+ end
27
+ end
28
+
29
+ def process(data)
30
+ if data.is_a?(Array)
31
+ data.select do |value|
32
+ process(value)
22
33
  end
34
+ elsif data.is_a?(Hash)
35
+ inspect_hash(data)
23
36
  end
24
37
  end
25
38
  end
@@ -11,8 +11,7 @@ module SensuCli
11
11
  def create(directory, file)
12
12
  FileUtils.mkdir_p(directory) unless File.directory?(directory)
13
13
  FileUtils.cp(File.join(File.dirname(__FILE__), '../../settings.example.rb'), file)
14
- puts "We created the configuration file for you at #{file}. You can also place this in /etc/sensu/sensu-cli. Edit the settings as needed.".color(:red)
15
- exit
14
+ SensuCli::die(0, "We created the configuration file for you at #{file}. You can also place this in /etc/sensu/sensu-cli. Edit the settings as needed.".color(:red))
16
15
  end
17
16
  end
18
17
 
@@ -1,3 +1,3 @@
1
1
  module SensuCli
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Brandau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-07 00:00:00.000000000 Z
11
+ date: 2014-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rainbow