File: check_rrd_data.pl

package info (click to toggle)
nagios-plugins 1.4-6sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,892 kB
  • ctags: 3,369
  • sloc: ansic: 29,379; perl: 12,117; sh: 5,836; makefile: 540; python: 444; yacc: 316; awk: 46; sed: 16
file content (129 lines) | stat: -rw-r--r-- 4,198 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl -wT

# check_rrd_data plugin for nagios
#
# usage:
#    check_rrd machine_id perlexp_warn perlexp_crit perlexp_default [ds]
#
# Checks data from a RRD file. machine_id is normally an IP address, that has
# to be mapped to a RRD file, by means of the config file (by default
# /var/spool/nagios/rrd-files, a file with pairs of (machine_id,rrd_file),
# separated by whitespace). It can be a RRD file, too.
#
# 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.
#
# Use these hosts.cfg entries as examples
#
# command[check_ping]=$USER1$/check_rrd_data.pl $HOSTADDRESS$ \
#	'return "CHECK_CRICKET_PING: Warning\n" if ($value > 10);' 'return \
#	"CHECK_CRICKET_PING: Critical\n" if ($value > 100);' 'printf \
#	"PING OK - RTA = %.2fms\n", $value; return 0;' 1
# service[machine]=PING;0;24x7;3;5;1;router-admins;240;24x7;1;1;1;;check_ping
#
# initial version: 28 Nov 2000 by Esteban Manchado Velzquez
# current status: 0.1
#
# Copyright Notice: GPL
#

# Doesn't work! Why?
# BEGIN {
        # my $runtimedir = substr($0,0,rindex($0,'/'));
        # require "$runtimedir/utils.pm";
# }

require '/usr/libexec/nagios/plugins/utils.pm';
use RRD::File;
# use strict;			# RRD:File and utils.pm don't like this

my $configfilepath = "/var/spool/nagios/rrd-files";	# Change if needed
my %hostfile;						# For storing config
my $rrdfile;						# RRD file to open

$ENV{'PATH'} = "/bin:/usr/bin";
$ENV{'ENV'} = "";

if (scalar @ARGV != 4 && scalar @ARGV != 5) {
	print STDERR join "' '", @ARGV, "\n";
	my $foo = 'check_rrd_data';
	print STDERR $foo, " <file.rrd> <perl_exp_warn> <perl_exp_crit> <perl_exp_default> [<ds>]\n\n";
	print STDERR "<perl_exp_*> is an expression that gets evaluated with \$_ at the current\n";
	print STDERR "value of the data source. If it returns something other than \"\", there\n";
	print STDERR "will be a warning or a critical failure. Else, the expression\n";
	print STDERR "<perl_exp_default> will be evaluated\n";
	exit;
}

# Check configuration file
open F, $configfilepath or do {
	print "Can't open config file $configfilepath\n";
	return $ERRORS{'UNKNOWN'};
};
while (<F>) {
	next unless /(.+)\s+(.+)/;
	$hostfile{$1} = $2;
}
close F;

# Default
my $ds = defined $ARGV[4]?$ARGV[4]:0;
	# print "\$ds = " . $ds . ":";
	# print "\$ARGV[4] = " . $ARGV[4] . ":";
$ds =~ s/\$//g;		# Sometimes Nagios gives 1$ as the last parameter

# Guess which RRD file have to be opened
$rrdfile = $ARGV[0] if (-r $ARGV[0]);		# First the parameter
$rrdfile = $hostfile{$ARGV[0]} unless $rrdfile;	# Second, the config file
	# print "$ARGV[0]:";

if (! $rrdfile) {
	print "Can't open data file for $ARGV[0]\n";	# Aaaargh!
	return $ERRORS{'UNKNOWN'};	# Unknown
}

	# print "Opening file $rrdfile:";
my $rrd = new RRD::File ( -file => $rrdfile );
$rrd->open();
if (! $rrd->loadHeader()) {
	print "Couldn't read header from $rrdfile\n";
	exit $ERRORS{'UNKNOWN'};	# Unknown
}
my $value = $rrd->getDSCurrentValue($ds);
$rrd->close();

# Perl expressions to evaluate
my ($perl_exp_warn, $perl_exp_crit, $perl_exp_default) =
		($ARGV[1], $ARGV[2], $ARGV[3]);
my $result;	# Result of the expressions (will be printed)
my @data;	# Special data reserved for the expressions, to pass data

# First check for critical errors
$perl_exp_crit =~ /(.*)/;
$perl_exp_crit = $1;
$result = eval $perl_exp_crit;
if ($result) {
	print $result;
	exit 2;		# Critical
}

# Check for warnings
$perl_exp_warn =~ /(.*)/;
$perl_exp_warn = $1;
$result = eval $perl_exp_warn;
if ($result) {
	print $result;
	exit 1;		# Warning
}

$perl_exp_default =~ /(.*)/;
$perl_exp_default = $1;
eval $perl_exp_default;	# Normally returns 0 (OK)