#! /usr/bin/perl -w
#
# check_netscreen_mem - nagios plugin
#
# Description: plugin to query a netscreen firewall and report
# the amount of memory allocated, free and a total percentage.
#
##
# 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.
#
#
# Report bugs to: ssugar@proserveit.com
#
# 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/lib/nagios/plugins"  ;
use utils qw($TIMEOUT %ERRORS &print_revision &support);

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

my $PROGNAME = "check_netscreen_mem";
my $status;

my $state = "UNKNOWN";
my $answer = "";
my $snmpkey = 0;
my $community = "public";
my $port = 161;
my @snmpoids;
my $snmpnsResMemAllocated = '.1.3.6.1.4.1.3224.16.2.1.0';
my $snmpnsResMemFree = '.1.3.6.1.4.1.3224.16.2.2.0';
my $hostname;
my $memory;
my $error;
my $response;
my $snmp_version = 1 ;
my $opt_h ;
my $opt_w = 70;
my $opt_c = 90;
my $opt_V ;
my $opt_t = "m";

my $memory_used=0;
my $totalmem=0;
my $freememinkb=0;
my $freememinmb=0;
my $allocmeminkb=0;
my $allocmeminmb=0;

# 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,
			"t=s" => \$opt_t, "type=s" => \$opt_t);



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

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

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

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


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

	if (!defined($memory)) {
		$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,$snmpnsResMemAllocated);
push(@snmpoids,$snmpnsResMemFree);

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

if ($opt_t =~ "b"){
        $totalmem = $response->{$snmpnsResMemAllocated} + $response->{$snmpnsResMemFree};

        if($snmpnsResMemAllocated ne 0) {$memory_used = 100*($response->{$snmpnsResMemAllocated}/($totalmem));}
                $answer = sprintf("host '%s', Memory : Allocated: %s Free: %s Percent Used:(%2.2f%%)\n",
                        $hostname,
                        $response->{$snmpnsResMemAllocated},
                        $response->{$snmpnsResMemFree},
                        $memory_used
                        );
}
elsif ($opt_t =~ "k"){
        $allocmeminkb = $response->{$snmpnsResMemAllocated} / 1024;
        $freememinkb = $response->{$snmpnsResMemFree} / 1024;
        $totalmem = $freememinkb + $allocmeminkb;

        if($snmpnsResMemAllocated ne 0) {$memory_used = 100*($allocmeminkb/$totalmem);}
                $answer = sprintf("host '%s', Memory: Allocated:(%2.2f) Free:(%2.2f) Percent Used:(%2.2f%%)\n",
	               $hostname,
	               $allocmeminkb,
	               $freememinkb,
	               $memory_used
	               );
}
elsif ($opt_t =~ "m"){
        $allocmeminkb = $response->{$snmpnsResMemAllocated} / 1024;
        $allocmeminmb = $allocmeminkb / 1024;
        $freememinkb = $response->{$snmpnsResMemFree} / 1024;
        $freememinmb = $freememinkb / 1024;
        $totalmem = $freememinmb + $allocmeminmb;

        if($snmpnsResMemAllocated ne 0) {$memory_used = 100*($allocmeminmb/$totalmem);}
                $answer = sprintf("host '%s', Memory: Allocated:(%2.2f) Free:(%2.2f) Percent Used:(%2.2f%%)\n",
	               $hostname,
	               $allocmeminmb,
	               $freememinmb,
	               $memory_used
                        );
}
else{
	$state='UNKNOWN';
	print ("$state: Only possible options for type are b (bytes), k (kilobytes), m (megabytes)\n");
	exit $ERRORS{$state};
}

# print "($response->{$snmpnsResMemAllocated} $allocmeminmb\n";
# print "($response->{$snmpnsResMemFree} $freememinmb\n";

#$totalmem = $response->{$snmpnsResMemAllocated} + $response->{$snmpnsResMemFree};

#if($snmpnsResMemAllocated ne 0) {$memory_used = 100*($response->{$snmpnsResMemAllocated}/($totalmem));}
#   $answer = sprintf("host '%s', Memory : Allocated: %s Free: %s Percent Used:(%2.2f%%)\n",
#      $hostname,
#      $response->{$snmpnsResMemAllocated},
#      $response->{$snmpnsResMemFree},
#      $memory_used
#   );






   $memory->close;

   if ( $memory_used <= $opt_w ) {
      $state = 'OK';
   }
   else {
    if ( $memory_used <= $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 "Copyright (C) 2005 Scott Sugar\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 memory\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 memory used <- defaults to 70\n";
        printf "   -c                integer threshold for critical level on percent memory used <- defaults to 90\n";
	printf "   -t		     type of output: b (bytes), k (kilobytes), m (megabytes) <- defaults to m\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.0 $');

}
