help with check_rrd_data.pl

Brown, Robin robin.brown at uconn.edu
Wed Aug 11 19:32:45 CEST 2004


I just can't get this to run from Nagios as a service check.  All works ok from
command line as the nagios user.  The error is 'No output!' .  Other service checks
are working ok.
 
I tried to wrap it for debugging, but I can't get it to run that way either.  I think
I need to escape some characters, but I'm not having any luck.
 
Can anyone help?
 
Nagios Version 1.2 on FC2
 
Comand line:
[root at jiminy libexec]# su - nagios
-sh-2.05b$ /usr/local/nagios/libexec/check_rrd_data.pl /home/cricket/cricket-data/router1/atm1_0.1-aal5_layer.rrd 'return "Check Traffic: Warning \n" if ($value > 2621440);' 'return "Check Traffic: Critical\n" if ($value > 39321600;' 'printf "Check Traffic: OK \n", $value; return 0;' 0
Check Traffic: Warning
 
Checkcommands.cfg:
define command {
                command_name                   check_bandwidth
                command_line                   $USER1$/check_rrd_data.pl /home/cricket/cricket-data/router1/atm1_0.1-aal5_layer.rrd 'return "Check Traffic: Warning \n" if ($value > 2621440);' 'return "Check Traffic: Critical\n" if ($value > 39321600;' 'printf "Check Traffic: OK \n", $value; return 0;' 0
                }

Services.cfg:
define service {
                host_name                      router1
                service_description            BW-In
                check_command                  check_bandwidth
                use                            generic-service
                max_check_attempts             3
                normal_check_interval          5
                retry_check_interval           1
                check_period                   24x7
                notification_interval          60
                notification_period            24x7
                notification_options           w,c,r
                contact_groups                 admin
                }

check_rrd_data.pl:
#!/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 Velázquez
# current status: 0.1
#
# Copyright Notice: GPL
#
# Doesn't work! Why?
# BEGIN {
        # my $runtimedir = substr($0,0,rindex($0,'/'));
        # require "$runtimedir/utils.pm";
# }
use lib "/usr/local/nagios/libexec","/home/cricket-1.0.4/lib";

require '/usr/local/nagios/libexec/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_ex
p_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 th
an \"\", there\n";
        print STDERR "will be a warning or a critical failure. Else, the express
ion\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)

Regards,
Robin
 
 
 


More information about the Users mailing list