sensu-plugin 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sensu-handler.rb +3 -53
- data/lib/sensu-plugin.rb +1 -1
- data/lib/sensu-plugin/utils.rb +64 -0
- metadata +8 -7
data/lib/sensu-handler.rb
CHANGED
@@ -1,16 +1,11 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'json'
|
3
|
+
require 'sensu-plugin/utils'
|
3
4
|
|
4
5
|
module Sensu
|
5
6
|
|
6
|
-
NET_HTTP_REQ_CLASS = {
|
7
|
-
'GET' => Net::HTTP::Get,
|
8
|
-
'POST' => Net::HTTP::Post,
|
9
|
-
'DELETE' => Net::HTTP::Delete,
|
10
|
-
'PUT' => Net::HTTP::Put
|
11
|
-
}
|
12
|
-
|
13
7
|
class Handler
|
8
|
+
include Sensu::Plugin::Utils
|
14
9
|
|
15
10
|
# Implementing classes should override this.
|
16
11
|
|
@@ -39,34 +34,6 @@ module Sensu
|
|
39
34
|
end
|
40
35
|
end
|
41
36
|
|
42
|
-
def config_files
|
43
|
-
if ENV['SENSU_CONFIG_FILES']
|
44
|
-
ENV['SENSU_CONFIG_FILES'].split(':')
|
45
|
-
else
|
46
|
-
['/etc/sensu/config.json'] + Dir['/etc/sensu/conf.d/*.json']
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def load_config(filename)
|
51
|
-
JSON.parse(File.open(filename, 'r').read) rescue Hash.new
|
52
|
-
end
|
53
|
-
|
54
|
-
def settings
|
55
|
-
@settings ||= config_files.map {|f| load_config(f) }.reduce {|a, b| a.deep_merge(b) }
|
56
|
-
end
|
57
|
-
|
58
|
-
def read_event(file)
|
59
|
-
begin
|
60
|
-
@event = ::JSON.parse(file.read)
|
61
|
-
@event['occurrences'] ||= 1
|
62
|
-
@event['check'] ||= Hash.new
|
63
|
-
@event['client'] ||= Hash.new
|
64
|
-
rescue => error
|
65
|
-
puts 'error reading event: ' + error.message
|
66
|
-
exit 0
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
37
|
at_exit do
|
71
38
|
handler = @@autorun.new
|
72
39
|
handler.read_event(STDIN)
|
@@ -83,7 +50,7 @@ module Sensu
|
|
83
50
|
|
84
51
|
def api_request(method, path, &blk)
|
85
52
|
http = Net::HTTP.new(settings['api']['host'], settings['api']['port'])
|
86
|
-
req =
|
53
|
+
req = net_http_req_class(method).new(path)
|
87
54
|
if settings['api']['user'] && settings['api']['password']
|
88
55
|
req.basic_auth(settings['api']['user'], settings['api']['password'])
|
89
56
|
end
|
@@ -159,20 +126,3 @@ module Sensu
|
|
159
126
|
end
|
160
127
|
|
161
128
|
end
|
162
|
-
|
163
|
-
# Monkey Patching.
|
164
|
-
|
165
|
-
class Array
|
166
|
-
def deep_merge(other_array, &merger)
|
167
|
-
concat(other_array).uniq
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
class Hash
|
172
|
-
def deep_merge(other_hash, &merger)
|
173
|
-
merger ||= proc do |key, old_value, new_value|
|
174
|
-
old_value.deep_merge(new_value, &merger) rescue new_value
|
175
|
-
end
|
176
|
-
merge(other_hash, &merger)
|
177
|
-
end
|
178
|
-
end
|
data/lib/sensu-plugin.rb
CHANGED
@@ -0,0 +1,64 @@
|
|
1
|
+
module Sensu
|
2
|
+
module Plugin
|
3
|
+
module Utils
|
4
|
+
|
5
|
+
def config_files
|
6
|
+
if ENV['SENSU_CONFIG_FILES']
|
7
|
+
ENV['SENSU_CONFIG_FILES'].split(':')
|
8
|
+
else
|
9
|
+
['/etc/sensu/config.json'] + Dir['/etc/sensu/conf.d/**/*.json']
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def load_config(filename)
|
14
|
+
JSON.parse(File.open(filename, 'r').read) rescue Hash.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def settings
|
18
|
+
@settings ||= config_files.map {|f| load_config(f) }.reduce {|a, b| a.deep_merge(b) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def read_event(file)
|
22
|
+
begin
|
23
|
+
@event = ::JSON.parse(file.read)
|
24
|
+
@event['occurrences'] ||= 1
|
25
|
+
@event['check'] ||= Hash.new
|
26
|
+
@event['client'] ||= Hash.new
|
27
|
+
rescue => e
|
28
|
+
puts 'error reading event: ' + e.message
|
29
|
+
exit 0
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def net_http_req_class(method)
|
34
|
+
case method.to_s.upcase
|
35
|
+
when 'GET'
|
36
|
+
Net::HTTP::Get
|
37
|
+
when 'POST'
|
38
|
+
Net::HTTP::Post
|
39
|
+
when 'DELETE'
|
40
|
+
Net::HTTP::Delete
|
41
|
+
when 'PUT'
|
42
|
+
Net::HTTP::Put
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Monkey Patching.
|
50
|
+
|
51
|
+
class Array
|
52
|
+
def deep_merge(other_array, &merger)
|
53
|
+
concat(other_array).uniq
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class Hash
|
58
|
+
def deep_merge(other_hash, &merger)
|
59
|
+
merger ||= proc do |key, old_value, new_value|
|
60
|
+
old_value.deep_merge(new_value, &merger) rescue new_value
|
61
|
+
end
|
62
|
+
merge(other_hash, &merger)
|
63
|
+
end
|
64
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sensu-plugin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Decklin Foster
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2012-
|
19
|
+
date: 2012-11-14 00:00:00 -08:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -61,14 +61,15 @@ extra_rdoc_files: []
|
|
61
61
|
|
62
62
|
files:
|
63
63
|
- lib/sensu-handler.rb
|
64
|
+
- lib/sensu-plugin.rb
|
64
65
|
- lib/sensu-plugin/check/cli.rb
|
65
66
|
- lib/sensu-plugin/cli.rb
|
66
67
|
- lib/sensu-plugin/metric/cli.rb
|
67
|
-
- lib/sensu-plugin.rb
|
68
|
+
- lib/sensu-plugin/utils.rb
|
68
69
|
- test/external_check_test.rb
|
69
70
|
- test/external_handler_test.rb
|
70
|
-
- test/external_metric_test.rb
|
71
71
|
- test/handle_filter_test.rb
|
72
|
+
- test/external_metric_test.rb
|
72
73
|
- test/helper.rb
|
73
74
|
has_rdoc: true
|
74
75
|
homepage: https://github.com/sonian/sensu-plugin
|
@@ -107,6 +108,6 @@ summary: Sensu Plugins
|
|
107
108
|
test_files:
|
108
109
|
- test/external_check_test.rb
|
109
110
|
- test/external_handler_test.rb
|
110
|
-
- test/external_metric_test.rb
|
111
111
|
- test/handle_filter_test.rb
|
112
|
+
- test/external_metric_test.rb
|
112
113
|
- test/helper.rb
|