Foros del Web » Programando para Internet » PHP »

Mi script de Server Status

Estas en el tema de Mi script de Server Status en el foro de PHP en Foros del Web. 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 (  ...
  #1 (permalink)  
Antiguo 31/10/2005, 15:43
 
Fecha de Ingreso: agosto-2005
Mensajes: 109
Antigüedad: 21 años, 1 mes
Puntos: 0
Mi script de Server Status

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
?>
  #2 (permalink)  
Antiguo 31/10/2005, 15:44
 
Fecha de Ingreso: agosto-2005
Mensajes: 109
Antigüedad: 21 años, 1 mes
Puntos: 0
Hola,

Como veo que últimamente este tema ha salido en un par de ocasiones, este es mi script de Server Status, Site monitor o como quiera ser llamado.

Viendo el source se ve claramente qué es lo que hace, pero básicamente se trata de traerse el 'index.php' del objetivo. Para monitorizar el servidor mySQL del objetivo, en el index está implementada una línea que muestra, entre otras cosas, los valores de Server Load y un simple contador incremental que reside en una base de datos cualquiera.

El script se lanza con el navegador y funciona en modo 'free running': el cierre del navegador no lo aborta, para abortarlo se crea una carpeta con un nombre específico.

El script monitoriza el objetivo cada minuto, y lleva funcionando ininterrumpidamente más de cuatro meses.

Sus complementos son un 'log viewer', un 'site stats' que interpreta y sumariza los logs, y un 'supervisor' del script, que se encarga de 'resucitarlo' en las contadas ocasiones en las que se ha reinicido el server que lo aloja... pero esos complementos son otra historia.


Un saludo.



El log que genera se interpreta así:

Código:
   START 22/07/2005 02:30:27  ----> (DD/MM/YY HH:MM:SS)
   [...]
   24, RUNNING, Server Running, 22/07/2005 02:53:38, 0.62, 1.342, 1357, 00/00:23:10, 0, 100.000, 0.000
   --  -------  --------------  -------------------  ----  -----  --    -----------  -  -------  -----
   |   |        |               |                    |     |      |     |            |  |        |
   |   |        |               |                    |     |      |     |            |  |        |
   |   |        |               |                    |     |      |     |            |  |        ----> DownTime (%)
   |   |        |               |                    |     |      |     |            |  ----> UpTime (%)
   |   |        |               |                    |     |      |     |            ----> Outages (#)
   |   |        |               |                    |     |      |     ----> Monitor Elapsed Time (DD/HH:MM:SS)
   |   |        |               |                    |     |      ----> Total Downloaded (Kb)
   |   |        |               |                    |     ----> Page Download Time (seconds)
   |   |        |               |                    ----> Host Server Load
   |   |        |               ---->  Round Date&Time (DD/MM/YY HH:MM:SS)
   |   |        ----> Brief Description
   |   ----> Host Status
   ----> Round (#)

y la definición de la info que suministra el target sobre él, es esta:

Código PHP:
//---------------------------------------------------------------------------------------------------
/*
    En 'global.php' del target:

        $monitor_info  = MAGIC_BEG;
        $monitor_info .= ",";
        $monitor_info .= $simple_counter;        //  extraido de la db
        $monitor_info .= ",";
        $monitor_info .= $serverload;
        $monitor_info .= ",";
        $monitor_info .= time();
        $monitor_info .= ",";
        $monitor_info .= MAGIC_END;

    En el template 'footer' del target:
    
        <!-- La info para el monitor: $monitor_info -->
        
    El resultado en el target:
    
        <!-- La info para el monitor: __ki78FEm__,235541,0.15,1130791487,__G3uuFT9__ -->

*/
//--------------------------------------------------------------------------------------------------- 
  #3 (permalink)  
Antiguo 01/11/2005, 05:19
 
Fecha de Ingreso: agosto-2005
Mensajes: 109
Antigüedad: 21 años, 1 mes
Puntos: 0
Hola,

En esta revisión simplemente he añadido que monitorize o no el estado del server mySQL del objetivo: MIRAR_mySQL true/false

Tal como está, al lanzarlo monitoriza http://www.google.com

Y poco más, creo que con esto cierro este asunto.

Un saludo.


La revisión v1.0.1, en el post que sigue, no cabe aquí.
  #4 (permalink)  
Antiguo 01/11/2005, 05:20
 
Fecha de Ingreso: agosto-2005
Mensajes: 109
Antigüedad: 21 años, 1 mes
Puntos: 0
Código PHP:
<?php
//
//  Version 1.0.1, Noviembre 2005, kernel AT elpombo DOT com
//        - Añadido:    MIRAR_mySQL        true/false
//
//  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
    
    
define( 'MIRAR_mySQL', false );
    
    
    
$objetivo          = "http://www.google.com/index.html";
    
$user_agent_string = "Site Monitor v1.0.1";
    
$cookie_string     = "";
    
//---------------------------------------------------------------------------------------------------
//  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;
        global    
$objetivo;

        
// 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: " . $objetivo;
            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 = $objetivo;
        
$ua_string = $user_agent_string;
        
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 ) ) {
                if( 
MIRAR_mySQL ) {
                    
$resp_sevr = 3;                                                    //  MySQL server has gone away
                
}
                else
                    
$server_load = 0;
                    
$fyh_round = 0;
            }
            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
?>

Última edición por Shockly; 01/11/2005 a las 05:28
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:58.