#!/usr/bin/perl -wT
# check_ciscotemp.pl
# 
# Copyright (C) 2000  Leland E. Vandervort <leland@mmania.com>
#
# 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 (or with Nagios);  if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
# Boston, MA 02111-1307, USA
####################################
# Nagios pluging to check inlet and outlet temperatures on 
# Cisco router platforms which support environmental monitoring
# (7200, 7500, GSR12000...)
####################################
# default temperature thresholds are 30C for inlet, 40C outlet.
# if input or output is less than thresholds, returns OK
# if equal to (the temps don't change that rapidly) returns WARNING
# if greater than threshold, returns CRITICAL
# if undetermined, or cannot access environmental, returns UNKNOWN
# (in accordance with the plugin coding guidelines)
####################################

require 5.004;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw($helpparameterlist $versionparameterlist $hostnameparameter
			 $communityparameter $portparameter $timeoutparameter
			 $criticalparameter $warningparameter $verboseparameter);
use Net::SNMP;
use vars qw($opt_H $opt_C $opt_p $opt_t $opt_v $ithresh $othresh
	    $inlet_thresh $outlet_thresh $inlet_warn $outlet_warn
	    $PROGNAME);
use utils qw(%ERRORS);

$portparameter->default(161);
$warningparameter->type("THRESHOLD");
$warningparameter->checker(sub { my ($opt, $parameter, $plugin) = @_;
				 if (!defined $$opt) { return; }
				 &Plugin::Parameter::_check_threshold($opt, $parameter, $plugin);
				 $$opt =~ m/^(\d+)[,:](\d+)$/;
				 ($inlet_warn, $outlet_warn) = ($1, $2);
			       });
$warningparameter->description("Warning threshold values");
$warningparameter->required("no");
$criticalparameter->type("THRESHOLD");
$criticalparameter->checker(sub { my ($opt, $parameter, $plugin) = @_;
				  if (!defined $$opt) { return; }
				  &Plugin::Parameter::_check_threshold($opt, $parameter, $plugin);
				  $$opt =~ m/^(\d+)[,:](\d+)$/;
				  ($inlet_thresh, $outlet_thresh) = ($1, $2);
				});
$criticalparameter->description("Critical threshold values");
$criticalparameter->required("no");
my $ithreshparameter = new Plugin::Parameter(-name => "ithresh", -flags => [ 'ithresh' ],
					     -optional => "yes", -valueoptional => "no", -type => "TEMPERATURE", -default => 30,
					     -checker => \&Plugin::Parameter::_check_temperature,
					     -binding => \$ithresh,
					     -description => "Inlet temperature threshold");
my $othreshparameter = new Plugin::Parameter(-name => "othresh", -flags => [ 'othresh' ],
					     -optional => "yes", -valueoptional => "no", -type => "TEMPERATURE", -default => 40,
					     -checker => \&Plugin::Parameter::_check_temperature,
					     -binding => \$othresh,
					     -description => "Outlet temperature threshold");
my @commandparameterlist = ( $hostnameparameter,
			     $communityparameter,
			     $ithreshparameter,
			     $othreshparameter,
			     $warningparameter,
			     $criticalparameter,
			     $portparameter,
			     $timeoutparameter,
			     $verboseparameter );

my $plugin = new Plugin(-revision => '$Revision: 1.3 $',
			-copyright => "2000  Leland E. Vandervort <leland\@mmania.com>, 2004 Howard Wilkinson <howard\cohtech.com>",
			-shortcomment => "Perl envmon temperature plugin for Nagios",
			-checker => sub { my ($plugin) = @_;
					  $inlet_warn = $ithresh unless (defined $inlet_warn);
					  $outlet_warn = $othresh unless (defined $outlet_warn);
					  $inlet_thresh = $ithresh unless (defined $inlet_thresh);
					  $outlet_thresh = $othresh unless (defined $outlet_thresh); },
			-parameterlists => [ \@commandparameterlist, $helpparameterlist, $versionparameterlist ]);

$plugin->init();

my $INTAKE_TEMP = "1.3.6.1.4.1.9.9.13.1.3.1.3.1";
my $OUTLET_TEMP = "1.3.6.1.4.1.9.9.13.1.3.1.3.3";

my $state = "UNKNOWN";

my $in_temp = &SNMPGET($INTAKE_TEMP);
my $out_temp = &SNMPGET($OUTLET_TEMP);

if (($in_temp < $inlet_thresh) && ($out_temp < $outlet_thresh)) {
    $state = "OK";
}
elsif (($in_temp == $inlet_thresh) || ($out_temp == $outlet_thresh)) {
    if(($in_temp > $inlet_thresh) || ($out_temp > $outlet_thresh)) {
        $state = "CRITICAL";
    }
    else {
        $state = "WARNING";
    }
}
elsif (($in_temp > $inlet_thresh) || ($out_temp > $outlet_thresh)) {
    $state = "CRITICAL";
}
else {
    $state = "WARNING";
}

print "$PROGNAME $state: Inlet Temp: $in_temp Outlet Temp: $out_temp\n";
exit($ERRORS{$state});

sub SNMPGET {
    my $OID = shift;
    my ($session,$error) = Net::SNMP->session(-Hostname        =>      $opt_H,
					      -Community       =>      $opt_C,
					      -Port            =>      $opt_p,
					      -Version         =>      "snmpv1",
					      -Timeout         =>      $opt_t);
    if(!defined($session)) {
        printf("$PROGNAME $state: %s\n", $error);
        exit($ERRORS{$state});
    }
    my $response = $session->get_request($OID);
    if(!defined $response) {
        printf("$PROGNAME $state: %s\n", $session->error());
        $session->close();
	exit($ERRORS{$state});
    }
    $session->close();
    return($response->{$OID});
}

