#!/usr/bin/php -q
<?php
/*
---------------------------------------------------------------------------
salam_check_nt_service 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['H']) && isset($args['S']) && $args['H'] != '' && $args['S'] != '') {
	$cmd = 'wmic -U "'. $wmi_user . '"%"' . $wmi_password . '" //' . $args['H'] . ' "Select Name, State, Status from Win32_Service WHERE Name=\'' . $args['S'] . '\' OR DisplayName=\'' . $args['S'] . '\'"';
	exec($cmd,$data,$return_code);
	if ($return_code == 0) {
		list($name, $state, $status) = explode("|", $data[2]);
		if (isset($state) && $state != '') {
			switch ($state) {
				case "Running":
					if ($status == 'OK') {
						echo 'OK - ' . $args['H'] . ':' . $name . ':' . $state . ':' . $status . '|state=0';
						exit;
					}
					else {
						echo 'Warning - ' . $args['H'] . ':' . $name . ':' . $state . ':' . $status . '|state=1';
						exit(1);
					}
					break;
				case "Paused":
					echo 'Warning - ' . $args['H'] . ':' . $name . ':' . $state . ':' . $status . '|state=1';
					exit(1);
					break;
				case "Stopped":
					echo 'Critical - ' . $args['H'] . ':' . $name . ':' . $state . ':' . $status . '|state=2';
					exit(2);
					break;
				default:
					echo 'ERROR - ' . $args['H'] . ':' . $name . ':' . $state . ':' . $status . '|state=3';
					exit(3);
					break;
			}
		}
		else {
			if (!isset($data[0]) || $data[0] == '')
				$data[0] = 'Could not find service ' . $args['S'];
			echo 'ERROR - ' . $data[0] . '|state=3';
			exit(3);
		}
	}
	else {
		if (!isset($data[0]) || $data[0] == '')
			$data[0] = 'Could not find service ' . $args['S'];
		echo 'ERROR - ' . $data[0] . '|state=3';
		exit(3);
	}
}
else {
	echo 'ERROR - Missing Arguments|state=3';
	exit(3);
}

?>
