#!/usr/bin/php5
<?php
/*
** Author
**  Original author Carsten Schoene (cs@linux-administrator.com)
**
** Copyright
**  Copyright (c) 2007 linux-administrator.com
**  All Rights Reserved.
*/

$lancomtotal=".1.3.6.1.4.1.2356.600.3.54.1.47.4";
$lancomfree=".1.3.6.1.4.1.2356.600.3.54.1.47.5";

define('MYNAME',"check_mem_lancom");
define_syslog_variables();
openlog(MYNAME,LOG_PID | LOG_ODELAY,LOG_MAIL);

if ( ! extension_loaded("snmp") ) {
        if ( ! dl("snmp") ) {
                syslog(LOG_ERR,"snmp extension not loaded!");
                exit;
        }
}
ini_set("display_errors","on");

$cmdlineopt = getopt("H:C:w:c:h");

if (empty($cmdlineopt)) {
	print_help();
}

function print_help() {
        echo "Usage: " . MYNAME . " < -H  HOSTNAME | -C COMMUNITY | -w | -c >\n";
        echo "\t -H\t target Hostname to check\n";
        echo "\t -C\t community name\n";
        echo "\t -w\t warning threshold\n";
        echo "\t -c\t critical threshold\n";
        echo "\t -h\t this help screen\n";
        exit(1);
}

function print_message ( $msg, $retval ) {
	echo "$msg\n";
	exit($retval);
}
if ( isset($cmdlineopt['H'])  &&  isset($cmdlineopt['C'])  &&  isset($cmdlineopt['w'])  &&  isset($cmdlineopt['c']) ) {

	$memtotal = explode(" " , snmpget($cmdlineopt['H'], $cmdlineopt['C'], $lancomtotal ) );
	$memfree = explode(" " , snmpget($cmdlineopt['H'], $cmdlineopt['C'], $lancomfree ) );


	
	$onepercent = $memtotal[1] / 100;

	$memused = $memtotal[1] - $memfree[1];
		
	$percentused = $memused / $onepercent;

	$percentused = round($percentused,0);

	if ( $percentused > $cmdlineopt['c'] ) {

		$message = "MEMORY Critical: " . $memused . " Bytes used / " . $memtotal[1] . " Bytes total";
		print_message($message,2);

	} elseif ( $percentused > $cmdlineopt['w'] ) {

		$message = "MEMORY Warning: " . $memused . " Bytes used / " . $memtotal[1] . " Bytes total";
		print_message($message,1);

	} else {

		$message = "MEMORY OK: " . $memused . " Bytes used / " . $memtotal[1] . " Bytes total";
		print_message($message,0);

	}

} else {
	print_help();
}
?>
