[Nagiosplug-devel] RRD database check script
Yves Martin
yves.martin at elca.ch
Tue Feb 21 04:42:04 CET 2006
Hello,
I have improved the "check_rrd_data.pl" (switch debug message)
and create a "check_rrd_percent.pl" for my own usage: compute the
% of disk used from cacti rrd file
And I do not want to create a new project.
Is it possible for somebody having "nagiosexchange" account to
install
my scripts in "Databases / RDD databases" project ? Thank you in
advance.
check_rrd_data.pl
-----------------------
#!/usr/bin/perl -wT
# check_rrd_data plugin for nagios
#
# usage:
# check_rrd_data.pl rrdfile datasource warning critical
#
# Copyright Notice: GPL
use lib "/usr/lib/nagios/plugins/";
use utils qw(%ERRORS $TIMEOUT);
use RRDs;
use strict; # RRD:File and utils.pm don't like this
$ENV{'PATH'} = "/bin:/usr/bin";
$ENV{'ENV'} = "";
use constant DEBUG => 0;
if (scalar @ARGV != 1 && scalar @ARGV != 4) {
print STDERR "Usage: check_rrd_data.pl rrdfile datasource warning
critical\n";
print STDERR " or check_rrd_data.pl rrdfile to list available
data sources.\n";
exit $ERRORS{'UNKNOWN'};
}
# Default
# Guess which RRD file have to be opened
my $rrdfile = $ARGV[0] if (-r $ARGV[0]); # First the parameter
print "\$rrdfile = $rrdfile\n" if DEBUG;
my $ds = defined $ARGV[1]?$ARGV[1]:0;
print "\$ds = " . $ds . "\n" if DEBUG;
$ds =~ s/\$//g; # Sometimes Nagios gives 1$ as the last parameter
my $warn = defined $ARGV[2] ? $ARGV[2] : 0;
print "\$warn = " . $warn . "\n" if DEBUG;
my $crit = defined $ARGV[3] ? $ARGV[3] : 0;
print "\$crit = " . $crit . "\n" if DEBUG;
my ($last) = RRDs::last ($rrdfile);
print "\$last: ","$last\n" if DEBUG;
my ($start,$step,$names,$data) = RRDs::fetch ($rrdfile,"AVERAGE","-s",$
last,"-e",$last);
$last = $start;
($start,$step,$names,$data) = RRDs::fetch ($rrdfile,"AVERAGE","-s",$las
t,"-e",$last);
if (DEBUG) {
print "Start: ", scalar localtime($start), " ($start)\n";
print "Step size: $step seconds\n";
print "DS names: ", join (", ", @$names)."\n";
print "Data points: ", $#$data + 1, "\n";
print "Data:\n";
foreach my $line (@$data) {
print " ", scalar localtime($start), " ($start) ";
$start += $step;
foreach my $val (@$line) {
printf "%12d ", $val;
}
print "\n";
}
}
my $i;
my $found_index;
for ($i= 0; $i < @$names; $i++) {
if (@$names[$i] eq $ds) {
$found_index = $i;
last;
}
}
if (!defined($found_index)) {
print "Available data sources in $rrdfile: " . join (", ", @$names)
. "\n";
exit $ERRORS{'UNKNOWN'};
}
my $value = @$data[0]->[$found_index];
printf "DS Index: %d value: %s\n", $found_index, "$value" if DEBUG;
# First check for critical errors
if ($value > $crit) {
print "RRD CRITICAL ", "$ds:", " $value\n";
exit $ERRORS{'CRITICAL'};
}
# Check for warnings
if ($value > $warn) {
print "RRD WARNING ", "$ds:", " $value\n";
exit $ERRORS{'WARNING'};
}
# Normally returns 0 (OK)
print "RRD Check OK ", "$ds:", " $value\n";
exit $ERRORS{'OK'};
-----------------------
check_rrd_percent.pl
-----------------------
#!/usr/bin/perl -wT
# check_rrd_percent plugin for nagios
#
# usage:
# check_rrd_percent.pl rrdfile datasource1 datasource2 warning
critical
#
# Copyright Notice: GPL
#
use lib "/usr/lib/nagios/plugins/";
use utils qw(%ERRORS $TIMEOUT);
use RRDs;
use strict; # RRD:File and utils.pm don't like this
$ENV{'PATH'} = "/bin:/usr/bin";
$ENV{'ENV'} = "";
use constant DEBUG => 0;
if (scalar @ARGV != 1 && scalar @ARGV != 5) {
print STDERR "Usage: check_rrd_data.pl rrdfile datasource1 datasource2
warning critical\n";
print STDERR " or check_rrd_data.pl rrdfile to list available
data sources.\n";
exit $ERRORS{'UNKNOWN'};
}
# Default
# Guess which RRD file have to be opened
my $rrdfile = $ARGV[0] if (-r $ARGV[0]); # First the parameter
print "\$rrdfile = $rrdfile\n" if DEBUG;
my $ds1 = defined $ARGV[1]?$ARGV[1]:0;
print "\$ds1 = " . $ds1 . "\n" if DEBUG;
$ds1 =~ s/\$//g; # Sometimes Nagios gives 1$ as the last parameter
my $ds2 = defined $ARGV[2]?$ARGV[2]:0;
print "\$ds2 = " . $ds2 . "\n" if DEBUG;
$ds2 =~ s/\$//g; # Sometimes Nagios gives 1$ as the last parameter
my $warn = defined $ARGV[3] ? $ARGV[3] : 0;
print "\$warn = " . $warn . "\n" if DEBUG;
my $crit = defined $ARGV[4] ? $ARGV[4] : 0;
print "\$crit = " . $crit . "\n" if DEBUG;
my ($last) = RRDs::last ($rrdfile);
print "\$last: ","$last\n" if DEBUG;
my ($start,$step,$names,$data) = RRDs::fetch ($rrdfile,"AVERAGE","-s",$
last,"-e",$last);
$last = $start;
($start,$step,$names,$data) = RRDs::fetch ($rrdfile,"AVERAGE","-s",$las
t,"-e",$last);
if (DEBUG) {
print "Start: ", scalar localtime($start), " ($start)\n";
print "Step size: $step seconds\n";
print "DS names: ", join (", ", @$names)."\n";
print "Data points: ", $#$data + 1, "\n";
print "Data:\n";
foreach my $line (@$data) {
print " ", scalar localtime($start), " ($start) ";
$start += $step;
foreach my $val (@$line) {
printf "%12d ", $val;
}
print "\n";
}
}
my $i;
my $found_index1;
my $found_index2;
for ($i= 0; $i < @$names; $i++) {
if (@$names[$i] eq $ds1) {
$found_index1 = $i;
}
if (@$names[$i] eq $ds2) {
$found_index2 = $i;
}
}
if (!defined($found_index1) || !defined($found_index2)) {
print "Available data sources in $rrdfile: " . join (", ", @$names)
. "\n";
exit $ERRORS{'UNKNOWN'};
}
my $value1 = @$data[0]->[$found_index1];
printf "DS Index: %d value: %s\n", $found_index1, "$value1" if
DEBUG;
my $value2 = @$data[0]->[$found_index2];
printf "DS Index: %d value: %s\n", $found_index2, "$value2" if
DEBUG;
my $value = $value1 / ($value1 + $value2) * 100;
print "Percentage datasource1/(datasource1+datasource2)*100 =
$value\n" if DEBUG;
# First check for critical errors
if ($value > $crit) {
print "RRD CRITICAL ", "$ds1/($ds1+$ds2) % :", " $value\n";
exit $ERRORS{'CRITICAL'};
}
# Check for warnings
if ($value > $warn) {
print "RRD WARNING ", "$ds1/($ds1+$ds2) % :", " $value\n";
exit $ERRORS{'WARNING'};
}
# Normally returns 0 (OK)
print "RRD Check OK ", "$ds1/($ds1+$ds2) % :", " $value\n";
exit $ERRORS{'OK'};
-----------------------
- Yves Martin (ymartin)
-----------------------
The mailing list archive is found here:
http://www.nagiosexchange.org/nagiosplug-devel.31.0.html
More information about the Devel
mailing list