forked from jrottenberg/Nagios-Munin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_munin_rrd.pl
executable file
·354 lines (302 loc) · 9.96 KB
/
check_munin_rrd.pl
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
#!/usr/bin/perl -w
# $Id: check_munin_rrd.pl
# 2007/05/20 01:40:47
#
# check_munin_rrd.pl Copyright (C) 2007 Julien Rottenberg <julien@rottenberg.info>
#
# check_munin_rrd.pl can check various modules via rrd objects.
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# you should have received a copy of the GNU General Public License
# along with this program (or with Nagios); if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA
# Globals
my $PROGNAME = "check_munin_rrd.pl";
use POSIX qw(strftime);
use RRDs;
use Getopt::Long;
use File::Find ();
use vars qw($opt_V $opt_v $opt_h $opt_w $opt_c $opt_h $opt_M $opt_S $opt_d $opt_H $opt_D $PROGNAME);
use lib "/usr/lib64/nagios/plugins" ;
use utils qw(%ERRORS &print_revision &support &usage);
# Munin specific
my $datadir = "/var/lib/munin";
my $rrdpath = undef;
my $cf = "AVERAGE"; # munin stores its data in this CF for the latest.
# check_munin_rrd specific
my $DEBUG = 0;
my $REVISION = "1.1";
my $hostname = undef;
my $domain = undef;
my $module = undef;
my $submodule = undef;
# nagios specific
my $status = '0';
my $problem_on_name = undef;
my $problem_value = undef;
sub in ($$);
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';
$ENV{'PATH'}='';
$ENV{'LC_ALL'}='C';
Getopt::Long::Configure('bundling');
GetOptions
("V" => \$opt_V, "version" => \$opt_V,
"h" => \$opt_h, "help" => \$opt_h,
"v" => \$opt_v, "verbose" => \$opt_v,
"w=f" => \$opt_w, "warning=f" => \$opt_w,
"c=f" => \$opt_c, "critical=f" => \$opt_c,
"d=s" => \$opt_d, "domain=s" => \$opt_d,
"D=s" => \$opt_D, "datadir=s" => \$opt_D,
"M=s" => \$opt_M, "module=s" => \$opt_M,
"S=s" => \$opt_S, "submodule=s" => \$opt_S,
"H=s" => \$opt_H, "hostname=s" => \$opt_H);
# check if everything is ok
check_parameters();
## Open suggested directory
if (-d $datadir."/".$domain) {
$rrdpath = $datadir."/".$domain;
printf "rrdpath : $rrdpath\n" if $DEBUG;
} else {
printf ("No such directory $datadir/$domain\n");
exit $ERRORS{"CRITICAL"};
}
my $response_text = '';
print "Opening $rrdpath/$hostname-$module-*-g.rrd\n" if $DEBUG;
my @rrd = <$rrdpath/$hostname-$module-*.rrd>;
printf ($#rrd+1 ." rrd(s) found\n") if $DEBUG;
# we didn't find any rrd there...
if ($#rrd < 0) {
printf("\nSearch for $hostname-$module-*.rrd on $datadir\n") if $DEBUG;
# Let's try a search before to fail
sub wanted {
if ( $File::Find::name =~ "$hostname-$module-([-_0-9a-z]*)\.rrd" ) {
printf("FOUND : $File::Find::name\n") if $DEBUG;
push (@rrd, $File::Find::name);
}
}
File::Find::find(\&wanted, $datadir);
if ($#rrd < 0) {
printf ("No such files $rrdpath/$hostname-$module-*.rrd Are you sure the domain defined in munin is correct ?\n");
exit $ERRORS{"CRITICAL"};
}
}
printf ($#rrd+1 ." rrd(s) found\n") if $DEBUG;
foreach (@rrd) {
my $current_rrd = $_;
print "\nDoing : $current_rrd\n" if $DEBUG;
if ($current_rrd =~ /$hostname-$module-(\w+)-[a-z]\.rrd$/im) {
my $component = sanitize($1); # Let's have a nicer output,
#some lines from Munin are not useful var_run for module df for example
printf($component) if $DEBUG;
if ($component ne "") {
my $mtime = (stat( $current_rrd ))[9];
printf $mtime if $DEBUG;
my $now_string = time;
printf "\n$now_string \n" if $DEBUG;
my $seconds_diff = $now_string - $mtime;
if ($seconds_diff > 600) {
my $formated_mtime = strftime "%d-%b-%Y %H:%M:%S %Z", localtime($mtime);
print "Problem on $current_rrd : data are too old, $formated_mtime\n" if $DEBUG;
exit $ERRORS{"UNKNOWN"} if $DEBUG;
}
print "Module_part : $component\n" if $DEBUG;
my $value = get_last_rrd_data($current_rrd);
print "$component : $value\n" if $DEBUG;
if (!defined($submodule)) {
if (($value> $opt_w) && ($status ne 2)) {
$status = "1";
$problem_on_name = $component;
$problem_value = $value;
}
if ( $value > $opt_c){
$status = "2";
$problem_on_name = $component;
$problem_value = $value;
}
$response_text .= "$component: $value ";
} else {
if ($component =~ /$submodule/) {
$response_text .= "$component: $value ";
if (($value> $opt_w) && ($status ne 2)) {
$status = "1";
$problem_on_name = $component;
$problem_value = $value;
}
if ( $value > $opt_c){
$status = "2";
$problem_on_name = $component;
$problem_value = $value;
}
}
}
print "Response text : $response_text\n" if $DEBUG;
}
else {
printf "Nothing to do \n" if $DEBUG;
}
}
}
if ($status eq 1) {
print "WARNING - $problem_on_name value $problem_value, is above warning treshold $opt_w\n";
$status = $ERRORS{"WARNING"};
} elsif ($status eq 2) {
print "CRITICAL - $problem_on_name value $problem_value, is above critical treshold $opt_c\n";
$status = $ERRORS{"CRITICAL"};
} else {
print "OK - $response_text \n";
$status = $ERRORS{"OK"};
}
exit $status;
##
###
### Functions
###
##
# Decypher the rrd black box ^^
sub get_last_rrd_data {
my $rrdfile = shift;
my $last = RRDs::last($rrdfile) or die "get last value failed ($RRDs::error)";
my $start = $last - 300; # Damn rrd ! we may get two values, the one we want and 0.0, we will focus on the first ;-)
my ($rrdstart, $step, $names, $data) = RRDs::fetch($rrdfile, "--start=$start", "--end=$last", $cf) or die "fetch failed ($RRDs::error)";
my $line;
my $val;
my $value;
my $d_start = $start;
foreach $line (@$data) {
print "@ $d_start: " if $DEBUG;
$d_start += $step;
foreach $val (@$line) {
if ( ! $val ) {
print "(undef)\n" if $DEBUG;
}
else {
print "$val \n" if $DEBUG;
$value = $val;
}
}
}
if ( ! $value ) {
print "(undef)\n" if $DEBUG;
}
else {
return sprintf ("%2.1f",$value); # more human readable format
}
}
# sanitize for human readable output
sub sanitize {
my $var = shift;
if ($opt_M eq "df") {
if (($var !~ m/dev/ ) || ($var =~ m/udev/) || ($var =~ m/shm/)) { # Get rid of non physical drives
$var = "";
}
else {
$var =~ s/\_/\//g;
}
}
return $var;
}
# That one check parameters
sub check_parameters {
# Basic checks
if ($opt_V) {
print_revision($PROGNAME,'$Revision: '.$REVISION.' $');
exit $ERRORS{'UNKNOWN'};
}
if ($opt_h) {
print_help ();
print_revision($PROGNAME,'$Revision: '.$REVISION. ' $');
exit $ERRORS{'UNKNOWN'};
}
if ($opt_v) {
$DEBUG = 1;
}
if (!defined($opt_H)) {
print "Hostname requested !\n";
print_usage();
exit $ERRORS{"UNKNOWN"}
} else {
$hostname = $opt_H if (utils::is_hostname($opt_H));
($hostname) || usage("Invalid hostname or address : $opt_H\n");
printf "Hostname : $hostname\n" if $DEBUG;
if ($hostname =~ /([^\.\/]+\.[^\.\/]+)$/m) {
$domain = $1;
printf "Computed Domain : $domain\n" if $DEBUG;
}
}
if (defined($opt_D)) {
$datadir = $opt_D;
printf "Datadir : $datadir\n" if $DEBUG;
}
if (defined($opt_M)) {
$module = $opt_M;
printf "Module : $module\n" if $DEBUG;
} else {
print "Which module do you want to check ?\n";
print_usage();
exit $ERRORS{"UNKNOWN"};
}
if (defined($opt_S) and !defined($opt_M)) {
print "Which module do you want to check ?\n";
print "Can't have Submodule without Module.\n";
print_usage();
exit $ERRORS{"UNKNOWN"};
} elsif (defined($opt_S)) {
$submodule = $opt_S;
printf "SubModule : $submodule\n" if $DEBUG;
}
if (defined($opt_d)) {
$domain = $opt_d;
} else {
if (!defined($domain)) {
print "I can't guess your domain, please add the domain manually\n";
print_usage();
exit $ERRORS{"UNKNOWN"};
}
}
printf "Domain : $domain\n" if $DEBUG;
# Check warnings and critical
if (!defined($opt_w) || !defined($opt_c)) {
print "put warning and critical info !\n";
print_usage();
exit $ERRORS{"UNKNOWN"};
}
} # end check_options
sub print_usage () {
print "Usage: $0 -H <host> -M <Module> [-d <domain>] -w <warn level> -c <crit level> [-V] [-D <datadir>]\n";
}
sub print_help () {
print "\nMonitor server via Munin-node pulled data\n";
print_usage();
print <<EOT;
-h, --help
print this help message
-H, --hostname=HOST
name or IP address of host to check
-M, --module=MUNIN MODULE
Munin module value to fetch
-d, --domain=DOMAIN
Domain as defined in munin
-w, --warn=INTEGER
warning level
-c, --crit=INTEGER
critical level
-v --verbose
Be verbose
-V, --version
prints version number
-D, --datadir
In case your datadir for munin is not /var/lib/munin
EOT
}