#!/usr/bin/perl

##################################################################################
##################################################################################
#######################  Made by Tytus Kurek on October 2012  ####################
##################################################################################
##################################################################################
####   This is a Nagios Plugin destined to check RAM usage on Cisco devices   ####
##################################################################################
##################################################################################

use strict;
use vars qw($community $critical $IP $warning);

use Getopt::Long;
use Pod::Usage;

# Subroutines execution

getParameters ();
checkMemoryUsage ();

# Subroutines definition

sub checkMemoryUsage ()	# Checks memory usage via SNMP
{
	my $freeOID = '.6.1';
	my $usedOID = '.5.1';
	my $OID = '1.3.6.1.4.1.9.9.48.1.1.1';
	my $version = '2c';

	my $command = "/usr/bin/snmpwalk -v $version -c $community $IP $OID 2>&1";
	my $result = `$command`;

	if ($result =~ m/^Timeout.*$/)
	{
		my $output = "UNKNOWN! No SNMP response from $IP. | 'memory usage'=0%";
		my $code = 3;
		exitScript ($output, $code);
	}

	my $extendedOID = $OID . $freeOID;
	$command = "/usr/bin/snmpget -v $version -c $community $IP $extendedOID";
	$result = `$command`;
	$result =~ m/^SNMPv2-SMI::enterprises\.9\.9\.48\.1\.1\.1\.6\.1\s=\sGauge32:\s(\d+)$/;
	my $free = $1;

	$extendedOID = $OID . $usedOID;
	$command = "/usr/bin/snmpget -v $version -c $community $IP $extendedOID";
	$result = `$command`;
	$result =~ m/^SNMPv2-SMI::enterprises\.9\.9\.48\.1\.1\.1\.5\.1\s=\sGauge32:\s(\d+)$/;
	my $used = $1;

	my $all = $used + $free;
	my $usage = $used / $all * 100;
	$usage =~ m/(\d+\.\d{0,2})\d*/;
	$usage = $1;

	my $output;
	my $code;

	if ($usage < $warning)
        {
                $output = "OK! Memory usage $usage%. | 'memory usage'=$usage%";
                $code = 0;
        }

        elsif (($usage >= $warning) && ($usage < $critical))
        {
                $output = "WARNING! Memory usage $usage%. | 'memory usage'=$usage%";
                $code = 1;
        }

        else
        {
                $output = "CRITICAL! Memory usage $usage%. | 'memory usage'=$usage%";
                $code = 2;
        }

        exitScript ($output, $code);
}

sub exitScript ()	# Exits the script with an appropriate message and code
{
	print "$_[0]\n";
	exit $_[1];
}

sub getParameters ()	# Obtains script parameters and prints help if needed
{
	my $help = '';

	GetOptions ('help|?' => \$help,
		    'C=s' => \$community,
		    'H=s' => \$IP,
		    'crit=s' => \$critical,
		    'warn=s' => \$warning)

	or pod2usage (1);
	pod2usage (1) if $help;
	pod2usage (1) if (($community eq '') || ($critical eq '') || ($IP eq '') || ($warning eq ''));
	pod2usage (1) if (($IP !~ m/^\d+\.\d+\.\d+\.\d+$/) || ($critical !~ m/^\d{1,3}$/) || ($warning !~ m/^\d{1,3}$/));

=head1 SYNOPSIS

check_asa_mem.pl [options] (-help || -?)

=head1 OPTIONS

Mandatory:

-H	IP address of monitored Cisco ASA device

-C	SNMP community

-warn	Warning threshold in %

-crit	Critical threshold in %

=cut
}
