diff options
Diffstat (limited to 'web/attachments/62197-check_rrd.pl')
| -rw-r--r-- | web/attachments/62197-check_rrd.pl | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/web/attachments/62197-check_rrd.pl b/web/attachments/62197-check_rrd.pl new file mode 100644 index 0000000..578277c --- /dev/null +++ b/web/attachments/62197-check_rrd.pl | |||
| @@ -0,0 +1,157 @@ | |||
| 1 | #!/usr/bin/perl -w | ||
| 2 | #-wT | ||
| 3 | # | ||
| 4 | # check_rrd plugin for nagios (or rather check_rrd but using the current rrd rather than cricket libraries) | ||
| 5 | # | ||
| 6 | # usage: | ||
| 7 | # check_rrd rrdfile perlexp_warn perlexp_crit perlexp_default [ds] | ||
| 8 | # | ||
| 9 | # Checks data from an RRD file. | ||
| 10 | # | ||
| 11 | # Based off the check_rrd_data.pl and the example code in the rrdtool dist. | ||
| 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 | # initial version: 28 Nov 2000 by Esteban Manchado Velázquez | ||
| 25 | # current status: 1.0 | ||
| 26 | # | ||
| 27 | # History | ||
| 28 | # 19-Sep-2003 1.0 steveh@brendata.co.uk | ||
| 29 | # Amended to utilise the RRDs supplied with rrdtool rather than the cricket code | ||
| 30 | # | ||
| 31 | # Required RRDs from rrdtool distribution be installed (install perl shared when building rrdtool). | ||
| 32 | # | ||
| 33 | # Copyright Notice: GPL | ||
| 34 | # | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | use lib '/usr/local/nagios/libexec/' ; | ||
| 38 | use utils qw(%ERRORS &print_revision); | ||
| 39 | use RRDs; | ||
| 40 | use Getopt::Long; | ||
| 41 | &Getopt::Long::config('auto_abbrev'); | ||
| 42 | |||
| 43 | my $PROGNAME="check_rrd.pl"; | ||
| 44 | |||
| 45 | sub usage { | ||
| 46 | print_revision($PROGNAME,'$Revision: 1.0 $ '); | ||
| 47 | print "Nagios Plugin - Check rrd datafile\n"; | ||
| 48 | print "=" x 75,"\nERROR: Missing or wrong arguments!\n","=" x 75,"\n"; | ||
| 49 | print "check_rrd.pl --file=<file.rrd> --warning=<perl_exp_warn> --critical=<perl_exp_crit> --default=<perl_exp_default> [--dataset=<ds>] [--cf=<cf>] [--expire=<minutes>]\n\n"; | ||
| 50 | print "<perl_exp_*> is an expression that gets evaluated with \$_ at the current\n"; | ||
| 51 | print "value of the data source. If it returns something other than \"\", there\n"; | ||
| 52 | print "will be a warning or a critical failure. Else, the expression\n"; | ||
| 53 | print "<perl_exp_default> will be evaluated\n"; | ||
| 54 | print "<ds> is the number of the dataset that you wish to use\n"; | ||
| 55 | print "<cf> is one of MIN, MAX, AVERAGE or LAST\n"; | ||
| 56 | print "<minutes> the maximum time since last rrd update, otherwise WARNING. (5 mins assumed if not specified)\n"; | ||
| 57 | exit $ERRORS{'UNKNOWN'}; # Unknown | ||
| 58 | } | ||
| 59 | |||
| 60 | my $rrdfile; # RRD file to open | ||
| 61 | my $cf; #Function to apply | ||
| 62 | my $ds; # Dataset to use | ||
| 63 | my @data; # Special data reserved for the expressions, to pass data | ||
| 64 | # Perl expressions to evaluate | ||
| 65 | my $perl_exp_warn; | ||
| 66 | my $perl_exp_crit; | ||
| 67 | my $perl_exp_default; | ||
| 68 | my $expire; | ||
| 69 | my $debug; | ||
| 70 | |||
| 71 | # Evaluate Command Line Parameters | ||
| 72 | my $status = GetOptions( | ||
| 73 | "file=s",\$rrdfile, | ||
| 74 | "critical=s",\$perl_exp_crit, | ||
| 75 | "warning=s",\$perl_exp_warn, | ||
| 76 | "default=s",\$perl_exp_default, | ||
| 77 | "dataset=i",\$ds, | ||
| 78 | "expire=i",\$expire, | ||
| 79 | "cf=s",\$cf, | ||
| 80 | "debug",\$debug | ||
| 81 | ); | ||
| 82 | |||
| 83 | $ds =~ s/\$$//g if ($ds); # Sometimes Nagios gives 1$ as the last parameter | ||
| 84 | $cf =~ s/\$$//g if($cf); # Sometimes Nagios gives 1$ as the last parameter | ||
| 85 | $rrdfile =~ s/\$$//g if($rrdfile); # Sometimes Nagios gives 1$ as the last parameter | ||
| 86 | |||
| 87 | $perl_exp_crit =~ s/\$$//g if($perl_exp_crit); # Sometimes Nagios gives 1$ as the last parameter | ||
| 88 | $perl_exp_warn =~ s/\$$//g if($perl_exp_warn); # Sometimes Nagios gives 1$ as the last parameter | ||
| 89 | $perl_exp_default =~ s/\$$//g if($perl_exp_default); # Sometimes Nagios gives 1$ as the last parameter | ||
| 90 | |||
| 91 | usage() if ($status == 0 || ! ($rrdfile && $perl_exp_warn && $perl_exp_crit)); # && $perl_exp_default)); | ||
| 92 | |||
| 93 | |||
| 94 | print $perl_exp_crit,"\n" if($debug); | ||
| 95 | print $perl_exp_warn,"\n" if($debug); | ||
| 96 | print $perl_exp_default,"\n" if($debug); | ||
| 97 | # Defaults | ||
| 98 | $ds=0 if(!($ds)); | ||
| 99 | $cf="MAX" if(!($cf)); | ||
| 100 | $expire=5*60 if(!($expire)); | ||
| 101 | |||
| 102 | if (! $rrdfile) { | ||
| 103 | print "Can't open data file for $rrdfile\n"; # Aaaargh! | ||
| 104 | return $ERRORS{'UNKNOWN'}; # Unknown | ||
| 105 | } | ||
| 106 | |||
| 107 | if($expire!=0) | ||
| 108 | { | ||
| 109 | my ($last) = RRDs::last($rrdfile); | ||
| 110 | printf("Now: %d Last: %d\n",time(),$last) if ($debug); | ||
| 111 | my $now=time(); | ||
| 112 | if($now - $last > $expire*60) | ||
| 113 | { | ||
| 114 | printf("RRD data has not been updated within expiry interval. %d minutes since last update\n",($now-$last)/60); | ||
| 115 | exit $ERRORS{'WARNING'}; | ||
| 116 | } | ||
| 117 | |||
| 118 | } | ||
| 119 | my ($start,$step,$names,$data) = RRDs::fetch ($rrdfile,$cf); | ||
| 120 | my $ERR=RRDs::error; | ||
| 121 | if($ERR) { | ||
| 122 | print "ERROR while fetching $rrdfile: $ERR\n"; # Ooops..... | ||
| 123 | exit $ERRORS{'UNKNOWN'}; # Unknown | ||
| 124 | } | ||
| 125 | |||
| 126 | my $line = @$data[$#data -1]; | ||
| 127 | my $value = @$line[$ds]; | ||
| 128 | |||
| 129 | my $result; # Result of the expressions (will be printed) | ||
| 130 | |||
| 131 | # First check for critical errors | ||
| 132 | $perl_exp_crit =~ /(.*)/; | ||
| 133 | $perl_exp_crit = $1; | ||
| 134 | print $perl_exp_crit,"\n" if($debug); | ||
| 135 | $result = eval $perl_exp_crit; | ||
| 136 | if ($result) { | ||
| 137 | print $result; | ||
| 138 | print $perl_exp_crit,"\n" if($debug); | ||
| 139 | exit 2; # Critical | ||
| 140 | } | ||
| 141 | |||
| 142 | # Check for warnings | ||
| 143 | $perl_exp_warn =~ /(.*)/; | ||
| 144 | $perl_exp_warn = $1; | ||
| 145 | print $perl_exp_warn,"\n" if($debug); | ||
| 146 | $result = eval $perl_exp_warn; | ||
| 147 | if ($result) { | ||
| 148 | print $result; | ||
| 149 | print $perl_exp_warn,"\n" if($debug); | ||
| 150 | exit 1; # Warning | ||
| 151 | } | ||
| 152 | |||
| 153 | $perl_exp_default =~ /(.*)/; | ||
| 154 | $perl_exp_default = $1; | ||
| 155 | print $perl_exp_default,"\n" if($debug); | ||
| 156 | eval $perl_exp_default; # Normally returns 0 (OK) | ||
| 157 | |||
