#!/usr/bin/perl  
# -*- perl -*-

#===============================================================================
# Name
#   check_cpu
#
# Description
#   Checks the CPU utilization of routers
#   Returns text to STDOUT and return code to be used by the netsaint process
#
# TODO
#
# Author
#  Original author Matthew Davy (mpd@iu.edu)
#
# Copyright
#  Copyright (c) 2002 The Trustees of Indiana University.
#  All Rights Reserved.
#===============================================================================

#===============================================================================
#                               Configuration        
#===============================================================================

my $lastScriptModification = '2002-11-24';

#===============================================================================
#                              Initialization
#===============================================================================

#-------------------------------------------------------------------------------
#    Libraries
#-------------------------------------------------------------------------------

use Getopt::Std;


#-------------------------------------------------------------------------------
#    Global var declarations
#-------------------------------------------------------------------------------
my (
   # Working vars 
   $usage,		# String containing usage help
   $hostname,		# Hostname of machine being queried via SNMP
   $community,		# SNMP community string to use for query
   $oid			# CPU OID 
   );

#-------------------------------------------------------------------------------
#    Global var initializations
#-------------------------------------------------------------------------------

$usage = <<"EOF";
usage:  $0 [-h] -r <hostname> -c <community> -v <vendor>

[-h]            :       Print this message
[-r] <router>   :       IP Address or Hostname of the router
[-c] <community>:       SNMP Community String  (default = "public")
[-v] <vendor>   :       juniper | cisco
                               
$lastScriptModification
 
EOF

#===============================================================================
#                              Input Phase
#===============================================================================

# Check the usage
die $usage if (!getopts('hr:c:v:') || $opt_h);
die $usage if (!$opt_r || !$opt_v || $opt_h );

# Build the args for the check_snmp command 
$hostname = $opt_r;
$community = $opt_c || "public";
if ($opt_v eq "juniper") {
	$oid = ".1.3.6.1.4.1.2636.3.1.13.1.8.9.1.0.0";
} elsif ($opt_v eq "cisco") {
	$oid = ".1.3.6.1.4.1.9.2.1.58.0";
} else {
	die $usage;
}


# Call the check_snmp command with the appropriate args
$_ = `/usr/lib/nagios/plugins/check_snmp -H $hostname -C $community -o $oid`;


#===============================================================================
#                              Output Phase
#===============================================================================

# Print a line to STDOUT and generate an exit code based on what the 
# check_snmp command returns.

if ( (/SNMP OK - (\d+)/) || (/SNMP OK - INTEGER: (\d+)/)) {
	$cpu = $1;	

	# Print some useful text...
	print "5 Minute Avg CPU Usage is $cpu%\n";

	# Exit with the correct status (2=Critical, 1=Warning, 0=OK, -1=error)
	if ($cpu > 75) {
		exit(2);
	} elsif ($cpu > 50) { 
		exit(1);
	} else {
		exit(0);
	}
} else {
	print "Error: No response from $hostname\n";
	exit(-1);
}

