sensu-plugins-mongodb-boutetnico 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 +4 -4
- data/README.md +1 -0
- data/bin/check-mongodb-query-count.rb +267 -0
- data/lib/sensu-plugins-mongodb/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5158f79cf0b2ca17989024073a6e6763c813a12
|
4
|
+
data.tar.gz: 33d481c535410f57d938cc3ed9a1d909978a7a56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7398a60f9c355056cc4904f5a723c5f27cf9d2e3293570bda98764acccbdfca8f63e04f04f78178dd5fd67c77b82ae3e8288612f64bc534b19cc7d18137f68d2
|
7
|
+
data.tar.gz: 5d13573db9f1ddc5f27e458c34fe14507a9ac7e25e73890a10e3ff1a9c5be0077aa1ae9acf32ed240aaf060c72cee0490c0801ff63b41a78b6c8b10da4435d2c
|
data/README.md
CHANGED
@@ -12,6 +12,7 @@ This fork is automatically tested, built and published to [RubyGems](https://rub
|
|
12
12
|
* bin/check-mongodb.py
|
13
13
|
* bin/check-mongodb.rb - wrapper for check-mongodb.py
|
14
14
|
* bin/check-mongodb-metric.rb
|
15
|
+
* bin/check-mongodb-query-count.rb
|
15
16
|
* bin/metrics-mongodb.rb
|
16
17
|
* bin/metrics-mongodb-replication.rb
|
17
18
|
|
@@ -0,0 +1,267 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# check-mongodb-query-count.rb
|
4
|
+
#
|
5
|
+
# DESCRIPTION:
|
6
|
+
# Check how many documents are returned by a MongoDB query.
|
7
|
+
#
|
8
|
+
# OUTPUT:
|
9
|
+
# Plain text
|
10
|
+
#
|
11
|
+
# PLATFORMS:
|
12
|
+
# Linux
|
13
|
+
#
|
14
|
+
# DEPENDENCIES:
|
15
|
+
# gem: sensu-plugin
|
16
|
+
# gem: mongo
|
17
|
+
# gem: bson
|
18
|
+
# gem: bson_ext
|
19
|
+
# gem: json
|
20
|
+
#
|
21
|
+
# USAGE:
|
22
|
+
# # Check MongoDB collection "logs" for critical events
|
23
|
+
# ./check-mongodb-query-count.rb --user sensu --pass sensu --database test --collection logs
|
24
|
+
# --query '{"level":"CRITICAL"}'
|
25
|
+
# --minutes-previous 5
|
26
|
+
# -w 0 -c 10 --include-results
|
27
|
+
#
|
28
|
+
# NOTES:
|
29
|
+
# Ruby is shit.
|
30
|
+
#
|
31
|
+
# LICENSE:
|
32
|
+
# Copyright 2019 github.com/boutetnico
|
33
|
+
# Released under the same terms as Sensu (the MIT license); see LICENSE
|
34
|
+
# for details.
|
35
|
+
#
|
36
|
+
|
37
|
+
require 'sensu-plugin/check/cli'
|
38
|
+
require 'mongo'
|
39
|
+
require 'json'
|
40
|
+
include Mongo
|
41
|
+
|
42
|
+
#
|
43
|
+
# Mongodb
|
44
|
+
#
|
45
|
+
|
46
|
+
class MongoDBQueryCount < Sensu::Plugin::Check::CLI
|
47
|
+
option :host,
|
48
|
+
description: 'MongoDB host',
|
49
|
+
long: '--host HOST',
|
50
|
+
default: 'localhost'
|
51
|
+
|
52
|
+
option :port,
|
53
|
+
description: 'MongoDB port',
|
54
|
+
long: '--port PORT',
|
55
|
+
default: 27_017
|
56
|
+
|
57
|
+
option :user,
|
58
|
+
description: 'MongoDB user',
|
59
|
+
long: '--user USER',
|
60
|
+
default: nil
|
61
|
+
|
62
|
+
option :password,
|
63
|
+
description: 'MongoDB password',
|
64
|
+
long: '--password PASSWORD',
|
65
|
+
default: nil
|
66
|
+
|
67
|
+
option :ssl,
|
68
|
+
description: 'Connect using SSL',
|
69
|
+
long: '--ssl',
|
70
|
+
default: false
|
71
|
+
|
72
|
+
option :ssl_cert,
|
73
|
+
description: 'The certificate file used to identify the local connection against mongod',
|
74
|
+
long: '--ssl-cert SSL_CERT',
|
75
|
+
default: ''
|
76
|
+
|
77
|
+
option :ssl_key,
|
78
|
+
description: 'The private key used to identify the local connection against mongod',
|
79
|
+
long: '--ssl-key SSL_KEY',
|
80
|
+
default: ''
|
81
|
+
|
82
|
+
option :ssl_ca_cert,
|
83
|
+
description: 'The set of concatenated CA certificates, which are used to validate certificates passed from the other end of the connection',
|
84
|
+
long: '--ssl-ca-cert SSL_CA_CERT',
|
85
|
+
default: ''
|
86
|
+
|
87
|
+
option :ssl_verify,
|
88
|
+
description: 'Whether or not to do peer certification validation',
|
89
|
+
long: '--ssl-verify',
|
90
|
+
default: false
|
91
|
+
|
92
|
+
option :debug,
|
93
|
+
description: 'Enable debug',
|
94
|
+
long: '--debug',
|
95
|
+
default: false
|
96
|
+
|
97
|
+
option :database,
|
98
|
+
description: 'Database to perform query on',
|
99
|
+
short: '-d DATABASE',
|
100
|
+
long: '--database DATABASE',
|
101
|
+
required: true
|
102
|
+
|
103
|
+
option :collection,
|
104
|
+
description: 'Collection to perform query on',
|
105
|
+
short: '-C COLLECTION',
|
106
|
+
long: '--collection COLLECTION',
|
107
|
+
required: true
|
108
|
+
|
109
|
+
option :query,
|
110
|
+
description: 'Query to perform',
|
111
|
+
short: '-q QUERY',
|
112
|
+
long: '--query QUERY',
|
113
|
+
required: true
|
114
|
+
|
115
|
+
option :warn,
|
116
|
+
short: '-w N',
|
117
|
+
long: '--warn N',
|
118
|
+
description: 'Result count WARNING threshold',
|
119
|
+
proc: proc(&:to_i),
|
120
|
+
default: 0
|
121
|
+
|
122
|
+
option :crit,
|
123
|
+
short: '-c N',
|
124
|
+
long: '--crit N',
|
125
|
+
description: 'Result count CRITICAL threshold',
|
126
|
+
proc: proc(&:to_i),
|
127
|
+
default: 0
|
128
|
+
|
129
|
+
option :invert,
|
130
|
+
long: '--invert',
|
131
|
+
description: 'Invert thresholds',
|
132
|
+
boolean: true
|
133
|
+
|
134
|
+
option :date_field,
|
135
|
+
description: 'Field to use instead of "date" for query.',
|
136
|
+
long: '--date-field FIELD_NAME',
|
137
|
+
default: 'date'
|
138
|
+
|
139
|
+
option :minutes_previous,
|
140
|
+
description: 'Minutes before offset to check date field against query.',
|
141
|
+
long: '--minutes-previous MINUTES_PREVIOUS',
|
142
|
+
proc: proc(&:to_i),
|
143
|
+
default: 0
|
144
|
+
|
145
|
+
option :hours_previous,
|
146
|
+
description: 'Hours before offset to check date field against query.',
|
147
|
+
long: '--hours-previous HOURS_PREVIOUS',
|
148
|
+
proc: proc(&:to_i),
|
149
|
+
default: 0
|
150
|
+
|
151
|
+
option :days_previous,
|
152
|
+
description: 'Days before offset to check date field against query.',
|
153
|
+
long: '--days-previous DAYS_PREVIOUS',
|
154
|
+
proc: proc(&:to_i),
|
155
|
+
default: 0
|
156
|
+
|
157
|
+
option :weeks_previous,
|
158
|
+
description: 'Weeks before offset to check date field against query.',
|
159
|
+
long: '--weeks-previous WEEKS_PREVIOUS',
|
160
|
+
proc: proc(&:to_i),
|
161
|
+
default: 0
|
162
|
+
|
163
|
+
option :months_previous,
|
164
|
+
description: 'Months before offset to check date field against query.',
|
165
|
+
long: '--months-previous MONTHS_PREVIOUS',
|
166
|
+
proc: proc(&:to_i),
|
167
|
+
default: 0
|
168
|
+
|
169
|
+
option :include_results,
|
170
|
+
long: '--include-results',
|
171
|
+
description: 'Include results in response',
|
172
|
+
boolean: false
|
173
|
+
|
174
|
+
def connect_mongo_db
|
175
|
+
address_str = "#{config[:host]}:#{config[:port]}"
|
176
|
+
client_opts = {}
|
177
|
+
client_opts[:database] = config[:database]
|
178
|
+
unless config[:user].nil?
|
179
|
+
client_opts[:user] = config[:user]
|
180
|
+
client_opts[:password] = config[:password]
|
181
|
+
end
|
182
|
+
if config[:ssl]
|
183
|
+
client_opts[:ssl] = true
|
184
|
+
client_opts[:ssl_cert] = config[:ssl_cert]
|
185
|
+
client_opts[:ssl_key] = config[:ssl_key]
|
186
|
+
client_opts[:ssl_ca_cert] = config[:ssl_ca_cert]
|
187
|
+
client_opts[:ssl_verify] = config[:ssl_verify]
|
188
|
+
end
|
189
|
+
mongo_client = Mongo::Client.new([address_str], client_opts)
|
190
|
+
@db = mongo_client.database
|
191
|
+
end
|
192
|
+
|
193
|
+
def query_mongo
|
194
|
+
collection = @db[config[:collection]]
|
195
|
+
begin
|
196
|
+
query = JSON.parse(config[:query])
|
197
|
+
rescue JSON::ParserError
|
198
|
+
unknown 'Failed to parse query. Provide a valid JSON array.'
|
199
|
+
end
|
200
|
+
|
201
|
+
start_time = Time.now.utc.to_i
|
202
|
+
if config[:minutes_previous] != 0
|
203
|
+
start_time -= (config[:minutes_previous] * 60)
|
204
|
+
end
|
205
|
+
if config[:hours_previous] != 0
|
206
|
+
start_time -= (config[:hours_previous] * 60 * 60)
|
207
|
+
end
|
208
|
+
if config[:days_previous] != 0
|
209
|
+
start_time -= (config[:days_previous] * 60 * 60 * 24)
|
210
|
+
end
|
211
|
+
if config[:weeks_previous] != 0
|
212
|
+
start_time -= (config[:weeks_previous] * 60 * 60 * 24 * 7)
|
213
|
+
end
|
214
|
+
if config[:months_previous] != 0
|
215
|
+
start_time -= (config[:months_previous] * 60 * 60 * 24 * 31)
|
216
|
+
end
|
217
|
+
|
218
|
+
query[config[:date_field]] = { '$gte' => Time.at(start_time).to_datetime }
|
219
|
+
|
220
|
+
if config[:debug]
|
221
|
+
puts 'Query: ' + query.inspect
|
222
|
+
end
|
223
|
+
|
224
|
+
collection.find(query)
|
225
|
+
end
|
226
|
+
|
227
|
+
def print_results(results)
|
228
|
+
count = results.count
|
229
|
+
|
230
|
+
if config[:include_results]
|
231
|
+
results.each { |document| puts document.inspect }
|
232
|
+
end
|
233
|
+
|
234
|
+
if config[:invert]
|
235
|
+
if count < config[:crit]
|
236
|
+
critical "Query count (#{count}) was below critical threshold."
|
237
|
+
elsif count < config[:warn]
|
238
|
+
warning "Query count (#{count}) was below warning threshold."
|
239
|
+
else
|
240
|
+
ok "Query count (#{count}) was ok"
|
241
|
+
end
|
242
|
+
elsif count > config[:crit]
|
243
|
+
critical "Query count (#{count}) was above critical threshold."
|
244
|
+
elsif count > config[:warn]
|
245
|
+
warning "Query count (#{count}) was above warning threshold."
|
246
|
+
else
|
247
|
+
ok "Query count (#{count}) was ok"
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def run
|
252
|
+
Mongo::Logger.logger.level = Logger::FATAL
|
253
|
+
@debug = config[:debug]
|
254
|
+
if @debug
|
255
|
+
Mongo::Logger.logger.level = Logger::DEBUG
|
256
|
+
config_debug = config.clone
|
257
|
+
config_debug[:password] = '***'
|
258
|
+
puts 'Arguments: ' + config_debug.inspect
|
259
|
+
end
|
260
|
+
|
261
|
+
connect_mongo_db
|
262
|
+
|
263
|
+
results = query_mongo
|
264
|
+
|
265
|
+
print_results(results)
|
266
|
+
end
|
267
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sensu-plugins-mongodb-boutetnico
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sensu-Plugins and contributors
|
@@ -188,6 +188,7 @@ description: |-
|
|
188
188
|
email: "<sensu-users@googlegroups.com>"
|
189
189
|
executables:
|
190
190
|
- check-mongodb.rb
|
191
|
+
- check-mongodb-query-count.rb
|
191
192
|
- metrics-mongodb.rb
|
192
193
|
- check-mongodb-metric.rb
|
193
194
|
- metrics-mongodb-replication.rb
|
@@ -198,6 +199,7 @@ files:
|
|
198
199
|
- LICENSE
|
199
200
|
- README.md
|
200
201
|
- bin/check-mongodb-metric.rb
|
202
|
+
- bin/check-mongodb-query-count.rb
|
201
203
|
- bin/check-mongodb.py
|
202
204
|
- bin/check-mongodb.rb
|
203
205
|
- bin/metrics-mongodb-replication.rb
|