sensu-plugin 1.2.0 → 1.3.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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b8bf9dc7a64cb5b2a2359146d1d8cfa747fc8e6
4
- data.tar.gz: 52223d03497178da064f55e89e307d1ab86dfef4
3
+ metadata.gz: 2730e59c7a3b4d3dbc9d167d84a5ca0a7e428ae7
4
+ data.tar.gz: 4922478ec5392ab0efb0e41bfee2076c175aee7a
5
5
  SHA512:
6
- metadata.gz: d698f30f72883a2024bb7096c14cc42ec4fa740a12e337c7903018b76b2e30444030fe907b8dc962d3f2a87c2dac6fc08b2400ec00fb36d84d9f0f4d83a654f5
7
- data.tar.gz: 5768eb08725dc2f4d723a2dc367587854f108082c08804fa6679982fd70ce684fcbb4f1f8867171a27cff6119ff94d1015594162ccdd465d619edec46f8eef13
6
+ metadata.gz: 76dd04a3e1c2819c4cff3daefbb1f6ffeb250820b8e7d3fcbfa9e9a4f467074b3b25839c3fc166cda4b6131743465bcb12946e8b2de033afb84391897fbe05a2
7
+ data.tar.gz: bfdf09aff3a445ffc1a9f94d21d46a28193812e633b8d0c91fb5354d0c3f3b07f50a347cfead92f7d06d6db1c910ac1e461e02d64d7e1ca9b9ee85a829a76703
@@ -1,4 +1,6 @@
1
1
  require 'net/http'
2
+ require 'timeout'
3
+ require 'uri'
2
4
  require 'json'
3
5
  require 'sensu-plugin/utils'
4
6
  require 'mixlib/cli'
@@ -71,21 +73,41 @@ module Sensu
71
73
  end
72
74
 
73
75
  def bail(msg)
74
- puts msg + ': ' + @event['client']['name'] + '/' + @event['check']['name']
76
+ client_name = @event['client']['name'] || 'error:no-client-name'
77
+ check_name = @event['check']['name'] || 'error:no-check-name'
78
+ puts "#{msg}: #{client_name}/#{check_name}"
75
79
  exit 0
76
80
  end
77
81
 
82
+ def api_settings
83
+ @api_settings ||= if ENV['SENSU_API_URL']
84
+ uri = URI(ENV['SENSU_API_URL'])
85
+ {
86
+ 'host' => uri.host,
87
+ 'port' => uri.port,
88
+ 'user' => uri.user,
89
+ 'password' => uri.password
90
+ }
91
+ else
92
+ settings['api']
93
+ end
94
+ end
95
+
78
96
  def api_request(method, path, &blk)
79
- if not settings.has_key?('api')
97
+ if api_settings.nil?
80
98
  raise "api.json settings not found."
81
99
  end
82
- http = Net::HTTP.new(settings['api']['host'], settings['api']['port'])
83
- req = net_http_req_class(method).new(path)
84
- if settings['api']['user'] && settings['api']['password']
85
- req.basic_auth(settings['api']['user'], settings['api']['password'])
100
+ domain = api_settings['host'].start_with?('http') ? api_settings['host'] : 'http://' + api_settings['host']
101
+ uri = URI("#{domain}:#{api_settings['port']}#{path}")
102
+ req = net_http_req_class(method).new(uri)
103
+ if api_settings['user'] && api_settings['password']
104
+ req.basic_auth(api_settings['user'], api_settings['password'])
86
105
  end
87
106
  yield(req) if block_given?
88
- http.request(req)
107
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
108
+ http.request(req)
109
+ end
110
+ res
89
111
  end
90
112
 
91
113
  def filter_disabled
@@ -113,7 +135,7 @@ module Sensu
113
135
  end
114
136
  if @event['occurrences'] > occurrences && @event['action'] == 'create'
115
137
  number = refresh.fdiv(interval).to_i
116
- unless number == 0 || @event['occurrences'] % number == 0
138
+ unless number == 0 || (@event['occurrences'] - occurrences) % number == 0
117
139
  bail 'only handling every ' + number.to_s + ' occurrences'
118
140
  end
119
141
  end
@@ -131,7 +153,7 @@ module Sensu
131
153
  ]
132
154
  stashes.each do |(scope, path)|
133
155
  begin
134
- timeout(2) do
156
+ Timeout.timeout(5) do
135
157
  if stash_exists?(path)
136
158
  bail scope + ' alerts silenced'
137
159
  end
@@ -153,7 +175,7 @@ module Sensu
153
175
  if @event['check']['dependencies'].is_a?(Array)
154
176
  @event['check']['dependencies'].each do |dependency|
155
177
  begin
156
- timeout(2) do
178
+ Timeout.timeout(2) do
157
179
  check, client = dependency.split('/').reverse
158
180
  if event_exists?(client || @event['client']['name'], check)
159
181
  bail 'check dependency event exists'
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Sensu-mutator
4
+ # ===
5
+ #
6
+ # DESCRIPTION:
7
+ # Base mutator class. All you need to do is extend this class and implement a
8
+ # #mutate function. Uses the autorun feature just like sensu-handler and sensu-plugin/cli
9
+ #
10
+ # Example Implementation: described https://sensuapp.org/docs/latest/mutators#example-mutator-plugin
11
+ #
12
+ # class Helper < Sensu::Mutator
13
+ # def mutate
14
+ # @event.merge!(:mutated => true)
15
+ # end
16
+ # end
17
+ #
18
+ # PLATFORM:
19
+ # all
20
+ #
21
+ # DEPENDENCIES:
22
+ # sensu-plugin/utils
23
+ # mixlib/cli
24
+ #
25
+ # Copyright 2015 Zach Bintliff <https://github.com/zbintliff>
26
+ #
27
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
28
+ # for details.
29
+ require 'json'
30
+ require 'sensu-plugin/utils'
31
+ require 'mixlib/cli'
32
+
33
+ module Sensu
34
+ class Mutator
35
+ include Sensu::Plugin::Utils
36
+ include Mixlib::CLI
37
+
38
+ attr_accessor :argv
39
+
40
+ def initialize(argv = ARGV)
41
+ super()
42
+ self.argv = parse_options(argv)
43
+ end
44
+
45
+ def mutate
46
+ ## Override this, be sure any changes are made to @event
47
+ nil
48
+ end
49
+
50
+ def dump
51
+ puts JSON.dump(@event)
52
+ end
53
+
54
+ # This works just like Plugin::CLI's autorun.
55
+ @@autorun = self
56
+ class << self
57
+ def method_added(name)
58
+ if name == :mutate
59
+ @@autorun = self
60
+ end
61
+ end
62
+ end
63
+
64
+ def self.disable_autorun
65
+ @@autorun = false
66
+ end
67
+
68
+ at_exit do
69
+ if @@autorun
70
+ mutator = @@autorun.new
71
+ mutator.read_event(STDIN)
72
+ mutator.mutate
73
+ mutator.dump
74
+ end
75
+ end
76
+ end
77
+ end
@@ -1,6 +1,6 @@
1
1
  module Sensu
2
2
  module Plugin
3
- VERSION = "1.2.0"
3
+ VERSION = "1.3.0"
4
4
  EXIT_CODES = {
5
5
  'OK' => 0,
6
6
  'WARNING' => 1,
@@ -51,20 +51,22 @@ module Sensu
51
51
  end
52
52
 
53
53
  at_exit do
54
- begin
55
- check = @@autorun.new
56
- check.run
57
- rescue SystemExit => e
58
- exit e.status
59
- rescue OptionParser::InvalidOption => e
60
- puts "Invalid check argument(s): #{e.message}, #{e.backtrace}"
61
- exit 1
62
- rescue Exception => e
63
- # This can't call check.critical, as the check may have failed to construct
64
- puts "Check failed to run: #{e.message}, #{e.backtrace}"
65
- exit 2
54
+ if @@autorun
55
+ begin
56
+ check = @@autorun.new
57
+ check.run
58
+ rescue SystemExit => e
59
+ exit e.status
60
+ rescue OptionParser::InvalidOption => e
61
+ puts "Invalid check argument(s): #{e.message}, #{e.backtrace}"
62
+ exit 1
63
+ rescue Exception => e
64
+ # This can't call check.critical, as the check may have failed to construct
65
+ puts "Check failed to run: #{e.message}, #{e.backtrace}"
66
+ exit 2
67
+ end
68
+ check.warning "Check did not exit! You should call an exit code method."
66
69
  end
67
- check.warning "Check did not exit! You should call an exit code method."
68
70
  end
69
71
 
70
72
  end
@@ -19,11 +19,13 @@ module Sensu
19
19
 
20
20
  class Graphite < Sensu::Plugin::CLI
21
21
  def output(*args)
22
- if args[0].is_a?(Exception) || args[1].nil?
23
- puts args[0].to_s
24
- else
25
- args[2] ||= Time.now.to_i
26
- puts args[0..2].join("\s")
22
+ unless args.empty?
23
+ if args[0].is_a?(Exception) || args[1].nil?
24
+ puts args[0].to_s
25
+ else
26
+ args[2] ||= Time.now.to_i
27
+ puts args[0..2].join("\s")
28
+ end
27
29
  end
28
30
  end
29
31
  end
@@ -59,7 +59,7 @@ class TestFilterExternal < MiniTest::Unit::TestCase
59
59
  event = {
60
60
  'client' => { 'name' => 'test' },
61
61
  'check' => { 'name' => 'test' },
62
- 'occurrences' => 60,
62
+ 'occurrences' => 61,
63
63
  'action' => 'create'
64
64
  }
65
65
  output = run_script_with_input(JSON.generate(event))
@@ -71,7 +71,7 @@ class TestFilterExternal < MiniTest::Unit::TestCase
71
71
  event = {
72
72
  'client' => { 'name' => 'test' },
73
73
  'check' => { 'name' => 'test' },
74
- 'occurrences' => 59,
74
+ 'occurrences' => 60,
75
75
  'action' => 'create'
76
76
  }
77
77
  output = run_script_with_input(JSON.generate(event))
@@ -83,7 +83,7 @@ class TestFilterExternal < MiniTest::Unit::TestCase
83
83
  event = {
84
84
  'client' => { 'name' => 'test' },
85
85
  'check' => { 'name' => 'test', 'refresh' => 0 },
86
- 'occurrences' => 59,
86
+ 'occurrences' => 60,
87
87
  'action' => 'create'
88
88
  }
89
89
  output = run_script_with_input(JSON.generate(event))
@@ -95,7 +95,7 @@ class TestFilterExternal < MiniTest::Unit::TestCase
95
95
  event = {
96
96
  'client' => { 'name' => 'test' },
97
97
  'check' => { 'name' => 'test', 'refresh' => 30 },
98
- 'occurrences' => 59,
98
+ 'occurrences' => 60,
99
99
  'action' => 'create'
100
100
  }
101
101
  output = run_script_with_input(JSON.generate(event))
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test_helper'
4
+
5
+ # Simple Heper to test mutator
6
+ class TestMutatorHelpers < MiniTest::Test
7
+ include SensuPluginTestHelper
8
+ def test_base_mutator
9
+ set_script 'external/mutator-trivial'
10
+ event = JSON.parse(fixture('basic_event.json').read)
11
+ output = run_script_with_input(JSON.generate(event))
12
+ assert_equal(0, $?.exitstatus)
13
+ assert_equal(event, JSON.parse(output))
14
+ end
15
+
16
+ def test_external_mutator
17
+ set_script 'external/mutator-helpers'
18
+ event = JSON.parse(fixture('basic_event.json').read)
19
+ output = run_script_with_input(JSON.generate(event))
20
+ assert_equal(0, $?.exitstatus)
21
+ assert_equal(true, JSON.parse(output)['mutated'])
22
+ end
23
+ end
@@ -3,7 +3,6 @@ gem 'minitest' if RUBY_VERSION < '1.9.0'
3
3
  require 'minitest/autorun'
4
4
 
5
5
  module SensuPluginTestHelper
6
-
7
6
  def set_script(script)
8
7
  @script = File.join(File.dirname(__FILE__), script)
9
8
  end
@@ -22,4 +21,11 @@ module SensuPluginTestHelper
22
21
  end
23
22
  end
24
23
 
24
+ def fixture_path
25
+ File.expand_path('../fixtures', __FILE__)
26
+ end
27
+
28
+ def fixture(f)
29
+ File.new(File.join(fixture_path, f))
30
+ end
25
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-plugin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Decklin Foster
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-07-09 00:00:00.000000000 Z
12
+ date: 2016-06-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -31,14 +31,14 @@ dependencies:
31
31
  requirements:
32
32
  - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: 1.1.0
34
+ version: 1.5.0
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
- version: 1.1.0
41
+ version: 1.5.0
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rake
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -76,6 +76,7 @@ extensions: []
76
76
  extra_rdoc_files: []
77
77
  files:
78
78
  - lib/sensu-handler.rb
79
+ - lib/sensu-mutator.rb
79
80
  - lib/sensu-plugin.rb
80
81
  - lib/sensu-plugin/check/cli.rb
81
82
  - lib/sensu-plugin/cli.rb
@@ -87,8 +88,9 @@ files:
87
88
  - test/external_metric_test.rb
88
89
  - test/handle_filter_test.rb
89
90
  - test/handle_helper_test.rb
91
+ - test/mutator_test.rb
90
92
  - test/test_helper.rb
91
- homepage: https://github.com/sonian/sensu-plugin
93
+ homepage: https://github.com/sensu-plugins/sensu-plugin
92
94
  licenses:
93
95
  - MIT
94
96
  metadata: {}
@@ -108,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
110
  version: '0'
109
111
  requirements: []
110
112
  rubyforge_project:
111
- rubygems_version: 2.2.2
113
+ rubygems_version: 2.6.3
112
114
  signing_key:
113
115
  specification_version: 4
114
116
  summary: Sensu Plugins
@@ -118,6 +120,7 @@ test_files:
118
120
  - test/external_handler_argument_test.rb
119
121
  - test/handle_filter_test.rb
120
122
  - test/external_handler_test.rb
123
+ - test/mutator_test.rb
121
124
  - test/external_check_test.rb
122
125
  - test/test_helper.rb
123
126
  has_rdoc: false