#!/usr/bin/php -q
<?php
/*
---------------------------------------------------------------------------
salam_check_nt_memory for SALAM

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM 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 3 of the License, or
    (at your option) any later version.

    SALAM 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 SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/

foreach ($argv as $value) {
	$temp .= ' ' . $value;
}
$temparr = explode("-", $temp);
foreach ($temparr as $value) {
	list($arg, $data) = explode(" ", $value, 2);
	$args[$arg] = trim($data);
}

$settings_file = dirname($_SERVER['PATH_TRANSLATED']) . "/../library/settings.php";
require($settings_file);

if (isset($args['C']))
	list($criticalmb,$criticalpercent) = explode(",", $args['C']);
else {
	$criticalmb = 0;
	$criticalpercent = 0;
}
if (isset($args['W']))
	list($warningmb,$warningpercent) = explode(",", $args['W']);
else {
	$warningmb = 0;
	$warningpercent = 0;
}

if (isset($args['H']) && $args['H'] != '') {
	$cmd = 'wmic -U "'. $wmi_user . '"%"' . $wmi_password . '" //' . $args['H'] . ' "Select FreePhysicalMemory FROM Win32_OperatingSystem"';
	exec($cmd,$data,$return_code);
	if ($return_code == 0) {
		list($freemem, $OS, $misc) = explode("|", $data[2], 3);
		$totalcmd = 'wmic -U "'. $wmi_user . '"%"' . $wmi_password . '" //' . $args['H'] . ' "Select TotalPhysicalMemory FROM Win32_ComputerSystem"';
		exec($totalcmd,$totaldata,$totalreturn_code);
		if ($return_code == 0) {
			list($name, $totalmem) = explode("|", $totaldata[2]);
			$totalmem = round($totalmem / 1048576);
			$freemem = round($freemem / 1024);
			$freepercent = round(($freemem / $totalmem) * 100);
			$usedmem = 100 - $freepercent;
			if ($freemem < $criticalmb || $freepercent < $criticalpercent) {
				echo 'Critical - ' . $args['H'] . ':' . $freemem . ' MB Free, ' . $freepercent . '% Free |freemb=' . $freemem . ' used%=' . $usedmem . ' totalmb=' . $totalmem;
				exit(2);
			}
			else if ($freemem < $warningmb || $freepercent < $warningpercent) {
				echo 'Warning - ' . $args['H'] . ':' . $freemem . ' MB Free, ' . $freepercent . '% Free |freemb=' . $freemem . ' used%=' . $usedmem . ' totalmb=' . $totalmem;
				exit(1);
			}
			else {
				echo 'OK - ' . $args['H'] . ':' . $freemem . ' MB Free, ' . $freepercent . '% Free |freemb=' . $freemem . ' used%=' . $usedmem . ' totalmb=' . $totalmem;
				exit(0);
			}
			
		}
	}
}
else {
	echo 'ERROR - Missing Arguments|state=3';
	exit(3);
}

?>
