#!/usr/local/bin/perl -w
#
# check_remote_load
#
# by ATonns Mon Sep  9 14:15:23 EDT 2002
#
# monitor the hrStorage OID in net-snmpd while
# emulating the check_load plugin for nagios
#
# check_load sample output:
#
# bigdog$ ./check_load -w 10,15,20 -c 15,20,25
# load average: 0.24, 0.20, 0.20
# bigdog$ ./check_load -w 1,10,15 -c 10,15,20
# load average: 2.57, 2.57, 2.57 WARNING
# bigdog$ ./check_load -w 1,10,15 -c 2,15,20
# load average: 2.59, 2.57, 2.57 CRITICAL
#

#
# $Id: check_remote_load,v 1.3 2003/07/08 18:12:33 atonns Exp atonns $
#
# check_remote_load
# 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_load";
my $version = '$Revision: 1.3 $';

# OIDs for the net-snmpd load average table
#
# iso.org.dod.internet.private.enterprises.ucdavis.laTable.laEntry.laLoad
my $baseoid = "1.3.6.1.4.1.2021.10.1.3";
my $baselen = length($baseoid) + 1;
# column -> load average linkage
my %linkage = (
	1 => 1,
	2 => 5,
	3 => 15,
);

# 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_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,
	"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_c )
	{ print_usage($nwpe,"must specify critical values with -c option."); }
if ( ! $opt_w )
	{ print_usage($nwpe,"must specify warning values with -w option."); }

# validate/rename input to sane variable names
my $hostname = $opt_H;
#
my ($w1,$w5,$w15) = split(',',$opt_w,3);
my ($c1,$c5,$c15) = split(',',$opt_c,3);
map {
	my $input = $_;
	my $output = $1 if ($input =~ /([0-9]+)/);
	($output) || print_usage($nwpe,"Invalid threshold: '$input' ");
} ($w1,$w5,$w15,$c1,$c5,$c15);
#
if (! ($w1<$c1) or ! ($w5<$c5) or ! ($w15<$c15) ) {
	print_usage($nwpe,qq!Inconsistence in parameters: "warning load" greater than "critical load".!);
}

#DEBUG
#print "chk warn: 1:$w1 5:$w5 15:$w15\n";
#print "chk crit: 1:$c1 5:$c5 15:$c15\n";

# 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 laTable
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 %data;

# 
foreach (sort keys %{$result}) {
        my $key = $_;
        my $column = substr($key,$baselen);
	$data{$linkage{$column}} = ${$result}{$key};
#DEBUG
#	print "$linkage{$column} minute load average = ${$result}{$key}\n";
}

# since we've checked all the sanity beforehand,
# start off assuming all is well
my $state = $ERRORS{OK};

if ( $data{1} > $w1 || $data{5} > $w5 || $data{15} > $w15 ) {
	$state = $ERRORS{WARNING};
}
if ( $data{1} > $c1 || $data{5} > $c5 || $data{15} > $c15 ) {
	$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 "load average: $data{1}, $data{5}, $data{15}";
print " $statetxt" unless $state == $ERRORS{OK};
print "\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 -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15\n";
	print "Usage: $PROGNAME --hostname=hostname " .
				"--warning=WLOAD1,WLOAD5,WLOAD15 --critical=CLOAD1,CLOAD5,CLOAD15\n";
	print "       ".' ' x length($PROGNAME) .
				     " [-v|--verbose -V|--version -h|--help]\n";
	$nwpe->quit($ERRORS{UNKNOWN});
}
