Archive for November 15th, 2008
Server Ping PHP Script
by admin on Nov.15, 2008, under Coding
This Script will ping the servers on the $p array non stop, timeout is calculated * 5, so it you set _timeout to 2 the server timeout will be 10 seconds, that means that after 10 seconds of no reply it will take the server/service as down. It’s obviously recommended that you don’t run this script on the very same machine you want to test, since, if it gets down, you will never know (stupid). Apart from that, if you run this script on a machine with a crappy connection or/and port limited/firewalled you might expect problems.
Click here to see the .phps file with right padding or check this ugly text above (thanks to wordpress) :
<?php
// (c) z3n – R1V1@081114 – Port Check – Distribute under MIT License
define(‘is_debug’,0); // debug mode
define(‘w_sw’,1); // enable warning by email
define(‘_e’,'happy@didlidodli.com’); // warning email address
define(‘_timeout’,2); // fsockopen timeout in seconds
define(‘_max_timeout’,5); // max fsockopen timeout loops
define(‘_dt’,”Y-m-d \@ H:i:s”); // date/time format
$p=array( // Servers to Check – Server Name , IP/Hostname , Port
array(‘Server Name 1 WWW’,'1.2.3.4′,’80′),
array(‘Server Name 1 FTP’,'1.2.3.4′,’21′),
array(‘Server Name 1 ParaChat’,'1.2.3.4′,’7777′)
);
function f($x) { return number_format($x,0,’.',’,'); }
function a($t) { if ($t > 1000) { if ($t > 1000000) { if ($t > 1000000000) { if ($t > 1000000000000) { if ($t > 1000000000000000) { if ($t > 1000000000000000000) { if ($t > 1000000000000000000000) { $u=”o”;$t=$t/1000000000000000000000; } else { $u=”E”;$t=$t/1000000000000000000; } } else { $u=”P”;$t=$t/1000000000000000; } } else { $u=”T”;$t=$t/1000000000000; } } else { $u=”G”;$t=$t/1000000000; } } else { $u=”M”;$t=$t/1000000; } } else { $u=”K”;$t=$t/1000; } } else { $u=”"; } return round($t,3).$u; }
function p($i,$p,$t=5) {
$f=@fsockopen($i,$p,$e,$s,$t); // ip/hostname, port, error number, error description, timeout (sec)
@fclose($f);
if(!$f) {
if (is_debug) { echo “\n[ERROR] “.$e.”: “.$s.”\n”; }
return 0;
} else {
return 1;
}
}
echo “(c) z3n – R1V1@081114 – Ping by Port Check\n”;
for ($i=$b=$o=0,$k=count($p),$u=array_fill(0,$k,0);0 < 1;$i++) {
echo “\r[".date(_dt)."] L:”.a($i).” G:”.a($i-$b).” B:”.f($b).” T:”.a($o); // Loops , Good , Bad , Timeout
for ($j=0;$j<$k;$j++) {
if (!p($p[$j][1],$p[$j][2],_timeout)) {
$u[$j]++;$o++;
if ($u[$j] == _max_timeout) {
$m=”[".date(_dt).":".$i."@".$u[$j].”] “.$p[$j][0].”: (“.$p[$j][1].”:”.$p[$j][2].”) Down”;
if (w_sw) { mail(_e,$p[$j][0].” is Down”,$m,”From: “._e); }
echo “\n”.$m.”\n”;$b++;
}
} else {
if ($u[$j] >= _max_timeout) {
$m=”[".date(_dt).":".$i."@".$u[$j].”] “.$p[$j][0].”: (“.$p[$j][1].”:”.$p[$j][2].”) Online”;
if (w_sw) { mail(_e,$p[$j][0].” Back Online”,$m,”From: “._e); }
echo “\n”.$m.”\n”;$b–;
}
$u[$j]=0;
}
}
}
?>