-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_fstab
executable file
·371 lines (325 loc) · 10.6 KB
/
check_fstab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/ruby -w
# This script should retrieve the fstab from a server via nrpe.
# Then it creates a check_multi configfile for those disks
#
# Usage:
# -h this help
# -H Host
# -w default warning level in percent
# -c default warning level in percent
# -C SNMPv2 Community to use for checking
# --nossl Don't use ssl for NRPE Connections
# --nocache Don't use the cachefile
# --debug Turn on debugging
#
require "pp"
require "getoptlong"
require "rdoc/usage"
require "logger"
require "ipaddr"
require "resolv"
require "yaml"
require "fileutils"
require 'timeout'
# initialize logging engine
$log = Logger.new(STDERR)
################################## Configuration ##################################
# Default warning levels at which disks shall be alerted. Can be overriden on the command line
$warning = "10%"
$critical = "5%"
# Where will we find check_multi and check_nrpe?
$plugin_path = "/usr/lib/nagios/plugins"
workdir = "/var/local/check_fstab"
# Check_multi configs will be put here
$config_path = "#{workdir}/config"
# YAML Cache path
$cache_path = "#{workdir}/cache"
# polling intervall in seconds e.g. how often will we retrieve updates (default: 60*60 = 1 hour)
$polling_intervall = 60*60
# Set the Time the plugin should take at maximum (default: 10 seconds)
$global_timeout = 10
### snmp configuration ###
$snmpcommunity = ""
# enable HTML output?
$html = true
################################## dont touch this ##################################
$status = 0
# DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
$log.level = Logger::FATAL
$output = "ERROR: Unexplained errors happen all the time."
$nocache = false
$ssl = ""
# Injects a "-E" into the critical var to circumvent a bug in NRPE.cfg
$isbuggy = true
$multi_opts = "-r 15 -s collapse=0" if $html == true
$multi_opts = "-r 12" if $html == false
################################## Classes - no need to touch them ##################################
class Disks
def initialize(ip)
begin
@host = ip if IPAddr.new(ip).ipv4?
rescue
@host = Resolv.getaddress(ip)
# catch remaining errors
# TODO: Resolv does not honor "localhost"
error "Error in name resolution for #{@host}" unless IPAddr.new(@host).ipv4?
end
@olddisks = nil
@disks = [{}]
# go into main loop
intelligence
end
def intelligence
unless read_disk_cache
$log.debug "yikes. something went wrong while reading cache file. get fresh data."
retrieve_data_from_host
end
# TODO: If timelimit is reached and nocache = true disks are pulled twice
if $nocache == true
retrieve_data_from_host
end
# skip if we have manual changes
if @disks[0][:changes] == false
# TODO: if the last retrieval of disks was empty this one fails.
# is data current?
difference = Time.now - @disks[1][:last_seen]
$log.debug "Last retrieval of disks: #{difference}"
if difference >= $polling_intervall
$log.info "ohnoes. Data is more than #{$polling_intervall} seconds old. Retrieving update."
retrieve_data_from_host
end
end
# ok by now we should have @disks initialized. just to be sure.
error "Sumthing happened while initializing @disks. Plz check." unless @disks
# did we have changes?
# if we had changes we must recreate the check_multi config
if check_for_changes
create_multi_config
end
# call check_multi
$output = call_check_multi
end
# Calls NRPE and fills @disks with teh list of mounted disks
def retrieve_data_from_host
disks = nrpe_request
return false if disks == false
tmpdisks = [{:changes => false, :provider => @disks[0][:provider]}]
disks.each do |disk|
tmpdisks << {
:disk => disk,
:warn => nil,
:crit => nil,
:last_seen => Time.now()
}
end
if @disks
$log.info "updating disk cache with new data"
update_disks(tmpdisks)
elsif
$log.info "Had no @disks defined. writing new default file"
@disks = tmpdisks
end
write_disk_cache
create_multi_config
end
def nrpe_request
@nrpe_result = false
return @nrpe_result if @nrpe_result = __nrpe_fstab
self.methods.select {|x| x=~ /__nrpe_.*/}.each do |x|
$log.debug "Trying #{x} for nrpe data"
@nrpe_result = send(x.to_sym)
@disks[0][:provider] = x.to_s
break unless @nrpe_result == false
end
if @nrpe_result == false
$log.error "Failed to update via nrpe, no methods did succeed."
return false
end
return @nrpe_result
end
# try to get currently mounted partitions via check_fstab
def __nrpe_fstab
data = call_nrpe "check_fstab"
return false if data == false
disks = data.chomp.split("|").map! {|x| x.strip }
disks.delete("$") if disks.include?("$")
return disks
end
# try to get data from the check_alldisks command.
# This one delivers the data from /etc/fstab
def __nrpe_alldisks
data = call_nrpe "check_alldisks"
return false if data == false
disks = data.chomp.split("|")[1].split.map! {|x| x = x.split("=")[0] }
return disks
end
# this one requires the snmp gem so we need to test for it
# TODO: If we pull by SNMP, why do we check by NRPE?
def __nrpe_snmp
begin
gem "snmp"
rescue Gem::LoadError
$log.debug "No snmp library. skipping."
return false
end
require "snmp"
disks = []
mount_oid = SNMP::ObjectId.new("1.3.6.1.2.1.25.3.8.1.2")
begin
SNMP::Manager.open(:Host => @host, :Community => $snmpcommunity) do |manager|
manager.walk(mount_oid) do |row|
row.each { |vb| disks << vb.value.to_s }
end
end
rescue SNMP::RequestTimeout
return false
end
return disks
end
def call_nrpe(command)
data = `#{$plugin_path}/check_nrpe -H #{@host} #{$ssl}-c #{command}`
return false unless nrpe_errors?($?,data)
return data
end
# Error handling for nrpe
def nrpe_errors?(retcode,data)
$log.debug "check_nrpe output: #{data.chomp}"
unless retcode == 0
$log.warn "Error in NRPE data retrieval. Output was: #{data}"
return false
end
return true
end
# writes the disk cache file
def write_disk_cache
if File.exists?("#{$cache_path}/#{@host}_disks.yaml")
$log.info "Doing backup of cachefile to #{@host}_disks.yaml"
FileUtils.mv("#{$cache_path}/#{@host}_disks.yaml","#{$cache_path}/#{@host}_disks_backup.yaml")
@olddisks = YAML::load(File.read("#{$cache_path}/#{@host}_disks_backup.yaml"))
end
File.open( "#{$cache_path}/#{@host}_disks.yaml", 'w' ) do |out|
YAML.dump(@disks,out)
end
$log.info "Disk cache written for #{@host}"
end
# reads the disk cache file
def read_disk_cache
if File.exists?("#{$cache_path}/#{@host}_disks.yaml")
@disks = YAML::load(File.read("#{$cache_path}/#{@host}_disks.yaml"))
$log.info "Loaded cachefile for #{@host}."
else
$log.warn "No cachefile found. Need to get a new one."
return false
end
end
# Creates a check_multi compatible configfile
def create_multi_config
File.open( "#{$config_path}/#{@host}_disks.cfg", 'w' ) do |out|
out.puts "# Diskfile for host #{@host} generated at #{Time.now}"
@disks.each do |disk|
next unless disk[:disk]
warn = disk[:warn]
crit = disk[:crit]
warn = "$WARN$" if disk[:warn].nil?
crit = "$CRIT$" if disk[:crit].nil?
out.puts "command[#{disk[:disk].tr("/","_")}::check_disk]=check_nrpe -H #{@host} #{$ssl} -c check_disk -a #{warn} '#{crit} -E' #{disk[:disk]}" if $isbuggy == true
out.puts "command[#{disk[:disk].tr("/","_")}::check_disk]=check_nrpe -H #{@host} #{$ssl} -c check_disk -a #{warn} #{crit} #{disk[:disk]}" if $isbuggy == false
end
end
$log.info "check_multi config written for host #{@host}"
end
# executes check_multi for the given host
def call_check_multi
$log.debug "calling: #{$plugin_path}/check_multi -s libexec=#{$plugin_path} -s WARN=#{$warning} -s CRIT=#{$critical} #{$multi_opts} -t 20 -f #{$config_path}/#{@host}_disks.cfg"
data = `#{$plugin_path}/check_multi -s libexec=#{$plugin_path} -s WARN=#{$warning} -s CRIT=#{$critical} #{$multi_opts} -t 20 -f #{$config_path}/#{@host}_disks.cfg`
$status = $? >> 8
$log.debug "return code of check_multi was #{$status}"
return data.gsub("\n", "<br />") if $html == true
return data if $html == false
end
# does the data differ from what we already know?
def check_for_changes
# Check on disk changes
unless @disks[0][:changes] == false
$log.info "Cachefile changed by user. Honoring changes."
@disks[0][:changes] = false
write_disk_cache
return true
end
return false
end
def update_disks(disks)
unless @disks.nil?
@disks.each do |olddisk|
found = false
disks.each_with_index do |disk,i|
next unless disk[:disk] == olddisk[:disk]
disk[:warn] = olddisk[:warn]
disk[:crit] = olddisk[:crit]
found = true
break
end
next if found == true
# hmm. disk has vanished. add it to new array.
disks << olddisk
end
end
@disks = disks
end
def error(message)
$log.error "#{message}"
puts message
exit 3;
end
end
################################# Main Program ##################################
# parse options
opts = GetoptLong.new(
["--host", "-H", GetoptLong::REQUIRED_ARGUMENT],
["--warning", "-w", GetoptLong::OPTIONAL_ARGUMENT],
["--critical", "-c", GetoptLong::OPTIONAL_ARGUMENT],
["--community", "-C", GetoptLong::OPTIONAL_ARGUMENT],
["--nocache", "-n", GetoptLong::NO_ARGUMENT],
["--nossl", "-s", GetoptLong::NO_ARGUMENT],
["--debug", "-d", GetoptLong::NO_ARGUMENT],
["--help", "-h", GetoptLong::NO_ARGUMENT ]
)
host = "unknown"
opts.each do |opt,arg|
case opt
when "--host"
host = arg
when "--warning"
$warning = arg
when "--critical"
$critical = arg
when "--nossl"
$ssl = " -n "
when "--nocache"
$nocache = true
when "--community"
$snmpcommunity = arg
when "--debug"
$log.level = Logger::DEBUG
when "--help"
RDoc::usage
end
end
if host == "unknown"
puts "No host given"
RDoc::usage
exit 3;
end
$log.info "Warning levels at #{$warning}"
$log.info "Critical levels at #{$critical}"
begin
# Best practice suggest not to take more than 10 seconds to run
Timeout::timeout($global_timeout) {
disks = Disks.new(host)
}
rescue Timeout::Error
puts "Timeout while getting data from #{host}"
exit 3
end
puts $output
exit $status