#! /usr/bin/perl -w
#
# check_netscreen_cpu - nagios plugin
#
# Description: plugin to query a netscreen firewall and report
# average usage of cpu.
#
##
# 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.
#
#
#
# This plugin is based on existing work from lots of users.
# No liability

use POSIX;
use strict;
# Update the following value to reflect your install
use lib "/usr/local/nagios/libexec"  ;
use utils qw($TIMEOUT %ERRORS &print_revision &support);

use Net::SNMP;
use Getopt::Long;
&Getopt::Long::config('bundling');

my $PROGNAME = "check_netscreen_cpu";
my $status;

my $state = "UNKNOWN";
my $answer = "";
my $snmpkey = 0;
my $community = "public";
my $port = 161;
my @snmpoids;
my $snmpnsResCpuAvg = '.1.3.6.1.4.1.3224.16.1.3.0';
my $hostname;
my $cpuavg;
my $error;
my $response;
my $snmp_version = 1 ;
my $opt_h ;
my $opt_w = 70;
my $opt_c = 90;
my $opt_V ;

#Just in case of problems, let's not hang nagios
$SIG{'ALRM'} = sub {
     print ("ERROR: No snmp response from $hostname (alarm)\n");
     exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);


$status = GetOptions(
			"V"   => \$opt_V, "version"    => \$opt_V,
			"w=i"   => \$opt_w, "warning=i"    => \$opt_w,
			"c=i"   => \$opt_c, "critical=i"    => \$opt_c,
			"h"   => \$opt_h, "help"       => \$opt_h,
			"v=i" => \$snmp_version, "snmp_version=i"  => \$snmp_version,
			"C=s" =>\$community, "community=s" => \$community,
			"p=i" =>\$port,  "port=i",\$port,
			"H=s" => \$hostname, "hostname=s" => \$hostname );



if ($status == 0)
{
	print_help();
	exit $ERRORS{'OK'};
}

if ($opt_V) {
	print_revision($PROGNAME,'$Revision: 1.1 $ ');
	exit $ERRORS{'OK'};
}

if ($opt_h) {
	print_help();
	exit $ERRORS{'OK'};
}

if (! utils::is_hostname($hostname)){
	usage();
	exit $ERRORS{"UNKNOWN"};
}


if ( $snmp_version =~ /[12]/ ) {
   ($cpuavg, $error) = Net::SNMP->session(
		-hostname  => $hostname,
		-community => $community,
		-port      => $port,
		-version	=> $snmp_version
	);

	if (!defined($cpuavg)) {
		$state='UNKNOWN';
		$answer=$error;
		print ("$state: $answer");
		exit $ERRORS{$state};
	}
}elsif ( $snmp_version =~ /3/ ) {
	$state='UNKNOWN';
	print ("$state: No support for SNMP v3 yet\n");
	exit $ERRORS{$state};
}else{
	$state='UNKNOWN';
	print ("$state: No support for SNMP v$snmp_version yet\n");
	exit $ERRORS{$state};
}



push(@snmpoids,$snmpnsResCpuAvg);

   if (!defined($response = $cpuavg->get_request(@snmpoids))) {
      print ("$cpuavg->error\n");
      $answer=$cpuavg->error;
      $cpuavg->close;
      $state = 'CRITICAL';
      print ("$state: $answer\n");
      exit $ERRORS{$state};
}
 
	else{
       		$cpuavg = $response->{$snmpnsResCpuAvg};

                $answer = sprintf("host '%s', CPU Load: Avg:(%2.2f%%)| stat=$cpuavg%%;$opt_w;$opt_c;0;100; \n",
	               $hostname,
	               $cpuavg
                       );
	    
}




   if ( $cpuavg <= $opt_w ) {
      $state = 'OK';
   }
   else {
    if ( $cpuavg <= $opt_c ) {
	$state = 'WARNING';
	} else {
	$state = 'CRITICAL';
	}
   }

print ("$state: $answer");
exit $ERRORS{$state};


sub usage {
  printf "\nMissing arguments!\n";
  printf "\n";
  printf "usage: \n";
  printf "$PROGNAME -H <HOSTNAME> [-C <community>] [-w warning] [-c critical]\n";
  printf "For help, try: $PROGNAME -h \n";
  printf "$PROGNAME comes with ABSOLUTELY NO WARRANTY\n";
  printf "This programm is licensed under the terms of the ";
  printf "GNU General Public License\n(check source code for details)\n";
  printf "\n\n";
  exit $ERRORS{"UNKNOWN"};
}

sub print_help {
	printf "$PROGNAME plugin for Nagios monitors the cpu\n";
  	printf "usage for a Netscreen/Juniper host\n";
	printf "\nUsage:\n";
	printf "   -H (--hostname)   Hostname to query - (required)\n";
	printf "   -C (--community)  SNMP read community <- defaults to public,\n";
	printf "                     used with SNMP v1 and v2c\n";
        printf "   -w                integer threshold for warning level on percent cpu used <- defaults to 70\n";
        printf "   -c                integer threshold for critical level on percent cpu used <- defaults to 90\n";
	printf "   -v (--snmp_version)  1 for SNMP v1 (default)\n";
	printf "                        2 for SNMP v2c\n";
	printf "                        SNMP v2c will use get_bulk for less overhead\n";
	printf "                        if monitoring with -d\n";
	printf "   -p (--port)       SNMP port (default 161)\n";
	printf "   -V (--version)    Plugin version\n";
	printf "   -h (--help)       usage help \n\n";
	print_revision($PROGNAME, '$Revision: 1.1 $');

}
