sensu-plugin 2.5.0 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sensu-handler.rb +0 -51
- data/lib/sensu-plugin.rb +1 -1
- data/lib/sensu-plugin/utils.rb +77 -1
- data/test/handle_api_request_test.rb +34 -0
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c15f38b4709b2ac89b59531eccfdc58d53ae6824
|
4
|
+
data.tar.gz: 5c165bc7759153d78948f0ee03093de67569cc41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e689ee660aeaab03c13780cbda36c65e834ebc83ddc0e6361cd04fb0baf998027a74b576a5739afc4c4537025efae80e6302aefc12c3bdb370b4b35bbb95c694
|
7
|
+
data.tar.gz: 9b78540d2de31488f61911fa144e81091f3eaf447b6377ca5a35d8c01a3410864aa28a86bc0688cad13f00d18224ae3bce0268eb64219768bd08426a6ccbcb4f
|
data/lib/sensu-handler.rb
CHANGED
@@ -99,57 +99,6 @@ module Sensu
|
|
99
99
|
exit 0
|
100
100
|
end
|
101
101
|
|
102
|
-
# Override API settings (for testing purposes)
|
103
|
-
#
|
104
|
-
# @param api_settings [Hash]
|
105
|
-
# @return [Hash]
|
106
|
-
def api_settings=(api_settings)
|
107
|
-
@api_settings = api_settings
|
108
|
-
end
|
109
|
-
|
110
|
-
# Return a hash of API settings derived first from ENV['SENSU_API_URL'] if set,
|
111
|
-
# then Sensu config `api` scope if configured, and finally falling back to
|
112
|
-
# to ipv4 localhost address on default API port.
|
113
|
-
#
|
114
|
-
# @return [Hash]
|
115
|
-
def api_settings
|
116
|
-
return @api_settings if @api_settings
|
117
|
-
if ENV['SENSU_API_URL']
|
118
|
-
uri = URI(ENV['SENSU_API_URL'])
|
119
|
-
ssl = uri.scheme == 'https' ? {} : nil
|
120
|
-
@api_settings = {
|
121
|
-
'ssl' => ssl,
|
122
|
-
'host' => uri.host,
|
123
|
-
'port' => uri.port,
|
124
|
-
'user' => uri.user,
|
125
|
-
'password' => uri.password
|
126
|
-
}
|
127
|
-
else
|
128
|
-
@api_settings = settings['api'] || {}
|
129
|
-
@api_settings['host'] ||= '127.0.0.1'
|
130
|
-
@api_settings['port'] ||= 4567
|
131
|
-
end
|
132
|
-
@api_settings
|
133
|
-
end
|
134
|
-
|
135
|
-
def api_request(method, path, &_blk)
|
136
|
-
if api_settings.nil?
|
137
|
-
raise 'api.json settings not found.'
|
138
|
-
end
|
139
|
-
use_ssl = api_settings['ssl'].is_a?(Hash) ||
|
140
|
-
api_settings['host'].start_with?('https')
|
141
|
-
hostname = api_settings['host'].gsub(/https?:\/\//, '')
|
142
|
-
req = net_http_req_class(method).new(path)
|
143
|
-
if api_settings['user'] && api_settings['password']
|
144
|
-
req.basic_auth(api_settings['user'], api_settings['password'])
|
145
|
-
end
|
146
|
-
yield(req) if block_given?
|
147
|
-
res = Net::HTTP.start(hostname, api_settings['port'], use_ssl: use_ssl) do |http|
|
148
|
-
http.request(req)
|
149
|
-
end
|
150
|
-
res
|
151
|
-
end
|
152
|
-
|
153
102
|
def filter_disabled
|
154
103
|
bail 'alert disabled' if @event['check']['alert'] == false
|
155
104
|
end
|
data/lib/sensu-plugin.rb
CHANGED
data/lib/sensu-plugin/utils.rb
CHANGED
@@ -2,7 +2,7 @@ require 'json'
|
|
2
2
|
|
3
3
|
module Sensu
|
4
4
|
module Plugin
|
5
|
-
module Utils
|
5
|
+
module Utils # rubocop:disable Metrics/ModuleLength
|
6
6
|
def config_files
|
7
7
|
if ENV['SENSU_LOADED_TEMPFILE'] && File.file?(ENV['SENSU_LOADED_TEMPFILE'])
|
8
8
|
IO.read(ENV['SENSU_LOADED_TEMPFILE']).split(':')
|
@@ -46,6 +46,82 @@ module Sensu
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
+
# Override API settings (for testing purposes)
|
50
|
+
#
|
51
|
+
# @param api_settings [Hash]
|
52
|
+
# @return [Hash]
|
53
|
+
def api_settings=(api_settings)
|
54
|
+
@api_settings = api_settings
|
55
|
+
end
|
56
|
+
|
57
|
+
# Return a hash of API settings derived first from ENV['SENSU_API_URL'] if set,
|
58
|
+
# then Sensu config `api` scope if configured, and finally falling back to
|
59
|
+
# to ipv4 localhost address on default API port.
|
60
|
+
#
|
61
|
+
# @return [Hash]
|
62
|
+
def api_settings
|
63
|
+
return @api_settings if @api_settings
|
64
|
+
if ENV['SENSU_API_URL']
|
65
|
+
uri = URI(ENV['SENSU_API_URL'])
|
66
|
+
ssl = uri.scheme == 'https' ? {} : nil
|
67
|
+
@api_settings = {
|
68
|
+
'ssl' => ssl,
|
69
|
+
'host' => uri.host,
|
70
|
+
'port' => uri.port,
|
71
|
+
'user' => uri.user,
|
72
|
+
'password' => uri.password
|
73
|
+
}
|
74
|
+
else
|
75
|
+
@api_settings = settings['api'] || {}
|
76
|
+
@api_settings['host'] ||= '127.0.0.1'
|
77
|
+
@api_settings['port'] ||= 4567
|
78
|
+
end
|
79
|
+
@api_settings
|
80
|
+
end
|
81
|
+
|
82
|
+
def api_request(method, path, &_blk)
|
83
|
+
if api_settings.nil?
|
84
|
+
raise 'api.json settings not found.'
|
85
|
+
end
|
86
|
+
use_ssl = api_settings['ssl'].is_a?(Hash) ||
|
87
|
+
api_settings['host'].start_with?('https')
|
88
|
+
hostname = api_settings['host'].gsub(/https?:\/\//, '')
|
89
|
+
req = net_http_req_class(method).new(path)
|
90
|
+
if api_settings['user'] && api_settings['password']
|
91
|
+
req.basic_auth(api_settings['user'], api_settings['password'])
|
92
|
+
end
|
93
|
+
yield(req) if block_given?
|
94
|
+
res = Net::HTTP.start(hostname, api_settings['port'], use_ssl: use_ssl) do |http|
|
95
|
+
http.request(req)
|
96
|
+
end
|
97
|
+
res
|
98
|
+
end
|
99
|
+
|
100
|
+
# Use API query parameters to paginate HTTP GET requests,
|
101
|
+
# iterating over the results until an empty set is returned.
|
102
|
+
#
|
103
|
+
# @param path [String]
|
104
|
+
# @param options [Hash]
|
105
|
+
# @return [Array]
|
106
|
+
|
107
|
+
def paginated_get(path, options = {})
|
108
|
+
limit = options.fetch('limit', 500)
|
109
|
+
offset = 0
|
110
|
+
results = []
|
111
|
+
loop do
|
112
|
+
query_path = "#{path}?limit=#{limit}&offset=#{offset}"
|
113
|
+
response = api_request(:GET, query_path)
|
114
|
+
unless response.is_a?(Net::HTTPOK)
|
115
|
+
unknown("Non-OK response from API query: #{get_uri(query_path)}")
|
116
|
+
end
|
117
|
+
data = JSON.parse(response.body)
|
118
|
+
break if data.empty?
|
119
|
+
results << data
|
120
|
+
offset += limit
|
121
|
+
end
|
122
|
+
results.flatten
|
123
|
+
end
|
124
|
+
|
49
125
|
def deep_merge(hash_one, hash_two)
|
50
126
|
merged = hash_one.dup
|
51
127
|
hash_two.each do |key, value|
|
@@ -7,6 +7,22 @@ class TestHandleAPIRequest < MiniTest::Test
|
|
7
7
|
|
8
8
|
Sensu::Handler.disable_autorun
|
9
9
|
|
10
|
+
def sample_check_result
|
11
|
+
{
|
12
|
+
client: 'sensu',
|
13
|
+
check: {
|
14
|
+
handler: 'keepalive',
|
15
|
+
name: 'keepalive',
|
16
|
+
issued: 1_534_373_016,
|
17
|
+
executed: 1_534_373_016,
|
18
|
+
output: 'Keepalive sent from client 4 seconds ago',
|
19
|
+
status: 0,
|
20
|
+
type: 'standard',
|
21
|
+
history: [0]
|
22
|
+
}
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
10
26
|
def test_http_request
|
11
27
|
stub_request(:get, 'http://127.0.0.1:4567/foo').to_return(status: 200, body: '', headers: {})
|
12
28
|
|
@@ -16,6 +32,24 @@ class TestHandleAPIRequest < MiniTest::Test
|
|
16
32
|
assert_equal(response.code, '200')
|
17
33
|
end
|
18
34
|
|
35
|
+
def test_http_paginated_get
|
36
|
+
stub_request(:get, 'http://127.0.0.1:4567/results?limit=1&offset=0')
|
37
|
+
.to_return(status: 200, headers: {}, body: JSON.dump([sample_check_result]))
|
38
|
+
|
39
|
+
stub_request(:get, 'http://127.0.0.1:4567/results?limit=1&offset=1')
|
40
|
+
.to_return(status: 200, headers: {}, body: JSON.dump([sample_check_result]))
|
41
|
+
|
42
|
+
stub_request(:get, 'http://127.0.0.1:4567/results?limit=1&offset=2')
|
43
|
+
.to_return(status: 200, headers: {}, body: JSON.dump([]))
|
44
|
+
|
45
|
+
handler = Sensu::Handler.new([])
|
46
|
+
response = handler.paginated_get('/results', 'limit' => 1)
|
47
|
+
|
48
|
+
# we expect the combined results to be an array containing two instances of the sample check result
|
49
|
+
combined_results = JSON.parse("[ #{JSON.dump(sample_check_result)} , #{JSON.dump(sample_check_result)} ]")
|
50
|
+
assert_equal(response, combined_results)
|
51
|
+
end
|
52
|
+
|
19
53
|
def test_https_request
|
20
54
|
stub_request(:get, 'https://127.0.0.1:4567/foo').to_return(status: 200, body: '', headers: {})
|
21
55
|
|
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: 2.
|
4
|
+
version: 2.6.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: 2018-
|
12
|
+
date: 2018-08-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
@@ -141,19 +141,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
141
|
version: '0'
|
142
142
|
requirements: []
|
143
143
|
rubyforge_project:
|
144
|
-
rubygems_version: 2.
|
144
|
+
rubygems_version: 2.6.11
|
145
145
|
signing_key:
|
146
146
|
specification_version: 4
|
147
147
|
summary: Sensu Plugins
|
148
148
|
test_files:
|
149
|
-
- test/handle_api_request_test.rb
|
150
|
-
- test/handle_helper_test.rb
|
151
|
-
- test/external_handler_test.rb
|
152
149
|
- test/external_metric_test.rb
|
153
|
-
- test/
|
150
|
+
- test/handle_api_request_test.rb
|
154
151
|
- test/external_check_test.rb
|
152
|
+
- test/test_helper.rb
|
153
|
+
- test/handle_filter_test.rb
|
154
|
+
- test/handle_helper_test.rb
|
155
155
|
- test/external_handler_argument_test.rb
|
156
|
-
- test/cast_bool_value_test.rb
|
157
156
|
- test/deep_merge_test.rb
|
157
|
+
- test/cast_bool_value_test.rb
|
158
|
+
- test/external_handler_test.rb
|
158
159
|
- test/mutator_test.rb
|
159
|
-
- test/test_helper.rb
|