-
Notifications
You must be signed in to change notification settings - Fork 11
/
nrpe-runner
executable file
·95 lines (74 loc) · 2.57 KB
/
nrpe-runner
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
#!/usr/bin/ruby
require 'rubygems'
require 'optparse'
require 'json'
checks = {}
options = {
'basedir' => '/etc/nrpe.d',
'conf_ext' => '.cfg',
}
OptionParser.new do |opts|
opts.on("-a", "--all") { |v| options['all'] = v }
opts.on("-c", "--command COMMAND") { |command| options['command'] = command }
opts.on("-d", "--directory DIR") { |dir| options['basedir'] = dir }
opts.on("-j", "--json") { |v| options['json'] = v }
opts.on("-n", "--name NAME") { |name| options['name'] = name }
opts.on("-o", "--output-file FILENAME") { |filename| options['output'] = filename }
opts.on("-r", "--return RETCODE") { |retcode| options['retcode'] = retcode }
opts.on("-s", "--summary") { |v| options['summary'] = v }
end.parse!
# build up the list of checks. Checks that appear more than once
# overwrite the older instance
Dir["#{options['basedir']}/*#{options['conf_ext']}"].each do |file|
File.open( file, 'r' ).each_line do |line|
line.chomp!
next unless line.match(/^command\[(.+)\]=(.+)/)
name, command = $1, $2
checks[name] = command
end
end
# filter out the checks we want to run
#########################
# just in case nothing is filtered.
wanted = checks
if options['name']
wanted = checks.reject { |k,v| ! k.include? options['name'] }
end
if options['command']
wanted = checks.reject { |k,v| ! v.include? options['command'] }
end
if options['name'] && options['command']
wanted = checks.reject { |k,v| (! k.include? options['name']) || (! v.include? options['command']) }
end
details = { }
statuses = { '0' => 0, '1' => 0, '2' => 0, '3' => 0 }
ran = 0
# run the checks and do some output wrangling
#########################
wanted.each_pair do |name,command|
output = %x{ #{command} }.chomp!
ret = $?.to_i / 256 # magic number
if options['all']
details[name] = { 'command' => command, 'output' => output, 'returncode' => ret }
elsif options['retcode']
if options['retcode'].to_i == ret
details[name] = { 'command' => command, 'output' => output, 'returncode' => ret }
end
else # default to showing everything broken
if ret != 0
details[name] = { 'command' => command, 'output' => output, 'returncode' => ret }
end
end
statuses[ret.to_s] += 1
ran += 1
end
if options['json']
puts details.to_json
exit
end
details.keys.each do |name|
puts "#{name} => #{details[name]['output']}"
end
if options['summary']
puts "Ran #{ran} checks - OK #{statuses['0']}. WARN #{statuses['1']}, CRIT #{statuses['2']}, UNKNOWN #{statuses['3']}"
end