download this file: run-tcpping.php view text/plain: run-tcpping.php file encoding: UTF-8 [goback]
<?php
##
##
## make current AVG MAX
##
function make_avg_mdev($rtt)
{
if(!$rtt) return array();
$pow = 0;
$size = sizeof($rtt);
foreach($rtt AS $ms) $pow += $ms*$ms;
$sum = array_sum($rtt);
$avg = sprintf('%.3f',$sum/$size);
$mdev = sqrt($pow/$size - pow($avg,2));
$mdev = sprintf('%.3f',$mdev);
return array($avg,$mdev);
}
function update_rtt_file($to, $rtts, $errs, $ctime)
{
if(!$rtts) return;
$to = str_replace(':','-',$to); // internal port
$file = "/dev/shm/tcpping-$to.rtt";
$btime1 = $ctime - 60; // last 1m
$btime5 = $ctime - 300; // last 5m
$last1m = $last5m = array();
foreach($rtts AS $utime=>$ms)
{
if($utime < $btime5) continue; // old rtt
$last5m[$utime] = $ms;
if($utime > $btime1) $last1m[] = $ms;
}
$newerrs = array();
foreach($errs AS $utime)
{
if($utime < $btime5) continue; // old err
$newerrs[] = $utime;
}
$err = sizeof($newerrs);
list($avg1m,$mdev1m) = make_avg_mdev($last1m); // current last 1m
list($avg5m,$mdev5m) = make_avg_mdev($last5m); // current last 5m
$data = time()." $avg1m $mdev1m $avg5m $mdev5m $err\n"; // current last 1m data
if(__DEBUG_UPDATE) echo "STATUS: $file $data";
if(__RUNMODE) file_put_contents($file,$data,LOCK_EX);
return array($last5m,$newerrs);
}
function usage()
{
$errstr = 'Usage: '.$_SERVER['argv'][0].' [-h|--help] [options] IP[:port]'."\n";
$errstr .= 'Options:'."\n";
$errstr .= ' --sleep secs : tcpping interval seconds'."\n";
$errstr .= ' --test : test mode(do not update rtt file), and debug mode'."\n";
$errstr .= ' --debug : debug mode, all print'."\n";
$errstr .= ' --debug-update : debug rtt file update'."\n";
$errstr .= ' --debug-error-only : debug mode, but only error print, if error.'."\n";
$errstr .= 'Note that default port is TCP/7'."\n";
//error_log($errstr,0);
tcpping::error_print($errstr);
return;
}
require_once 'class.socket.tcpping.php';
$_debug = FALSE;
$_debug_error_only = FALSE;
$_debug_update = FALSE;
$_usleep = 1000000;
$_runmode = TRUE; // update rtt file
$argc = $_SERVER['argc'];
for($i=1; $i<$argc; $i++)
{
$argv = $_SERVER['argv'][$i];
if(preg_match('/^--debug$/',$argv)) $_debug = TRUE;
else if(preg_match('/^--debug-error-only/',$argv)) $_debug_error_only = TRUE;
else if(preg_match('/^--debug-update/',$argv)) $_debug_update = TRUE;
else if(preg_match('/^--sleep/',$argv))
{
if(preg_match('/=/',$argv)) list(,$_sleep) = explode('=',$argv);
else $_sleep = $_SERVER['argv'][++$i];
}
else if(preg_match('/^--test/',$argv)) $_runmode = FALSE; // do not update rtt file
else if(preg_match('/^[\d.:]+$/',$argv)) $to = $argv;
else if(preg_match('/^--?h/',$argv))
{
usage();
exit(1);
}
}
if(!$to) { usage(); exit(1); }
if(!$_runmode) $_debug = TRUE; // force debug to active
if($_debug_error_only) $_debug = FALSE; // force debug to disable
define('__RUNMODE',$_runmode);
define('__DEBUG',$_debug);
define('__DEBUG_ERROR_ONLY',$_debug_error_only);
define('__DEBUG_UPDATE',$_debug|$_debug_update);
$rtts = array();
$errs = array();
$otime = $rtime = time();
$ms = 0;
$usleep = $_sleep ? ($_sleep*1000000) : $_usleep;
while(1)
{
if($ms > 0)
{
$sleepsec = $usleep - ($ms*1000);
if($sleepsec > 0) usleep($sleepsec);
}
else usleep($usleep);
list($ms,$msg,$ip) = tcpping::ping($to);
if(__DEBUG) printf("%s\t%.3f\t%s\t%s\n",date('Y-m-d H:i:s'),$ms,$msg,$ip);
$ctime = time();
if($ms < 0)
{
$errs[] = $ctime;
if(__DEBUG_ERROR_ONLY) printf("%s\t%.3f\t%s\t%s\n",date('Y-m-d H:i:s'),$ms,$msg,$ip);
}
else $rtts[$ctime] = $ms;
if(($ctime-$otime) >= 60)
{
if(__RUNMODE || __DEBUG_UPDATE) list($rtts,$errs) = update_rtt_file($to,$rtts,$errs,$ctime);
$otime = $ctime;
}
}
?>