#!/usr/local/bin/php
<?php
define_syslog_variables();
class Script_Timer {
function Script_Timer ()
{
return true;
}
function Start_Timer ()
{
define ("TIMER_START_TIME", microtime());
return true;
}
function Get_Time ($decimals=2)
{
// $decimals will set the number of decimals you want for your milliseconds.
// format start time
$start_time = explode (" ", TIMER_START_TIME);
$start_time = $start_time[1] + $start_time[0];
$start_time = $start_time * 1000;
// get and format end time
$end_time = explode (" ", microtime());
$end_time = $end_time[1] + $end_time[0];
$end_time = $end_time * 1000;
return number_format ($end_time - $start_time, $decimals);
}
}
Script_Timer::Start_Timer();
$options = "H:s:h";
$opts = getopt("$options");
$service_uri="/";
$help="$argv[0] [options] \n-h [help] \n-s \"Suchstring\" \n-H [hostname]";
if($opts[s]=='' || $opts[H]==''){ echo "$help\n";exit(); }
$string=$opts[s];
$host=$opts[H];
$hilfe=$opts[h];
function doPost($uri,$postdata,$host){
$da = fsockopen($host, 80, $errno, $errstr,10);
if (!$da) {
echo "Keine Verbindung zu $host moeglich. $errstr ($errno)<br/>\n";
exit(2);
}
else {
$salida ="POST $uri HTTP/1.1\r\n";
$salida.="Host: $host\r\n";
$salida.="User-Agent: Nagios PHP Script\r\n";
$salida.="Content-Type: application/x-www-form-urlencoded\r\n";
$salida.="Content-Length: ".strlen($postdata)."\r\n";
$salida.="Connection: close\r\n\r\n";
$salida.=$postdata;
fwrite($da, $salida);
while (!feof($da))
$response.=fgets($da, 128);
$response=split("\r\n\r\n",$response);
$header=$response[0];
$responsecontent=$response[1];
if(!(strpos($header,"Transfer-Encoding: chunked")===false)){
$aux=split("\r\n",$responsecontent);
for($i=0;$i<count($aux);$i++)
if($i==0 || ($i%2==0))
$aux[$i]="";
$responsecontent=implode("",$aux);
}//if
return chop($responsecontent);
}//else
}//function-doPost
$return=doPost($service_uri,$vars,$host);
if ($string) {
$suchmuster = "/$string/";
preg_match($suchmuster, $return, $treffer, PREG_OFFSET_CAPTURE, 3);
if ($treffer[0][0] == "$string" ) {
syslog(LOG_INFO, "Laufzeit: ".Script_Timer::Get_Time(0). " ms +OK. Webseite auf $host beinhaltet $string. \n");
echo "Laufzeit: ".Script_Timer::Get_Time(0). " ms +OK. Webseite auf $host beinhaltet $string. \n"; exit(0);
}
else
{
syslog(LOG_INFO, "Laufzeit: ".Script_Timer::Get_Time(0). " ms +CRITICAL. Webseite auf $host wirft FEHLER\n");
echo "Laufzeit: ".Script_Timer::Get_Time(0). " ms +CRITICAL. Webseite auf $host wirft FEHLER\n"; exit(2);
}
}
else {
print "Suchstring ist leer \n";
}