I'll try that in another way; feel free to give me your hacks for it:
 
<?php
$file = file_get_contents( "./ip" );
$line = explode( "\n", $file );
 
## Number of log entries
$total = 0;
 
$unavail = 0;
$unavaillog = FALSE;
 
## Establish the standard difference between log entries of 5 minutes
$diff = 300;
## Add 5 because cron often is off by a couple of seconds
$maxdiff = $diff + 5;
 
 
$downtime = array(array());
 
Done the log parser:
 
## Count variable for iterations of downtime
$cntdown = 0;
 
foreach( $line as $val ) {
   $bits = explode( "\t", trim( $val ) );
 
      if ( $bits[0] && ( $bits[1] == "S" || $bits[1] == "U" ) ) {
         $ctime = $bits[0];
 
         if ( $total == 0 ) {
            $firsttime = $ctime;
            $ptime = $firsttime;
            }
 
 
         $diftime = $ctime - $ptime;
                         ## Calculate the amount of time the server has
been down.
         if( $diftime > $maxdiff ) {
            print $diftime ."\n";
            $missed = $diftime / $diff;
            $unavail += $missed;
            $total += $missed;
            if ( $unavailog ) { }
            else {
               $downtime[$cntdown][start]  = $ptime;
               $unavailog = TRUE;
               }
            }
 
         if( trim( $bits[1] ) == "U" ) {
            if ( $unavailog ) {
 
               }
            else {
               $downtime[$cntdown][start] = $ctime;
               $unavailog = TRUE;
               }
            $unavail++;
            }
         else {
         ## Once the down time has finished
            if ( $unavailog ) {
               $downtime[$cntdown][finish] = $ctime;
               $downtime[$cntdown][diff] = $downtime[$cntdown][finish] -
                                           $downtime[$cntdown][start];
               $cntdown++;
               }
            $unavailog = FALSE;
            }
 
         $total++;
         $ptime = $ctime;
         }
   }
 
## Last time is registered as the timestamp of the last entry
$lasttime = $ctime;
 
$firsttime = date( "j/n/y H:i", $firsttime );
$lasttime  = date( "j/n/y H:i", $lasttime );
 
## Make sure the percentage is uptime, one decimal place
$percentage = number_format( ( 100 - ( ( $unavail / $total ) * 100 ) ),
1 );
 
print "$percentage% uptime from $firsttime to $lasttime\n";
 
## Need to make this look pretty
print_r( $downtime );
?>
 
example log file:
1092528602       S
1092528901       S
1092529201       S
1092529502       S
1092530102       S
1092530401       S
1092530701       S
1092531601       S
1092531902       S
1092532201       S
1092532501       U

Reply via email to