#!/usr/bin/perl -w #-wT # # check_rrd plugin for nagios (or rather check_rrd but using the current rrd rather than cricket libraries) # # usage: # check_rrd rrdfile perlexp_warn perlexp_crit perlexp_default [ds] # # Checks data from an RRD file. # # Based off the check_rrd_data.pl and the example code in the rrdtool dist. # # The Perl expressions are expressions to be evaluated in the following cases: # # - perlexp_crit. The first one, to check if there is a critical situation. If # it returns other than "", it will be a critical message. # - perlexp_warn. The second one to be evaluated. If returns other than "", a # warning will be issued to Nagios. # - perlexp_default. If both of the above return "", it will be evaluated, and # wathever returns this expression will be returned by the script. NOTE that # this is different from the other two cases, to allow the user issue a # warning or critical failure even if the other two don't return it. # # initial version: 28 Nov 2000 by Esteban Manchado Velázquez # current status: 1.0 # # History # 19-Sep-2003 1.0 steveh@brendata.co.uk # Amended to utilise the RRDs supplied with rrdtool rather than the cricket code # # Required RRDs from rrdtool distribution be installed (install perl shared when building rrdtool). # # Copyright Notice: GPL # use strict; use lib '/usr/local/nagios/libexec/' ; use utils qw(%ERRORS &print_revision); use RRDs; use Getopt::Long; &Getopt::Long::config('auto_abbrev'); my $PROGNAME="check_rrd.pl"; sub usage { print_revision($PROGNAME,'$Revision: 1.0 $ '); print "Nagios Plugin - Check rrd datafile\n"; print "=" x 75,"\nERROR: Missing or wrong arguments!\n","=" x 75,"\n"; print "check_rrd.pl --file= --warning= --critical= --default= [--dataset=] [--cf=] [--expire=]\n\n"; print " is an expression that gets evaluated with \$_ at the current\n"; print "value of the data source. If it returns something other than \"\", there\n"; print "will be a warning or a critical failure. Else, the expression\n"; print " will be evaluated\n"; print " is the number of the dataset that you wish to use\n"; print " is one of MIN, MAX, AVERAGE or LAST\n"; print " the maximum time since last rrd update, otherwise WARNING. (5 mins assumed if not specified)\n"; exit $ERRORS{'UNKNOWN'}; # Unknown } my $rrdfile; # RRD file to open my $cf; #Function to apply my $ds; # Dataset to use my @data; # Special data reserved for the expressions, to pass data # Perl expressions to evaluate my $perl_exp_warn; my $perl_exp_crit; my $perl_exp_default; my $expire; my $debug; # Evaluate Command Line Parameters my $status = GetOptions( "file=s",\$rrdfile, "critical=s",\$perl_exp_crit, "warning=s",\$perl_exp_warn, "default=s",\$perl_exp_default, "dataset=i",\$ds, "expire=i",\$expire, "cf=s",\$cf, "debug",\$debug ); $ds =~ s/\$$//g if ($ds); # Sometimes Nagios gives 1$ as the last parameter $cf =~ s/\$$//g if($cf); # Sometimes Nagios gives 1$ as the last parameter $rrdfile =~ s/\$$//g if($rrdfile); # Sometimes Nagios gives 1$ as the last parameter $perl_exp_crit =~ s/\$$//g if($perl_exp_crit); # Sometimes Nagios gives 1$ as the last parameter $perl_exp_warn =~ s/\$$//g if($perl_exp_warn); # Sometimes Nagios gives 1$ as the last parameter $perl_exp_default =~ s/\$$//g if($perl_exp_default); # Sometimes Nagios gives 1$ as the last parameter usage() if ($status == 0 || ! ($rrdfile && $perl_exp_warn && $perl_exp_crit)); # && $perl_exp_default)); print $perl_exp_crit,"\n" if($debug); print $perl_exp_warn,"\n" if($debug); print $perl_exp_default,"\n" if($debug); # Defaults $ds=0 if(!($ds)); $cf="MAX" if(!($cf)); $expire=5*60 if(!($expire)); if (! $rrdfile) { print "Can't open data file for $rrdfile\n"; # Aaaargh! return $ERRORS{'UNKNOWN'}; # Unknown } if($expire!=0) { my ($last) = RRDs::last($rrdfile); printf("Now: %d Last: %d\n",time(),$last) if ($debug); my $now=time(); if($now - $last > $expire*60) { printf("RRD data has not been updated within expiry interval. %d minutes since last update\n",($now-$last)/60); exit $ERRORS{'WARNING'}; } } my ($start,$step,$names,$data) = RRDs::fetch ($rrdfile,$cf); my $ERR=RRDs::error; if($ERR) { print "ERROR while fetching $rrdfile: $ERR\n"; # Ooops..... exit $ERRORS{'UNKNOWN'}; # Unknown } my $line = @$data[$#data -1]; my $value = @$line[$ds]; my $result; # Result of the expressions (will be printed) # First check for critical errors $perl_exp_crit =~ /(.*)/; $perl_exp_crit = $1; print $perl_exp_crit,"\n" if($debug); $result = eval $perl_exp_crit; if ($result) { print $result; print $perl_exp_crit,"\n" if($debug); exit 2; # Critical } # Check for warnings $perl_exp_warn =~ /(.*)/; $perl_exp_warn = $1; print $perl_exp_warn,"\n" if($debug); $result = eval $perl_exp_warn; if ($result) { print $result; print $perl_exp_warn,"\n" if($debug); exit 1; # Warning } $perl_exp_default =~ /(.*)/; $perl_exp_default = $1; print $perl_exp_default,"\n" if($debug); eval $perl_exp_default; # Normally returns 0 (OK)