#!/usr/bin/perl -w

# nagios: +epn

=pod

=head1 NAME

check_tp_mem.pl - Check Tipping Point memory pressure

=head1 DESCRIPTION

This script will check memory on a Tipping Point device.  

=cut

sub check_tp_mem {

    use strict;

    use FindBin;
    use lib "$FindBin::Bin/lib";

    use Nagios::Plugin::SNMP;
    my $total=0;
    my $used=0;
    my $warn;
	my $crit;
    my $LABEL = 'TPT MEM';

    my $plugin = Nagios::Plugin::SNMP->new(
        'shortname' => $LABEL,
        'usage' => 'USAGE: %s'
    );

    $plugin->getopts;

    my $DEBUG = $plugin->opts->get('snmp-debug');

    #  Return from current state will be one of these:
    my @states = qw(ok low-warning warning high-warning
                    low-critical critical high-critical);

    my %oids = qw(
        sgProxyMemAvailable.0  .1.3.6.1.4.1.10734.3.3.2.5.3.6.0
        sgProxyMemSysUsage.0   .1.3.6.1.4.1.10734.3.3.2.5.3.1.0
        memWarningThreshold.0  .1.3.6.1.4.1.10734.3.3.2.5.3.2.0
        memCriticalThreshold.0 .1.3.6.1.4.1.10734.3.3.2.5.3.3.0
    );

    #  Get the mem values
    my $result = $plugin->get(values %oids);

    #  Close and destroy session
    $plugin->close();

    my @perf_data;

    my %mem;

    $mem{'avail'} = $result->{$oids{'sgProxyMemAvailable.0'}};
    $mem{'sys'} = $result->{$oids{'sgProxyMemSysUsage.0'}};

    $mem{'warn'} = $result->{$oids{'memWarningThreshold.0'}};
    $mem{'crit'} = $result->{$oids{'memCriticalThreshold.0'}};


    my $level =OK;
    my $output = "$LABEL ";

    if ($mem{'sys'} <$mem{'warn'}) {
        $output .= "OK - Utilization $mem{'sys'}% ";
    } elsif (($mem{'sys'} >= $mem{'warn'}) && ($mem{'sys'} < $mem{'crit'} )) {
        $output .= "WARNING  - Utilization $mem{'sys'}% >= $mem{'warn'}% ";
        $level = WARNING;
    } else {
        $output .= "CRITICAL - Utilization $mem{'sys'}% >= $mem{'crit'}% ";
        $level = CRITICAL;
    }
	$total=$mem{'avail'}/1048576;
	my $total2=sprintf("%.2f", $total);
	$used=$mem{'avail'}*$mem{'sys'}/100/1048576;
	my $used2=sprintf("%.2f", $used);	
        $warn=$mem{'avail'}*$mem{'warn'}/100/1048576;
	my $warn2=sprintf("%.2f", $warn);
	$crit=$mem{'avail'}*$mem{'crit'}/100/1048576;
	my $crit2=sprintf("%.2f", $crit);
    print "$output, Memory Used: $used2/$total2 MB ",
          "|'Percentage_Used'=$mem{'sys'}%;$mem{'warn'};$mem{'crit'};0;100",
	   "'Memory'=$used2\MB;$warn2;$crit2;0;$total2\n";

    return $level;

    sub debug {

        #return unless $DEBUG == 1;

        my $msg = shift;

        print STDERR scalar(localtime()) . ": $msg\n";

    }

}

exit check_tp_mem();

