#!/usr/bin/php . */ class Sensor { private function recvXML($ipaddr, $username, $password) { $context = stream_context_create(array( "http" => array( "header" => "Authorization: Basic " . base64_encode($username . ":" . $password) ) )); $data = file_get_contents("http://" . $ipaddr . "/fresh.xml", false, $context); return simplexml_load_string($data); } private function cmpResults($result, $min, $max) { $tmpRes = intval($result); $arrResult = array(); if (($tmpRes < $max) && ($tmpRes < $min)) { $arrResult["prefix"] = "OK"; $arrResult["excode"] = 0; } else if (($tmpRes < $max) && ($tmpRes >= $min)) { $arrResult["prefix"] = "WARN"; $arrResult["excode"] = 1; } else if (($tmpRes >= $max) && ($tmpRes > $min)) { $arrResult["prefix"] = "CRIT"; $arrResult["excode"] = 2; } else { $arrResult["prefix"] = "UNKW"; $arrResult["excode"] = 3; } $arrResult["result"] = $result; return $arrResult; } public static function getSensorData($ipaddr, $username, $password, $min, $max, $tsensor) { $xml = self::recvXML($ipaddr, $username, $password); foreach ($xml->sns as $sensor) { if ($sensor->attributes()->id == $tsensor) { return self::cmpResults($sensor->attributes()->val, $min, $max); } } } } // SCRIPT ENTRY POINT $shortopts = "u:p:i:s:m:M:"; $options = getopt($shortopts); if (count($options) === 6) { $data = Sensor::getSensorData($options["i"], $options["u"], $options["p"], $options["m"], $options["M"], $options["s"]); switch ($options["s"]) { case 1: $strRet = "%s: Room temperature is %s °C | temperature=%s;%s;%s"; break; case 2: $strRet = "%s: Room humidity is at %s%% | humidity=%s;%s;%s"; break; } printf($strRet, $data["prefix"], $data["result"], $data["result"], $options["m"], $options["M"]); exit($data["excode"]); } else { printf("Syntax error!\nUsage: %s -arg1 -arg2 ...\n\n", $argv[0]); printf("The following parameters must be specified:\n -u \n -p \n -i \n -s <1..2>\n 1 = Temperature\n 2 = Humidity\n -m \n -M \n\n"); printf("Written by Warnimont Pol\nCopyright (C) 2015 ASTRON Buildings S.A.\nThe check_temp_hum_th2e script comes with ABSOLUTELY NO WARRANTY!\n"); printf("Please visit https://github.com/pwarnimo/nagios_plugins for licensing informations.\n"); exit(1); }