sensu-cli 0.3.1 → 0.4.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
- MzQyZTY3YzM1OTNlNzM3NzdmMWQ4MzcxN2E0MWQxOWQ1MjRhYzM1Nw==
4
+ MjIwNTc1ZDI4NDJiZjU2ODJlYWU0MDk2NTIxYWQyZTY4NWZjMzJjZg==
5
5
  data.tar.gz: !binary |-
6
- MzUyYzcwNjQ5MzA5MDE0ODIzYTFjZDdlODcxYTNkMDBlOTVhNDM3Ng==
6
+ ZGZiYWRmM2QwMmZkODc3MTRmNDMxMjI3NjBhNjM1MTdlZmNiYWJmMA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NzEzOTlhNjk3MjNlMmU1NjZkYTk2M2ZmZjg2OGNkOWQ5OTU1M2UxOTIwNDQy
10
- MWM2NmY2MGNkZDgxNmNlN2ZmMTkyZjljNTFkN2NlNjcwZmI3MjVlNWVlODBh
11
- YWVkOGMxNTI0N2JjYWU4OTdkMTg3Y2I4OGJmZjAyOGQzZWQ0MWI=
9
+ MDA0MTcyMmUyZGVkMDdmYjE0MTIyZjZkZWM2NTU1OGI0YzJiYzQyNjM1NTIz
10
+ OGU4MGFiN2NlMTRkMGQ5NmY4NmRlZjY5ZDUxYWE2OGQ3Y2JjMjQxMWJjNGJj
11
+ NTE3ZDNkNDI1YTM0MjJjZjkyOWFkZWVkNjdjMWFiMTYzYjkxZDM=
12
12
  data.tar.gz: !binary |-
13
- ZWRjNjMwNGM3NDZmNTZkYTUyMGJkMDZlYTBkYWVjZmY4MzQ5M2Q5YmViZmU1
14
- YmY0NGUzOGQ1ZjNhOTZiM2I5ZGQ4NTFkOWY4YzhiNjZkYjlhMDYyMzc5MGQw
15
- NzYxZTE0NGQzZDZhNWZhZGUwMzdhMzJkOGIwZDViNjJlZDgzMDQ=
13
+ MTA5YmM2MWI5NzU1NzMyMzdlYTRlMTRiNjRhMWM1NDAyZTc3MGRmOTlmM2Mw
14
+ NTJkZmYwNzFhOTJjNTZlNTM3YThjYjZlNzkzZGVlMzA0MzVhODA5NTVlOGZh
15
+ ZWQ1OWM1NDNjMWFlMTM5YjhlYThlMTRhM2I4YmMzOGI3MDRjNmU=
data/README.md CHANGED
@@ -41,23 +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
- * If your Sensu API has basic auth, add the parameters to the config.
45
-
46
- ````
47
- host "127.0.0.1"
48
- port "4567"
49
- ssl false
50
- user "some_user"
51
- password "some_secret_password"
52
- ````
53
- If `read_timeout` and `open_timeout` are not set, they default to 15 and
54
- 5 seconds respectively.
55
-
56
- Expire Silenced Hosts/Checks
57
- ----------------------------
58
- I added an expires option to `sensu-cli silence` to be used like `sensu-cli silence HOST -e 30` where `-e` denotes the number of minutes from now a host/checks silence should expire. This won't work by itself. We add a key to the silence stash with a time in the future. If you run [check-stashes](https://github.com/agent462/sensu-check-stashes) on your sensu-server it will check for expired stashes and delete them.
59
-
60
- There is also a reason option `-r` available. Be nice and use it so your colleagues know what you're doing.
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
61
51
 
62
52
  Examples
63
53
  -----------
@@ -111,10 +101,6 @@ Contributions
111
101
  -------------
112
102
  Please provide a pull request. I'm an ops guy, not a developer, so if you're submitting code cleanup, all I ask is that you explain the improvement so I can learn.
113
103
 
114
- TODO
115
- ----
116
- * cleanup the cli
117
- * Once complete api support is implemented I'll add other features like filtering or issuing a event.
118
104
 
119
105
  License and Author
120
106
  ==================
data/lib/sensu-cli.rb CHANGED
@@ -6,6 +6,7 @@ require 'sensu-cli/path.rb'
6
6
  require 'sensu-cli/api.rb'
7
7
  require 'sensu-cli/base.rb'
8
8
  require 'sensu-cli/version.rb'
9
+ require 'sensu-cli/filter.rb'
9
10
 
10
11
  module SensuCli
11
12
  end
data/lib/sensu-cli/api.rb CHANGED
@@ -1,10 +1,9 @@
1
1
  require 'net/https'
2
2
  require 'json'
3
- require 'rainbow'
3
+ require 'rainbow/ext/string'
4
4
 
5
5
  module SensuCli
6
6
  class Api
7
-
8
7
  def request(opts)
9
8
  http = Net::HTTP.new(opts[:host], opts[:port])
10
9
  http.read_timeout = opts[:read_timeout]
@@ -16,11 +15,11 @@ module SensuCli
16
15
  proxy_header = { 'api-proxy' => 'true' }
17
16
  case opts[:method]
18
17
  when 'Get'
19
- req = Net::HTTP::Get.new(opts[:path], initheader = proxy_header)
18
+ req = Net::HTTP::Get.new(opts[:path], proxy_header)
20
19
  when 'Delete'
21
- req = Net::HTTP::Delete.new(opts[:path], initheader = proxy_header)
20
+ req = Net::HTTP::Delete.new(opts[:path], proxy_header)
22
21
  when 'Post'
23
- req = Net::HTTP::Post.new(opts[:path], initheader = proxy_header.merge!({ 'Content-Type' => 'application/json' }))
22
+ req = Net::HTTP::Post.new(opts[:path], proxy_header.merge!('Content-Type' => 'application/json'))
24
23
  req.body = opts[:payload]
25
24
  end
26
25
  req.basic_auth(opts[:user], opts[:password]) if opts[:user] && opts[:password]
@@ -60,6 +59,5 @@ module SensuCli
60
59
  end
61
60
  end
62
61
  end
63
-
64
62
  end
65
63
  end
@@ -1,6 +1,5 @@
1
1
  module SensuCli
2
2
  class Base
3
-
4
3
  def setup
5
4
  clis = Cli.new
6
5
  @cli = clis.global
@@ -14,13 +13,14 @@ module SensuCli
14
13
  file = "#{directory}/settings.rb"
15
14
  alt = '/etc/sensu/sensu-cli/settings.rb'
16
15
  settings = Settings.new
17
- if settings.is_file?(file)
16
+ if settings.file?(file)
18
17
  SensuCli::Config.from_file(file)
19
- elsif settings.is_file?(alt)
18
+ elsif settings.file?(alt)
20
19
  SensuCli::Config.from_file(alt)
21
20
  else
22
21
  settings.create(directory, file)
23
22
  end
23
+ Rainbow.enabled = Config.pretty_colors == false ? false : true
24
24
  end
25
25
 
26
26
  def api_path(cli)
@@ -45,6 +45,7 @@ module SensuCli
45
45
  api = Api.new
46
46
  res = api.request(opts)
47
47
  msg = api.response(res.code, res.body, @api[:command])
48
+ msg = Filter.filter_json(msg, @cli[:fields][:filter]) if @cli[:fields][:filter]
48
49
  if res.code != '200'
49
50
  exit
50
51
  elsif @cli[:fields][:format] == 'single'
@@ -60,6 +61,5 @@ module SensuCli
60
61
  end
61
62
  Pretty.count(msg) unless @cli[:fields][:format] == 'table'
62
63
  end
63
-
64
64
  end
65
65
  end
data/lib/sensu-cli/cli.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require 'trollop'
2
2
  require 'sensu-cli/version'
3
- require 'rainbow'
3
+ require 'rainbow/ext/string'
4
4
 
5
5
  module SensuCli
6
- class Cli
6
+ class Cli # rubocop:disable ClassLength
7
7
  SUB_COMMANDS = %w(info client check event stash aggregate silence resolve health)
8
8
  CLIENT_COMMANDS = %w(list show delete history)
9
9
  CHECK_COMMANDS = %w(list show request)
@@ -109,7 +109,7 @@ module SensuCli
109
109
  end
110
110
 
111
111
  def deep_merge(hash_one, hash_two)
112
- hash_one.merge(hash_two) { |key, hash_one_item, hash_two_item| deep_merge(hash_one_item, hash_two_item) }
112
+ hash_one.merge(hash_two) { |_key, hash_one_item, hash_two_item| deep_merge(hash_one_item, hash_two_item) }
113
113
  end
114
114
 
115
115
  def parser(cli)
@@ -127,7 +127,7 @@ module SensuCli
127
127
  ARGV.shift
128
128
  end
129
129
 
130
- def client
130
+ def client # rubocop:disable MethodLength
131
131
  opts = parser('CLIENT')
132
132
  command = next_argv
133
133
  explode_if_empty(opts, command)
@@ -138,6 +138,7 @@ module SensuCli
138
138
  opt :offset, 'The number of clients to offset before returning', :short => 'o', :type => :string
139
139
  opt :format, 'Available formats; single, table, json', :short => 'f', :type => :string
140
140
  opt :fields, 'Fields for table ouput: -F name,address,subscriptions', :short => 'F', :type => :string
141
+ opt :filter, 'Field and value to filter on: name,graphite', :type => :string
141
142
  end
142
143
  Trollop::die :format, 'Available optional formats: single, table, json'.color(:red) if p[:format] != 'table' && p[:format] != 'single' && p[:format] != 'json' && p[:format]
143
144
  Trollop::die :fields, 'Fields must be used in conjunction with --format table'.color(:red) if p[:format] != 'table' && p[:fields]
@@ -215,7 +216,7 @@ module SensuCli
215
216
  p = Trollop::options
216
217
  item = next_argv
217
218
  check = next_argv
218
- explode(opts) if check == nil
219
+ explode(opts) if check.nil?
219
220
  deep_merge({ :command => 'events', :method => 'Delete', :fields => { :client => item, :check => check } }, { :fields => p })
220
221
  else
221
222
  explode(opts)
@@ -289,7 +290,7 @@ module SensuCli
289
290
  opt :expire, 'The number of seconds the silenced event is valid', :short => 'e', :type => :integer
290
291
  end
291
292
  command = next_argv
292
- explode(opts) if command == nil
293
+ explode(opts) if command.nil?
293
294
  deep_merge({ :command => 'silence', :method => 'Post', :fields => { :client => command } }, { :fields => p })
294
295
  end
295
296
 
@@ -300,6 +301,5 @@ module SensuCli
300
301
  ARGV.empty? ? explode(opts) : check = next_argv
301
302
  deep_merge({ :command => 'resolve', :method => 'Post', :fields => { :client => command, :check => check } }, { :fields => p })
302
303
  end
303
-
304
304
  end
305
305
  end
@@ -3,9 +3,8 @@ require 'tempfile'
3
3
 
4
4
  module SensuCli
5
5
  class Editor
6
-
7
6
  def create_stash(create_path)
8
- file = temp_file({ :path => create_path, :content => { :timestamp => Time.now.to_i } })
7
+ file = temp_file(:path => create_path, :content => { :timestamp => Time.now.to_i })
9
8
  edit(file)
10
9
  end
11
10
 
@@ -26,6 +25,5 @@ module SensuCli
26
25
  file.close
27
26
  file.path
28
27
  end
29
-
30
28
  end
31
29
  end
@@ -0,0 +1,26 @@
1
+ module SensuCli
2
+ class Filter
3
+ class << self
4
+ def filter_split(filter)
5
+ filter.sub(' ', '').split(',')
6
+ end
7
+
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
20
+ else
21
+ res
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,6 +1,5 @@
1
1
  module SensuCli
2
2
  class PathCreator
3
-
4
3
  def clients(cli)
5
4
  path = '/clients'
6
5
  path << "/#{cli[:fields][:name]}" if cli[:fields][:name]
@@ -9,7 +8,7 @@ module SensuCli
9
8
  respond(path)
10
9
  end
11
10
 
12
- def info(cli)
11
+ def info(*)
13
12
  path = '/info'
14
13
  respond(path)
15
14
  end
@@ -57,14 +56,14 @@ module SensuCli
57
56
 
58
57
  def silence(cli)
59
58
  content = { :timestamp => Time.now.to_i }
60
- content.merge!({ :owner => cli[:fields][:owner] }) if cli[:fields][:owner]
61
- content.merge!({ :reason => cli[:fields][:reason] }) if cli[:fields][:reason]
59
+ content.merge!(:owner => cli[:fields][:owner]) if cli[:fields][:owner]
60
+ content.merge!(:reason => cli[:fields][:reason]) if cli[:fields][:reason]
62
61
  payload = { :content => content }
63
- payload.merge!({ :expire => cli[:fields][:expire].to_i }) if cli[:fields][:expire]
62
+ payload.merge!(:expire => cli[:fields][:expire].to_i) if cli[:fields][:expire]
64
63
  silence_path = 'silence'
65
64
  silence_path << "/#{cli[:fields][:client]}" if cli[:fields][:client]
66
65
  silence_path << "/#{cli[:fields][:check]}" if cli[:fields][:check]
67
- payload = payload.merge!({ :path => silence_path }).to_json
66
+ payload = payload.merge!(:path => silence_path).to_json
68
67
  respond('/stashes', payload)
69
68
  end
70
69
 
@@ -77,22 +76,21 @@ module SensuCli
77
76
  end
78
77
 
79
78
  def pagination(cli)
80
- if cli[:fields].has_key?(:limit) && cli[:fields].has_key?(:offset)
81
- page = "?limit=#{cli[:fields][:limit]}&offset=#{cli[:fields][:offset]}"
82
- elsif cli[:fields].has_key?(:limit)
83
- page = "?limit=#{cli[:fields][:limit]}"
79
+ if cli[:fields].key?(:limit) && cli[:fields].key?(:offset)
80
+ "?limit=#{cli[:fields][:limit]}&offset=#{cli[:fields][:offset]}"
81
+ elsif cli[:fields].key?(:limit)
82
+ "?limit=#{cli[:fields][:limit]}"
84
83
  else
85
- page = ''
84
+ ''
86
85
  end
87
86
  end
88
87
 
89
- def method_missing(method_name, *args)
88
+ def method_missing(method_name, *_args)
90
89
  puts "Path method: #{method_name} does not exist. "
91
90
  end
92
91
 
93
92
  def respond(path, payload = false)
94
93
  { :path => path, :payload => payload }
95
94
  end
96
-
97
95
  end
98
96
  end
@@ -1,11 +1,10 @@
1
- require 'rainbow'
1
+ require 'rainbow/ext/string'
2
2
  require 'hirb'
3
3
  require 'json'
4
4
 
5
5
  module SensuCli
6
6
  class Pretty
7
7
  class << self
8
-
9
8
  def print(res)
10
9
  if !res.empty?
11
10
  if res.is_a?(Hash)
@@ -25,7 +24,7 @@ module SensuCli
25
24
  end
26
25
  end
27
26
  else
28
- self.no_values
27
+ no_values
29
28
  end
30
29
  end
31
30
 
@@ -65,22 +64,21 @@ module SensuCli
65
64
  end
66
65
 
67
66
  # Print header
68
- format = keys.map { |key| "%-#{value_lengths[key]}s" }.join(' ')
69
- puts sprintf(format, *keys)
67
+ lengths = keys.map { |key| "%-#{value_lengths[key]}s" }.join(' ')
68
+ puts format(lengths, *keys)
70
69
 
71
70
  # Print value rows
72
71
  res.each do |item|
73
72
  if item.is_a?(Hash)
74
73
  values = keys.map { |key| item[key] }
75
- format = keys.map { |key| "%-#{value_lengths[key]}s" }.join(' ')
76
- puts sprintf(format, *values)
74
+ puts format(lengths, *values)
77
75
  else
78
76
  puts item.to_s.color(:cyan)
79
77
  end
80
78
  end
81
79
  end
82
80
  else
83
- self.no_values
81
+ no_values
84
82
  end
85
83
  end
86
84
 
@@ -93,21 +91,20 @@ module SensuCli
93
91
  if res.is_a?(Array)
94
92
  terminal_size = Hirb::Util.detect_terminal_size
95
93
  if endpoint == 'events'
96
- keys = %w[check client status flapping occurrences handlers issued output]
94
+ keys = %w(check client status flapping occurrences handlers issued output)
97
95
  else
98
96
  if fields
99
- keys = self.parse_fields(fields)
97
+ keys = parse_fields(fields)
100
98
  else
101
99
  keys = res.map { |item| item.keys }.flatten.uniq
102
100
  end
103
101
  end
104
- puts Hirb::Helpers::AutoTable.render(res, { :max_width => terminal_size[0], :fields => keys })
102
+ puts Hirb::Helpers::AutoTable.render(res, :max_width => terminal_size[0], :fields => keys)
105
103
  end
106
104
  else
107
- self.no_values
105
+ no_values
108
106
  end
109
107
  end
110
-
111
108
  end
112
109
  end
113
110
  end
@@ -1,11 +1,10 @@
1
1
  require 'fileutils'
2
2
  require 'mixlib/config'
3
- require 'rainbow'
3
+ require 'rainbow/ext/string'
4
4
 
5
5
  module SensuCli
6
6
  class Settings
7
-
8
- def is_file?(file)
7
+ def file?(file)
9
8
  !File.readable?(file) ? false : true # rubocop:disable FavorUnlessOverNegatedIf
10
9
  end
11
10
 
@@ -15,11 +14,9 @@ module SensuCli
15
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)
16
15
  exit
17
16
  end
18
-
19
17
  end
20
18
 
21
19
  class Config
22
20
  extend(Mixlib::Config)
23
21
  end
24
-
25
22
  end
@@ -1,3 +1,3 @@
1
1
  module SensuCli
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
data/sensu-cli.gemspec CHANGED
@@ -4,23 +4,23 @@ Gem::Specification.new do |s|
4
4
  s.name = 'sensu-cli'
5
5
  s.version = SensuCli::VERSION
6
6
  s.platform = Gem::Platform::RUBY
7
- s.summary = "A command line utility for Sensu."
8
- s.description = "A command line utility for interacting with the Sensu api."
9
- s.authors = ["Bryan Brandau"]
7
+ s.summary = 'A command line utility for Sensu.'
8
+ s.description = 'A command line utility for interacting with the Sensu api.'
9
+ s.authors = ['Bryan Brandau']
10
10
  s.email = 'agent462@gmail.com'
11
11
  s.has_rdoc = false
12
- s.licenses = ['MIT','APACHE']
13
- s.homepage ='http://github.com/agent462/sensu-cli'
12
+ s.licenses = %w(MIT APACHE)
13
+ s.homepage = 'http://github.com/agent462/sensu-cli'
14
14
 
15
- s.add_dependency('rainbow', '1.1.4')
15
+ s.add_dependency('rainbow', '2.0.0')
16
16
  s.add_dependency('trollop', '2.0')
17
- s.add_dependency('mixlib-config', '1.1.2')
17
+ s.add_dependency('mixlib-config', '2.1.0')
18
18
  s.add_dependency('hirb', '0.7.1')
19
19
 
20
20
  s.add_development_dependency('rspec')
21
21
  s.add_development_dependency('rubocop')
22
22
 
23
- s.files = Dir.glob('{bin,lib}/**/*') + %w[sensu-cli.gemspec README.md settings.example.rb]
23
+ s.files = Dir.glob('{bin,lib}/**/*') + %w(sensu-cli.gemspec README.md settings.example.rb)
24
24
  s.executables = Dir.glob('bin/**/*').map { |file| File.basename(file) }
25
25
  s.require_paths = ['lib']
26
26
  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.3.1
4
+ version: 0.4.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: 2013-12-12 00:00:00.000000000 Z
11
+ date: 2014-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rainbow
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.1.4
19
+ version: 2.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.1.4
26
+ version: 2.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: trollop
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: 1.1.2
47
+ version: 2.1.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: 1.1.2
54
+ version: 2.1.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: hirb
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +108,7 @@ files:
108
108
  - lib/sensu-cli/base.rb
109
109
  - lib/sensu-cli/cli.rb
110
110
  - lib/sensu-cli/editor.rb
111
+ - lib/sensu-cli/filter.rb
111
112
  - lib/sensu-cli/path.rb
112
113
  - lib/sensu-cli/pretty.rb
113
114
  - lib/sensu-cli/settings.rb