#!/usr/bin/php -q
<?php

define('OK', 0);
define('WARNING', 1);
define('CRITICAL', 2);
define('UNKWON', 3);

function get_inifile()
{

	$file = '/usr/local/opmon/libexec/opservices/dummy/init_dummy.ini';

	if (file_exists($file)) {
		$ini = file($file);
	} else {
		print 'Cant find '.$file."\n";
		exit(CRITICAL);
	}

	return $ini;
}

function get_options()
{

	$options = getopt("H:S:");

	if (!count($options)) {
		print "Use -H macro HOSTADDRESS and/or -S macro SERVICEDESC";
		exit(CRITICAL);
	}

	return $options;

}

function randomize($type, $init, $final)
{

	$random = null;

	if ($type == "float") {
		$random = rand($init, $final) .".". rand($init, $final);
		$random = round($random, 2);
	} elseif ($type == "int") {
		$random = rand($init, $final);
	} else {
		print "Invalid format type metric, use int or float\n";
		exit(CRITICAL);
	}

	return $random;
}

function str_replace_once($str_pattern, $str_replacement, $string)
{

	if (strpos($string, $str_pattern) !== false) {
		$occurrence = strpos($string, $str_pattern);
		return substr_replace($string, $str_replacement, strpos($string, $str_pattern), strlen($str_pattern));
	}

	return $string;
}

function metric($value, $warn, $crit)
{

	if ($crit >= $warn) {
		if ($value >= $crit) {
			return CRITICAL;
		} elseif ($value >= $warn) {
			return WARNING;
		} elseif ($value < $warn) {
			return OK;
		} else {
			quit("Unable to check.", 3);
		}

	} else {
		if ($value <= $warn) {
			return WARNING;
		} elseif ($value <= $crit) {
			return CRITICAL;
		} elseif ($value > $warn) {
			return OK;
		} else {
			quit("Unable to check.", 3);
		}
	}

}

function quit($text, $code)
{

	print $text."\n";
	exit($code);

}


function main()
{

	$ini = get_inifile();
	$options = get_options();

	$code = OK;

	foreach ($ini as $line) {
		$array = explode('|', $line);
		$host = null;
		$service = null;
		$group = null;
		$output = null;
		$metrics = null;

		$mhost = $options['H'];
		$mservice = $options['S'];

		# if service
		if (count($array) == 5) {
				list($host, $service, $group, $output, $metrics) = explode('|', $line);
		# if host
		} elseif (count($array) == 4) {
				list($host, $group, $output, $metrics) = explode('|', $line);
		} else {
				print "Invalid format ini file.\n";
				exit;
		}

		if (preg_match("/(.+)_\d+/", $mhost, $match)) {
			$mhost = $match[1];
		}

		if (preg_match("/(.+)_\d+/", $mservice, $match)) {
			$mservice = $match[1];
		}

		if ($host == $mhost and $service == $mservice) {
			$check = array();

			if (isset($output) && isset($metrics)) {
				$m = array();
				foreach (explode(' ', trim($metrics)) as $data => $perfdata) {
					if (preg_match("/(.+)=\{([a-z]+):(\d+):(\d+)\};(\d+);(\d+);(.+)/", $perfdata, $match)) {
						$metric = $match[1];
						$type = $match[2];
						$init = $match[3];
						$end = $match[4];
						$warning = $match[5];
						$critical = $match[6];
						$mm = $match[7];

						$value = randomize($type, $init, $end);

						$check[] = array(
							'value' => $value,
							'warning' => $warning,
							'critical' => $critical
						);

						$m[] = $metric.'='.$value.";".$warning.";".$critical.";".$mm;

					} else {
						print "Invalid perdata, use ex.: download={float:1024:4096};3072;3572;0;4096\n";
						exit(CRITICAL);
					}

				}
				$exit_codes = array();

				foreach ($check as $c) {
					$output = str_replace_once("?", $c['value'], $output);
					$exit_codes[] = metric($c['value'], $c['warning'], $c['critical']);
				}

				asort($exit_codes);
				foreach ($exit_codes as $e) {
					$code = $e;
				}

				print $output."|".trim(implode(' ', $m))."\n";
			} elseif (isset($output)) {
				print $output;
			}
		}
	}
	exit($code);
}

main();
