summaryrefslogtreecommitdiffstats
path: root/web/attachments/62197-check_rrd.pl
blob: 578277c94f4fbf4958eed8be02dab1a67a509d72 (plain)
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
#!/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=<file.rrd> --warning=<perl_exp_warn> --critical=<perl_exp_crit> --default=<perl_exp_default> [--dataset=<ds>] [--cf=<cf>] [--expire=<minutes>]\n\n";
	print "<perl_exp_*> 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 "<perl_exp_default> will be evaluated\n";
	print "<ds> is the number of the dataset that you wish to use\n";
	print "<cf> is one of MIN, MAX, AVERAGE or LAST\n";
	print "<minutes> 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)