<?php
function _pavg()
{
  global 
$i$c$t;

  
$avg sprintf('%.3f',$t/$c);
  
$loss sprintf('%d',($i-$c)*100/$i);

  echo 
"$i packets transmitted, $c received, ${loss}% packet loss, rtt avg = $avg ms\n";
  
//exit(0);
}

require_once 
'class.icmp.php';

## ICMP ECHO: measure of lost-time(seconds)
##

set_time_limit(0);

if(
preg_match('/WIN/i',PHP_OS)) error_reporting(E_ALL E_NOTICE);
else
{
  declare(
ticks 1);
  
## 2 3 5 15 19 --> kill, 1 --> HUP
  ##
  
pcntl_signal(SIGHUP,'_pavg'); // 1
  
pcntl_signal(SIGINT,'_pavg'); // 2, Ctrl+C
  
pcntl_signal(SIGQUIT,'_pavg'); // 3
  
pcntl_signal(SIGABRT,'_pavg'); // 6
  //pcntl_signal(SIGKILL,'_pavg'); // 9, it's fault
  //pcntl_signal(SIGUSR1,'_pavg'); // 10, does not work
  
pcntl_signal(SIGALRM,'_pavg'); // 14
  
pcntl_signal(SIGTERM,'_pavg'); // 15
  //pcntl_signal(SIGSTOP,'_pavg'); // 19, it's fault
}

if(!
$ip $_SERVER['argv'][1]) { echo 'type ip.address'; exit(1); }
if(
preg_match('/[a-z]/',$ip)) $ip gethostbyname($ip);

$i $t 0;
$c 0;
$e 1;
while(
TRUE)
{
  list(
$ms$error) = icmp::ping($ip);
  
/***
  if($error) echo "$ip seq:$i $error\n"; //$msg = $error;
  else
  {
    list($ms) = explode(' ',$ms);
    $msg = $ms.' ms';
    $t += $ms;
    $c++;
    _pavg();
  }
  //echo "$ip seq:$i $msg\n";
  ***/

  
if($error)
  {
    if(!
$etime$etime time();
  } else
  {
    if(
$etime)
    {
        
$dtime = (time() - $etime) . 'sec';
        
printf("%03d %s %s\n",$e,date('Y/m/d H:i'),$dtime);
        
$etime 0// reset
        
$e++;
    }
  }

  
$i++;
  
sleep(1);
}

exit(
0);
?>