#!/usr/bin/perl

# v0.1		CISCO Cpu

use lib "/app/nagios/libexec";
use utils qw(%ERRORS $TIMEOUT);

use vars qw($opt_h $opt_C $opt_H $opt_c $opt_w $opt_p);
use Getopt::Long;
use Net::SNMP;


my $OID_Cisco = ".1.3.6.1.4.1.9.2.1.58.0";

#Definition des variables par defaut
$opt_c = (80);
$opt_w = (40);
$community = ('public');

Getopt::Long::Configure('bundling');
GetOptions
    (    "h"  => \$opt_h, "help"         => \$opt_h,
     	"C=s" => \$opt_C, "community=s"  => \$opt_C,
     	"w=s" => \$opt_w, "warning=s"  => \$opt_w,
     	"c=s" => \$opt_c, "critical=s"  => \$opt_c,
         "p" => \$opt_p, 	"perfdata"  => \$opt_p,
     	"H=s" => \$opt_H, "hostname=s"   => \$opt_H);

sub isnnum { # Return true if arg is not a number
  my $num = shift;
  if ( $num =~ /^(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;}
  return 1;
}

if ($opt_h) {
    print_usage();
    exit $ERRORS{'UNKNOWN'};
}

if (!$opt_H) {
        print_usage();
        exit $ERRORS{'UNKNOWN'};
}else {
        $host_address = $opt_H;
}

if ( defined($opt_C)) {
        $community = $opt_C;
}

if ( isnnum($opt_w) || isnnum($opt_c)) {
        print "Numeric value for warning or critical !\n";
        print_usage();
        exit $ERRORS{"UNKNOWN"};
}else {
        if (($opt_c > 100) || ($opt_w > 100)) {
                print "critical percent & warning percent must be < 100 !\n";
                exit $ERRORS{"UNKNOWN"};
        }
        if (($opt_c!= 0) && ($opt_w > $opt_c)) {
                print "warning > critical ! \n";
                print_usage();
                exit $ERRORS{"UNKNOWN"};
        }
        $crit=$opt_c;
        $warn=$opt_w;
}

sub print_usage()
{
 print "Usage: check_cisco_cpu -H host -C community -w <warning>  -c <critical>\n\n";
 print "Options:\n";
 print " -H --host STRING or IPADDRESS\n";
 print "   Check interface on the indicated host.\n";
 print " -w --warning\n";
 print "   warning thoughput. (default = 20%)\n";
 print " -c --critical\n";
 print "   critical thoughput. (default = 10%)\n";
 print " -C --community=COMMUNITY NAME\n";
 print "   community name for the host's SNMP agent\n";
 print " -p --perf-data\n";
 print "   enable perf-data for centreon\n";
}


($session, $error) = Net::SNMP->session(
            -hostname  => $host_address,
            -community => $community,
            -port      => 161,
	    -version   => 2,
	    -timeout   => 5
            );

if (!defined($session)) {
   printf("ERROR opening session: %s.\n", $error);
   exit $ERRORS{"WARNING"};
}


$result = $session->get_request(
   Varbindlist => [$OID_Cisco],
);

if (!defined($result)) {
	printf("ERROR: for get $OID_Cisco OID : %s.\n", $session->error);
	$session->close;
	exit $ERRORS{"UNKNOWN"};
}

$cpu5min = $result->{$OID_Cisco};

$session->close;


	if ($cpu5min > $warn) 
	{
        	if ($cpu5min> $crit) 
		{
	                $nagios_status_str = "Critical $cpu5min > $crit";
      			$nagios_status = $ERRORS{"CRITICAL"};
		}
        	else 
		{
                	$nagios_status_str = "Warning $cpu5min > $warn";
                	$nagios_status = $ERRORS{"WARNING"}; 
		}
	}
	else 
	{
        	$nagios_status_str = "OK $cpu5min %";
        	$nagios_status =  $ERRORS{"OK"};
	}

	

	if( defined( $opt_p ) )
	{
		$nagios_status_str .= " | cisco_cpu=$cpu5min%";
	}

	print $nagios_status_str;
	exit( $nagios_status );




