1 #!/usr/bin/perl -wT
3 # check_rrd_data plugin for nagios
4 #
5 # usage:
6 # check_rrd machine_id perlexp_warn perlexp_crit perlexp_default [ds]
7 #
8 # Checks data from a RRD file. machine_id is normally an IP address, that has
9 # to be mapped to a RRD file, by means of the config file (by default
10 # /var/spool/nagios/rrd-files, a file with pairs of (machine_id,rrd_file),
11 # separated by whitespace). It can be a RRD file, too.
12 #
13 # The Perl expressions are expressions to be evaluated in the following cases:
14 #
15 # - perlexp_crit. The first one, to check if there is a critical situation. If
16 # it returns other than "", it will be a critical message.
17 # - perlexp_warn. The second one to be evaluated. If returns other than "", a
18 # warning will be issued to Nagios.
19 # - perlexp_default. If both of the above return "", it will be evaluated, and
20 # wathever returns this expression will be returned by the script. NOTE that
21 # this is different from the other two cases, to allow the user issue a
22 # warning or critical failure even if the other two don't return it.
23 #
24 # Use these hosts.cfg entries as examples
25 #
26 # command[check_ping]=$USER1$/check_rrd_data.pl $HOSTADDRESS$ \
27 # 'return "CHECK_CRICKET_PING: Warning\n" if ($value > 10);' 'return \
28 # "CHECK_CRICKET_PING: Critical\n" if ($value > 100);' 'printf \
29 # "PING OK - RTA = %.2fms\n", $value; return 0;' 1
30 # service[machine]=PING;0;24x7;3;5;1;router-admins;240;24x7;1;1;1;;check_ping
31 #
32 # initial version: 28 Nov 2000 by Esteban Manchado Velázquez
33 # current status: 0.1
34 #
35 # Copyright Notice: GPL
36 #
38 # Doesn't work! Why?
39 # BEGIN {
40 # my $runtimedir = substr($0,0,rindex($0,'/'));
41 # require "$runtimedir/utils.pm";
42 # }
44 require '/usr/libexec/nagios/plugins/utils.pm';
45 use RRD::File;
46 # use strict; # RRD:File and utils.pm don't like this
48 my $configfilepath = "/var/spool/nagios/rrd-files"; # Change if needed
49 my %hostfile; # For storing config
50 my $rrdfile; # RRD file to open
52 $ENV{'PATH'} = "/bin:/usr/bin";
53 $ENV{'ENV'} = "";
55 if (scalar @ARGV != 4 && scalar @ARGV != 5) {
56 print STDERR join "' '", @ARGV, "\n";
57 my $foo = 'check_rrd_data';
58 print STDERR $foo, " <file.rrd> <perl_exp_warn> <perl_exp_crit> <perl_exp_default> [<ds>]\n\n";
59 print STDERR "<perl_exp_*> is an expression that gets evaluated with \$_ at the current\n";
60 print STDERR "value of the data source. If it returns something other than \"\", there\n";
61 print STDERR "will be a warning or a critical failure. Else, the expression\n";
62 print STDERR "<perl_exp_default> will be evaluated\n";
63 exit;
64 }
66 # Check configuration file
67 open F, $configfilepath or do {
68 print "Can't open config file $configfilepath\n";
69 return $ERRORS{'UNKNOWN'};
70 };
71 while (<F>) {
72 next unless /(.+)\s+(.+)/;
73 $hostfile{$1} = $2;
74 }
75 close F;
77 # Default
78 my $ds = defined $ARGV[4]?$ARGV[4]:0;
79 # print "\$ds = " . $ds . ":";
80 # print "\$ARGV[4] = " . $ARGV[4] . ":";
81 $ds =~ s/\$//g; # Sometimes Nagios gives 1$ as the last parameter
83 # Guess which RRD file have to be opened
84 $rrdfile = $ARGV[0] if (-r $ARGV[0]); # First the parameter
85 $rrdfile = $hostfile{$ARGV[0]} unless $rrdfile; # Second, the config file
86 # print "$ARGV[0]:";
88 if (! $rrdfile) {
89 print "Can't open data file for $ARGV[0]\n"; # Aaaargh!
90 return $ERRORS{'UNKNOWN'}; # Unknown
91 }
93 # print "Opening file $rrdfile:";
94 my $rrd = new RRD::File ( -file => $rrdfile );
95 $rrd->open();
96 if (! $rrd->loadHeader()) {
97 print "Couldn't read header from $rrdfile\n";
98 exit $ERRORS{'UNKNOWN'}; # Unknown
99 }
100 my $value = $rrd->getDSCurrentValue($ds);
101 $rrd->close();
103 # Perl expressions to evaluate
104 my ($perl_exp_warn, $perl_exp_crit, $perl_exp_default) =
105 ($ARGV[1], $ARGV[2], $ARGV[3]);
106 my $result; # Result of the expressions (will be printed)
107 my @data; # Special data reserved for the expressions, to pass data
109 # First check for critical errors
110 $perl_exp_crit =~ /(.*)/;
111 $perl_exp_crit = $1;
112 $result = eval $perl_exp_crit;
113 if ($result) {
114 print $result;
115 exit 2; # Critical
116 }
118 # Check for warnings
119 $perl_exp_warn =~ /(.*)/;
120 $perl_exp_warn = $1;
121 $result = eval $perl_exp_warn;
122 if ($result) {
123 print $result;
124 exit 1; # Warning
125 }
127 $perl_exp_default =~ /(.*)/;
128 $perl_exp_default = $1;
129 eval $perl_exp_default; # Normally returns 0 (OK)