#!/usr/local/bin/perl -w # # check_remote_disk # # by ATonns Mon Sep 9 14:15:23 EDT 2002 # # monitor the hrStorage OID in net-snmpd while # emulating the check_disk plugin for nagios # # check_disk sample output: # bigdog$ ./check_disk -w 15% -c 10% -p /home # DISK OK - [5082779 kB (58%) free on /dev/dsk/c0t1d0s6] # # $Id: check_remote_disk,v 1.3 2003/07/08 18:12:33 atonns Exp atonns $ # # check_remote_disk # Copyright (C) 2003 - iVillage.com, Anthony Tonns # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # perl setup use strict; use Getopt::Long; use Net::SNMP (); use CGI; use IO::String; use lib "/usr/local/nagios/libexec"; use utils qw($TIMEOUT %ERRORS &print_revision &support); delete @ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; use NWPE; # static variables my $PROGNAME = "check_remote_disk"; my $version = '$Revision: 1.3 $'; # OIDs for the net-snmpd host resources mib # # iso.org.dod.internet.mgmt.mib-2.host.hrStorage.hrStorageTable.hrStorageEntry my $baseoid = "1.3.6.1.2.1.25.2.3.1"; my $baselen = length($baseoid) + 1; # hrStorageDescr my $oidkey = "3"; # hrStorageSize my $maxsizekey = "5"; # hrStorageUsed my $currusedkey = "6"; # auth config stuff my $username = "xxxxxxxx"; my $authpass = "xxxxxxxx"; my $privpass = "xxxxxxxx"; ################################################################################ my $nwpe = NWPE->new($PROGNAME,$version); @ARGV = $nwpe->get_args; if ( ! exists $ARGV[0] ) { print "$PROGNAME: no args passed\n"; $nwpe->quit($ERRORS{UNKNOWN}); } # parse args my ($opt_V,$opt_h,$opt_p,$opt_H,$opt_c,$opt_w,$opt_v); Getopt::Long::Configure('bundling'); GetOptions( "V" => \$opt_V, "version" => \$opt_V, "h" => \$opt_h, "help" => \$opt_h, "v+" => \$opt_v, "verbose+" => \$opt_v, "H=s" => \$opt_H, "hostname=s" => \$opt_H, "p=s" => \$opt_p, "path=s" => \$opt_p, "c=s" => \$opt_c, "critical=s" => \$opt_c, "w=s" => \$opt_w, "warning=s" => \$opt_w, ); # check args if ( $opt_h ) { print_usage($nwpe,""); } if ( $opt_V ) { print_revision($PROGNAME,$version); $nwpe->quit($ERRORS{OK}); } if ( ! $opt_H ) { print_usage($nwpe,"must specify hostname with -H option."); } if ( ! $opt_p ) { print_usage($nwpe,"must specify path with -p option."); } if ( ! $opt_c ) { print_usage($nwpe,"must specify critical value with -c option."); } if ( ! $opt_w ) { print_usage($nwpe,"must specify warning value with -w option."); } # validate/rename input to sane variable names my $hostname = $opt_H; my $path = $opt_p; my $warn = $1 if ($opt_w =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])+/); ($warn) || print_usage($nwpe,"Invalid warning threshold: $opt_w"); my $crit = $1 if ($opt_c =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])/); ($crit) || print_usage($nwpe,"Invalid critical threshold: $opt_c"); # set a timeout w/error message $SIG{'ALRM'} = sub { print ("$PROGNAME: ERROR: alarm timeout\n"); $nwpe->quit($ERRORS{UNKNOWN}); }; alarm($TIMEOUT); # establish a session my ($session,$error) = Net::SNMP->session( -hostname => $hostname, -version => "3", -username => $username, -authprotocol => "md5", -authpassword => $authpass, -privpassword => $privpass, -maxmsgsize => 1048576, -timeout => $TIMEOUT, -retries => 3, ); if ( $error ) { print "$PROGNAME: session error: $error\n"; $nwpe->quit($ERRORS{UNKNOWN}); } # retreive the entire hrStorage table my $result = $session->get_table( -baseoid => $baseoid, ); if ( $session->error ) { print "$PROGNAME: get_table error: ".$session->error."\n"; $session->close; $nwpe->quit($ERRORS{UNKNOWN}); } $session->close; my %map; # from path to oid instance my %data; # data table re-hashed by path # create a map based on the final oid for the column with $oidkey foreach (sort keys %{$result}) { my $key = $_; my ($column,$instance) = split(/\./, substr($key,$baselen)); if ( $column eq $oidkey ) { $map{$instance} = ${$result}{$key}; } } # NOW reorder all the data in the table based on what was $oidkey's value foreach (sort keys %{$result}) { my $key = $_; my ($column,$instance) = split(/\./, substr($key,$baselen)); if ( $column ne $oidkey ) { $data{$map{$instance}}->{$column} = ${$result}{$key}; } } ## for debugging ## let's see it in order now #foreach (sort keys %data) { # my $key = $_; # print "$key "; # print " " if length($key) < 8; # foreach (sort keys %{$data{$key}}) { # my $subkey = $_; # my $val = $data{$key}->{$subkey}; # print " $subkey:$val, "; # } # print "\n"; #} # fail now if we didn't find the partition that was asked for if ( ! exists $data{$path} ) { print "$PROGNAME: ".$path." does not exist\n"; $nwpe->quit($ERRORS{UNKNOWN}); } # crunch the numbers my $maxsize = $data{$path}->{$maxsizekey}; my $currused = $data{$path}->{$currusedkey}; my $curravail = $maxsize - $currused; my $currpcnt = int( ($curravail/$maxsize)*100 ); # since we've checked all the sanity beforehand, # start off assuming all is well my $state = $ERRORS{OK}; # check the warning state first # is it a percentage? if ( substr($warn,-1,1) eq "%" ) { my $wpcnt = substr($warn,0,length($warn)-1) + 0; if ( $wpcnt > $currpcnt ) { $state = $ERRORS{WARNING} if ( $state < $ERRORS{WARNING} ); } } # ah, it must be a total value else { my $wavail = $warn + 0; if ( $wavail > $curravail ) { $state = $ERRORS{WARNING} if ( $state < $ERRORS{WARNING} ); } } # check the critical state last # is it a percentage? if ( substr($crit,-1,1) eq "%" ) { my $cpcnt = substr($crit,0,length($crit)-1) + 0; if ( $cpcnt > $currpcnt ) { $state = $ERRORS{CRITICAL} if ( $state < $ERRORS{CRITICAL} ); } } # ah, it must be a total value else { my $cavail = $crit + 0; if ( $cavail > $curravail ) { $state = $ERRORS{CRITICAL} if ( $state < $ERRORS{CRITICAL} ); } } # print text for the humans my $statetxt; foreach (keys(%ERRORS)) { my $key = $_; $statetxt=$key if ( $state == $ERRORS{$key} ); } # the almighty output print "DISK $statetxt - [$curravail kB ($currpcnt%) free on $path]\n"; $nwpe->quit($state); ################################################################################ # how does this work again? sub print_usage { my ($nwpe,$msg) = @_; my $PROGNAME = $nwpe->PROGNAME; my $version = $nwpe->version; if ( $msg ) { print "$PROGNAME: $msg\n\n"; } print_revision($PROGNAME,$version); print "Usage: $PROGNAME -H hostname -p path " . "-w warnlimit -c critlimit\n"; print "Usage: $PROGNAME --hostname=hostname --path=path " . "--warning=warnlimit --critical=critlimit\n"; print " ".' ' x length($PROGNAME) . " [-v|--verbose -V|--version -h|--help]\n"; $nwpe->quit($ERRORS{UNKNOWN}); }