#!/usr/bin/perl -wT
# cm@financial.com 07/2002


require 5.004;
use POSIX;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw($helpparameterlist $versionparameterlist $hostnameparameter
			 $userparameter $warningparameter $criticalparameter

			 $communityparameter
			 $authprotocolparameter $authpasswordparameter $authkeyparameter
			 $privprotocolparameter $privpasswordparameter $privkeyparameter
			 $snmpversionparameter

			 $timeoutparameter $portparameter $verboseparameter);
use Net::SNMP;

use vars qw($opt_P $opt_V $opt_h $opt_m $opt_s
	    $opt_W $opt_u $opt_p $opt_w $opt_c
	    $opt_d $opt_x $opt_K $opt_k $opt_A
	    $opt_a $opt_C $opt_S $opt_t
	    $opt_v $xml $PROGNAME);

use utils qw(%ERRORS &usage);

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';

$hostnameparameter->flags([ 'm', @{$hostnameparameter->flags()} ]);

$userparameter->default('nobody');
$authprotocolparameter->default('MD5');
$privprotocolparameter->default('DES');
$portparameter->default(161);
$warningparameter->default(70);
$warningparameter->description("Percentage disk space used before Warning status occurs");
$criticalparameter->default(85);
$criticalparameter->description("Percentage disk space used before Critical status occurs");

my $devicenumberparameter = new Plugin::Parameter(-name => "devicenumber", -flags => [ 'd', 'devicenumber' ],
						  -type => "INTEGER", -optional => "yes", -valueoptional => "no",
						  -default => 1,
						  -checker => \&Plugin::Parameter::_check_integer,
						  -description => "Ordinal number of device in MIB to be queried");

my @commandparameterlist = ( $hostnameparameter,
			     $communityparameter,
			     $userparameter,
			     $authprotocolparameter,
			     $authpasswordparameter,
			     $authkeyparameter,
			     $privprotocolparameter,
			     $privpasswordparameter,
			     $privkeyparameter,
			     $devicenumberparameter,
			     $warningparameter,
			     $criticalparameter,
			     $snmpversionparameter,
			     $portparameter,
			     $timeoutparameter,
			     $verboseparameter );

my $plugin = new Plugin(-revision         => '$Revision: 1.2$',
			-copyright        => "2002 <cm\@financial.com>, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment     => "Report on disk space using SNMP access",
			-longcomment      => "",
			-parameterlists   => [ \@commandparameterlist, $helpparameterlist, $versionparameterlist ] );

$plugin->init();

if ($opt_w >= $opt_c) {
  $plugin->usage();
  usage("$PROGNAME UNKNOWN: Critical ratio must be higher than Warning ratio!\n");
}

my @SNMPArgs = ( -hostname => $opt_m,
		 -timeout  => $opt_t,
		 -port     => $opt_p,
		 -version  => $opt_S );

my @SNMPAuthenticators = ();
if ($opt_S eq "snmpv1" || $opt_S eq "snmpv2"
   || $opt_S == 1 || $opt_S == 2) {
  @SNMPAuthenticators = ( @SNMPAuthenticators, -community => $opt_C );
} else {
  my %authenticators = ( username       => $opt_u,
			 authprotocol   => $opt_a,
			 authpassword   => $opt_A,
			 authkey        => $opt_k,
			 privprotocol   => $opt_x,
			 privkey        => $opt_K,
			 privpassword   => $opt_P );

  foreach my $key (keys %authenticators) {
    next unless ($authenticators{$key});
    @SNMPAuthenticators = ( @SNMPAuthenticators,
			    -$key  => $authenticators{$key} );
  }
}

my $deviceSize='.1.3.6.1.2.1.25.2.3.1.5.'.$opt_d;
my $deviceUsed='.1.3.6.1.2.1.25.2.3.1.6.'.$opt_d;
my $deviceName='.1.3.6.1.2.1.25.2.3.1.3.'.$opt_d;

my @OIDs=($deviceSize, $deviceUsed, $deviceName);

my ($session, $error) = Net::SNMP->session( @SNMPArgs, @SNMPAuthenticators );

if (!defined $session) {
  print "$PROGNAME UNKNOWN: Unable to contact server '$opt_m' $error\n";
  exit $ERRORS{'UNKNOWN'};
}

my $response = $session->get_request(-varbindlist => \@OIDs);

if (!defined $response) {
  printf("$PROGNAME UNKNOWN: %s.\n", $session->error);
  $session->close;
  exit $ERRORS{'UNKNOWN'};
}

my $ratio = $response->{$deviceUsed} * 100 / $response->{$deviceSize};

if ($ratio > $opt_c) {
  printf("$PROGNAME CRITICAL: %s usage %.2f%%\n", $response->{$deviceName}, $ratio);
  exit $ERRORS{'CRITICAL'};
}

if ($ratio > $opt_w) {
  printf("$PROGNAME WARNING: %s usage %.2f%%\n", $response->{$deviceName}, $ratio);
  exit $ERRORS{'WARNING'};
}

printf("$PROGNAME OK: %s usage %.2f%%\n", $response->{$deviceName}, $ratio);

exit $ERRORS{'OK'};
