Código PHP:
<?php
//
// Version 1.0.0, Junio 2005, kernel AT elpombo DOT com
//
//---------------------------------------------------------------------------------------------------
// definiciones
error_reporting( E_ALL ^ (E_NOTICE | E_WARNING) );
ignore_user_abort( 1 );
@set_time_limit( 0 );
define( 'MAGIC_BEG', '__ki78FEm__' );
define( 'MAGIC_END', '__G3uuFT9__' );
define( 'ABORT_SCRIPT', 'ABORT_SCRIPT' );
define( 'SCRIPT_RUNNING', 'SCRIPT_RUNNING' );
define( 'GET_TIMEOUT', 30 );
define( 'LOG_CADA', (24*60*60) ); // nuevo log cada 24 horas
//---------------------------------------------------------------------------------------------------
// funciones
function split_campos( $todos ) {
global $num_campos, $primer_campo, $segundo_campo, $siguientes_campos, $ultimo_campo;
$beg = strpos($todos, MAGIC_BEG);
if ($beg === false) {
return false;
}
$end = strpos($todos, MAGIC_END);
if ($end === false) {
return false;
}
list( $primer_campo, $campos ) = my_split( substr( $todos, $beg+12 , ($end-$beg)-13 ) );
$num_campos = count( $campos ) + 1;
if( $num_campos ) {
$segundo_campo = $campos[0];
$ultimo_campo = $campos[count( $campos ) - 1];
} else {
$segundo_campo = "";
$ultimo_campo = "";
}
$siguientes_campos = implode( ',', $campos );
return true;
}
function my_split( $todos ) {
$loscampos = explode( ',', $todos );
$primero = array_shift( $loscampos );
return array( $primero, $loscampos );
}
function micro_time() {
$temp = explode(" ", microtime());
return bcadd($temp[0], $temp[1], 6);
}
// desde el server a GMT_local (España == +1)
function GetTZOffset( $GMT_local ) {
$Offset = date("O", 0);
$Parity = $Offset < 0 ? -1 : 1;
$Offset = $Parity * $Offset;
$Offset = ($Offset - ($Offset % 100))/100*60 + $Offset % 100;
$TZOffset = $Parity * $Offset;
$TZOffset = (0 - ($TZOffset*60)) + ($GMT_local * 3600);
return $TZOffset;
}
function Get_HTTP( $url, $ua_string, $cookie_string, $get_timeout ) {
global $resp_sevr, $response, $errno, $errstr;
$response = "";
$resp_sevr = 0; # no errors
$s_Complete = parse_url($url);
$s_Host = $s_Complete["host"];
$s_Port = 80;
$s_URI = $s_Complete["path"];
$fp = @fsockopen($s_Host, $s_Port, $errno, $errstr, 10);
if ( $fp ) {
$request = "GET $s_URI HTTP/1.1\r\n";
$request .= "Host: $s_Host\r\n";
$request .= "Accept: */*\r\n";
$request .= "Accept-Language: es\r\n";
$request .= "Connection: Close\r\n";
$request .= "User-Agent: $ua_string\r\n";
$request .= "Cookie: $cookie_string\r\n";
$request .= "\r\n";
fputs($fp, $request);
$query_timeout = 4;
stream_set_blocking( $fp, FALSE );
stream_set_timeout( $fp, $query_timeout );
$loop_time = time();
$status = socket_get_status( $fp );
while( !feof($fp) && !$status['timed_out'] ) {
$newtext = @fread( $fp, 10000 );
$length = strlen( $newtext );
$response .= $newtext;
$diff = time() - $loop_time;
if( $diff > $get_timeout ) {
$resp_sevr = 2; # error
break;
}
$status = socket_get_status( $fp );
}
fclose( $fp );
}
else {
$resp_sevr = 1; # error
}
return $resp_sevr;
}
function Crea_el_log_e_initz_variables() {
global $fn_log, $hay_log;
global $rounds, $outages, $downloaded, $start_time;
global $cnt_RUNNING, $cnt_OUTAGE_Unable, $cnt_OUTAGE_GET_Timeout, $cnt_OUTAGE_MySQL;
// Crea el log
$tz = time() + GetTZOffset( '+1' ); // +1 porque ES es GMT+1
$fn = strftime( "%d.%m.%Y.%H.%M.%S", $tz );
$fn_log = $fn . ".txt";
for(;;) {
if( !file_exists( $fn_log ) )
break;
$fn .= $fn . "1";
$fn_log = $fn . ".txt";
}
$f_id = fopen( $fn_log, "w" );
if( !$f_id )
$hay_log = false;
else {
$hay_log = true;
$str = "Site Monitor";
echo $str . "<br>";
fwrite( $f_id, $str . "\r\n" );
$str = "Host: http://domain.com/foro/index.php";
echo $str . "<br>";
fwrite( $f_id, $str . "\r\n" );
$str = "Check Period: 1 minute";
echo $str . "<br>";
fwrite( $f_id, $str . "\r\n" );
$str = "";
echo $str . "<br>";
fwrite( $f_id, $str . "\r\n" );
$tz = time() + GetTZOffset( '+1' ); // +1 porque ES es GMT+1;
$str = "START " . strftime( "%d/%m/%Y %H:%M:%S", $tz );
echo $str . "<br>";
fwrite( $f_id, $str . "\r\n" );
fclose( $f_id );
}
$rounds = 0;
$outages = 0;
$downloaded = 0;
$cnt_RUNNING++;
$cnt_OUTAGE_Unable++;
$cnt_OUTAGE_GET_Timeout++;
$cnt_OUTAGE_MySQL++;
$start_time = time();
}
function Close_log() {
global $fn_log, $hay_log;
global $rounds, $outages, $downloaded, $start_time;
global $cnt_RUNNING, $cnt_OUTAGE_Unable, $cnt_OUTAGE_GET_Timeout, $cnt_OUTAGE_MySQL;
$monitor_time = time() - $start_time;
$monitor_time = str_pad((floor($monitor_time/86400)),2,"0",STR_PAD_LEFT)."/"
.str_pad(floor(($monitor_time-(floor($monitor_time/86400)*86400))/3600),2,"0",STR_PAD_LEFT).":"
.str_pad(floor(($monitor_time-(floor($monitor_time/3600)*3600))/60),2,"0",STR_PAD_LEFT).":"
.str_pad(floor($monitor_time-(floor($monitor_time/60))*60),2,"0",STR_PAD_LEFT);
$tz = time() + GetTZOffset( '+1' ); // +1 porque ES es GMT+1;
$log_str = "STOP " . strftime( "%d/%m/%Y %H:%M:%S", $tz ) . " (" . $monitor_time . ")\r\n";
if( $hay_log ) {
$f_id = fopen( $fn_log, "a" );
fwrite( $f_id, $log_str );
fclose( $f_id );
}
if( connection_status()== 0 ) {
echo "$log_str<br><br><br>";
ob_flush();
flush();
}
}
//------------------------------------------------------------------------------------------
// main
if( file_exists( SCRIPT_RUNNING ) )
exit; // ignora la re-entrada
else
mkdir( SCRIPT_RUNNING, 0775 );
if( file_exists( ABORT_SCRIPT ) )
exit; // ignora la re-entrada
Crea_el_log_e_initz_variables();
for(;;) { // funciona en bucle infinito
$t1 = micro_time();
$rounds++;
$url = "http://domain.com/index.php";
$ua_string = "Site Monitor (round: " . $rounds . " outages: " . $outages . ")";
$cookie_string = "bblastvisit=0000000000; bbpassword=00000000000000000000000000000000; bbuserid=00000; bblastactivity=0000000000";
Get_HTTP( $url, $ua_string, $cookie_string, GET_TIMEOUT );
$t2 = micro_time();
$this_round = bcsub( $t2, $t1, 6 );
$downloaded += strlen($response);
$t_now = time();
$monitor_time = $t_now - $start_time;
$monitor_time = str_pad((floor($monitor_time/86400)),2,"0",STR_PAD_LEFT)."/"
.str_pad(floor(($monitor_time-(floor($monitor_time/86400)*86400))/3600),2,"0",STR_PAD_LEFT).":"
.str_pad(floor(($monitor_time-(floor($monitor_time/3600)*3600))/60),2,"0",STR_PAD_LEFT).":"
.str_pad(floor($monitor_time-(floor($monitor_time/60))*60),2,"0",STR_PAD_LEFT);
$server_load = 0;
$fyh_round = 0;
if( !$resp_sevr ) {
if( !split_campos( $response ) ) {
$resp_sevr = 3; // MySQL server has gone away
}
else {
$server_load = $segundo_campo /= 100;
$fyh_round = $ultimo_campo;
}
}
if( $resp_sevr ) {
$outages++;
}
$log_str = "$rounds, ";
switch( $resp_sevr ) {
case 0:
$log_str .= "RUNNING, Server Running, ";
$cnt_RUNNING++;
break;
case 1:
$log_str .= "OUTAGE, Unable to Connect, ";
$cnt_OUTAGE_Unable++;
break;
case 2:
$log_str .= "OUTAGE, GET Timeout, ";
$cnt_OUTAGE_GET_Timeout++;
break;
case 3:
$log_str .= "OUTAGE, MySQL server has gone away, ";
$cnt_OUTAGE_MySQL++;
break;
default:
$log_str .= "Oops, Oops, ";
}
if( $server_load ) {
$serv_load = explode( '.', $server_load );
if( !$serv_load[1] ) $serv_load[1] = "00";
else if( $serv_load[1] < 10 ) $serv_load[1] *= 10;
}
else {
$serv_load[0] = "0";
$serv_load[1] = "00";
}
if( $fyh_round ) {
$tz = $fyh_round + GetTZOffset( '+1' ); // +1 porque ES es GMT+1
$str_fyh_round = strftime( "%d/%m/%Y %H:%M:%S", $tz );
}
else {
$str_fyh_round = "00/00/0000 00:00:00";
}
$tz = time() + GetTZOffset( '+1' ); // +1 porque ES es GMT+1;
$tl = strftime( "%d/%m/%Y %H:%M:%S", $tz );
$downtime = (( 100000 * $outages ) / $rounds) / 1000;
$downtime = bcsub( $downtime, 0, 3 );
$uptime = 100 - $downtime;
$uptime = bcsub( $uptime, 0, 3 );
$kbdl = $downloaded / 1000;
$kbdl = bcsub( $kbdl, 0, 0 );
$thr = bcsub( $this_round, 0, 3 );
$log_str .= "$tl, $serv_load[0].$serv_load[1], $thr, $kbdl, $monitor_time, $outages, $uptime, $downtime";
if( $hay_log ) {
$f_id = fopen( $fn_log, "a" );
fwrite( $f_id, $log_str . "\r\n" );
fclose( $f_id );
}
if( connection_status()== 0 ) {
echo "$log_str<br>";
ob_flush();
flush();
}
if( file_exists( ABORT_SCRIPT ) ) {
@rmdir( ABORT_SCRIPT );
@rmdir( SCRIPT_RUNNING );
Close_log();
die();
}
if( (time() - $start_time) >= LOG_CADA ) {
Close_log();
$t2 = micro_time();
$this_round = bcsub( $t2, $t1, 0 );
if( $this_round < 60 ) {
sleep( 60 - $this_round ); // cada minuto
}
Crea_el_log_e_initz_variables();
}
else {
$t2 = micro_time();
$this_round = bcsub( $t2, $t1, 0 );
if( $this_round < 60 ) {
sleep( 60 - $this_round ); // cada minuto
}
}
}
//---------------------------------------------------------------------------------------------------
// END
?>