sensu-plugin 0.0.3 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sensu-handler.rb +68 -18
- data/lib/sensu-plugin.rb +1 -1
- data/test/{check.rb → external_check_test.rb} +13 -7
- data/test/external_handler_test.rb +37 -0
- metadata +10 -8
data/lib/sensu-handler.rb
CHANGED
@@ -1,29 +1,22 @@
|
|
1
|
+
require 'net/http'
|
1
2
|
require 'json'
|
2
|
-
require 'sensu/config'
|
3
3
|
|
4
4
|
module Sensu
|
5
5
|
class Handler
|
6
6
|
|
7
7
|
# Implementing classes should override this.
|
8
8
|
|
9
|
-
def handle
|
9
|
+
def handle
|
10
10
|
puts 'ignoring event -- no handler defined'
|
11
11
|
end
|
12
12
|
|
13
|
-
#
|
14
|
-
#
|
13
|
+
# Filters exit the proccess if the event should not be handled.
|
14
|
+
# Implementation of the default filters is below.
|
15
15
|
|
16
|
-
def filter
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
refresh = (60.fdiv(event['check']['interval']) * 30).to_i
|
22
|
-
event['occurrences'] == 1 || event['occurrences'] % refresh == 0
|
23
|
-
end
|
24
|
-
|
25
|
-
def short_name(event)
|
26
|
-
event['client']['name'] + '/' + event['check']['name']
|
16
|
+
def filter
|
17
|
+
filter_disabled
|
18
|
+
filter_repeated
|
19
|
+
filter_silenced
|
27
20
|
end
|
28
21
|
|
29
22
|
# This works just like Plugin::CLI's autorun.
|
@@ -50,11 +43,68 @@ module Sensu
|
|
50
43
|
@settings ||= CONFIGS.map {|f| load_config(f) }.reduce {|a, b| a.deep_merge(b) }
|
51
44
|
end
|
52
45
|
|
46
|
+
def read_event(file)
|
47
|
+
begin
|
48
|
+
@event = ::JSON.parse(file.read)
|
49
|
+
@event['occurrences'] ||= 1
|
50
|
+
@event['check'] ||= Hash.new
|
51
|
+
@event['client'] ||= Hash.new
|
52
|
+
rescue
|
53
|
+
puts 'Error reading event'
|
54
|
+
exit 0
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
53
58
|
at_exit do
|
54
59
|
handler = @@autorun.new
|
55
|
-
|
56
|
-
|
57
|
-
|
60
|
+
handler.read_event(STDIN)
|
61
|
+
handler.filter
|
62
|
+
handler.handle
|
63
|
+
end
|
64
|
+
|
65
|
+
# Helpers and filters
|
66
|
+
|
67
|
+
def bail(msg)
|
68
|
+
puts msg + ': ' + @event['client']['name'] + '/' + @event['check']['name']
|
69
|
+
exit 0
|
70
|
+
end
|
71
|
+
|
72
|
+
def api_request(*path)
|
73
|
+
http = Net::HTTP.new(settings['api']['host'], settings['api']['port'])
|
74
|
+
http.request(Net::HTTP::Get.new(path.join('/')))
|
75
|
+
end
|
76
|
+
|
77
|
+
def filter_disabled
|
78
|
+
if @event['check']['alert'] == false
|
79
|
+
bail 'alert disabled'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def filter_repeated
|
84
|
+
occurrences = @event['check']['occurrences'] || 1
|
85
|
+
interval = @event['check']['interval'] || 30
|
86
|
+
refresh = @event['check']['refresh'] || 1800
|
87
|
+
if @event['occurrences'] < occurrences
|
88
|
+
bail 'not enough occurrences'
|
89
|
+
end
|
90
|
+
if @event['occurrences'] > occurrences
|
91
|
+
n = refresh.fdiv(interval).to_i
|
92
|
+
bail 'only repeating alert every' + n + 'occurrences' unless @event['occurrences'] % n == 0
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def filter_silenced
|
97
|
+
begin
|
98
|
+
timeout(3) do
|
99
|
+
if api_request('/stash/silence', @event['client']['name']).code == '200'
|
100
|
+
bail 'client alerts silenced'
|
101
|
+
end
|
102
|
+
if api_request('/stash/silence', @event['client']['name'], @event['check']['name']).code == '200'
|
103
|
+
bail 'check alerts silenced'
|
104
|
+
end
|
105
|
+
end
|
106
|
+
rescue Timeout::Error
|
107
|
+
puts 'Timed out while attempting to query the Sensu API for stashes'
|
58
108
|
end
|
59
109
|
end
|
60
110
|
|
data/lib/sensu-plugin.rb
CHANGED
@@ -3,34 +3,40 @@
|
|
3
3
|
require 'rubygems'
|
4
4
|
require 'minitest/autorun'
|
5
5
|
|
6
|
-
class
|
6
|
+
class TestCheckExternal < MiniTest::Unit::TestCase
|
7
7
|
|
8
8
|
def setup
|
9
|
-
@script = File.join(File.dirname(__FILE__), '
|
9
|
+
@script = File.join(File.dirname(__FILE__), 'external/check-options')
|
10
|
+
end
|
11
|
+
|
12
|
+
def run_script(*args)
|
13
|
+
IO.popen([@script] + args, 'r+') do |child|
|
14
|
+
child.read
|
15
|
+
end
|
10
16
|
end
|
11
17
|
|
12
18
|
def test_ok
|
13
|
-
|
19
|
+
run_script '-o'
|
14
20
|
assert $?.exitstatus == 0
|
15
21
|
end
|
16
22
|
|
17
23
|
def test_warning
|
18
|
-
|
24
|
+
run_script '-w'
|
19
25
|
assert $?.exitstatus == 1
|
20
26
|
end
|
21
27
|
|
22
28
|
def test_critical
|
23
|
-
|
29
|
+
run_script '-c'
|
24
30
|
assert $?.exitstatus == 2
|
25
31
|
end
|
26
32
|
|
27
33
|
def test_unknown
|
28
|
-
|
34
|
+
run_script '-u'
|
29
35
|
assert $?.exitstatus == 3
|
30
36
|
end
|
31
37
|
|
32
38
|
def test_fallthrough
|
33
|
-
|
39
|
+
run_script
|
34
40
|
assert $?.exitstatus == 1
|
35
41
|
end
|
36
42
|
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
class TestHandlerExternal < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@script = File.join(File.dirname(__FILE__), 'external/handle-nofilter')
|
11
|
+
end
|
12
|
+
|
13
|
+
def run_script(event)
|
14
|
+
output = IO.popen(@script, 'r+') do |child|
|
15
|
+
child.puts JSON.generate(event)
|
16
|
+
child.close_write
|
17
|
+
child.read
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_handled
|
22
|
+
event = {
|
23
|
+
'client' => { 'name' => 'test' },
|
24
|
+
'check' => { 'name' => 'test' },
|
25
|
+
'occurrences' => 1,
|
26
|
+
}
|
27
|
+
output = run_script(event)
|
28
|
+
assert $?.exitstatus == 0 && output =~ /Event:.*test/
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_missing_keys
|
32
|
+
event = {}
|
33
|
+
output = run_script(event)
|
34
|
+
assert $?.exitstatus == 0 && output =~ /Event:/
|
35
|
+
end
|
36
|
+
|
37
|
+
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: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: &
|
16
|
+
requirement: &70106722973320 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70106722973320
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: mixlib-cli
|
27
|
-
requirement: &
|
27
|
+
requirement: &70106722972800 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: 1.1.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70106722972800
|
36
36
|
description: Plugins and helper libraries for Sensu, a monitoring framework
|
37
37
|
email:
|
38
38
|
- decklin@red-bean.com
|
@@ -46,7 +46,8 @@ files:
|
|
46
46
|
- lib/sensu-plugin/metric/cli.rb
|
47
47
|
- lib/sensu-plugin/util/procs.rb
|
48
48
|
- lib/sensu-plugin.rb
|
49
|
-
- test/
|
49
|
+
- test/external_check_test.rb
|
50
|
+
- test/external_handler_test.rb
|
50
51
|
homepage: https://github.com/sonian/sensu-plugin
|
51
52
|
licenses:
|
52
53
|
- MIT
|
@@ -73,4 +74,5 @@ signing_key:
|
|
73
74
|
specification_version: 3
|
74
75
|
summary: Sensu Plugins
|
75
76
|
test_files:
|
76
|
-
- test/
|
77
|
+
- test/external_check_test.rb
|
78
|
+
- test/external_handler_test.rb
|