blob: 73976e6e2a7c22d9206bed2533797341f38d573f [file] [log] [blame]
<?php
/**************************************************
*
* Name: computeyearly
* Function: this is run from cron once per day and generates the currently daily average and then adds that to
* the store value and averages the two. Once it has teh new average it writes that to the file.
*
* I/O: reads and writes the the outputfile(loadavgfor.monthname), and reads from the other loadavg files.
*
* By: M. Ward
* Date: May 30/06
*
**************************************************/
//time for some defines
define('OUTFILEPREFIX','/loadavgfor.');
define('OUTPATH','/home/data/common/monitor/loadstats/');
define('MONTHNUMBER',date('n'));
define('MONTHNAME',date('M'));
define('TODAY', date('d'));
/*************************
*
* Name: runcatchup
* Function: this function 'catches' up to where we are in the month by processing all of the data available
* upto today, and returns the computed average.
*
* I/O: returns the computed average
*
*************************/
function runcatchup($hostname) {
$monthavg=0;
for( $day = 1; $day < TODAY; $day++) {
$monthavg=$monthavg+getdayavg(OUTPATH . $hostname . "/" . MONTHNAME . OUTFILEPREFIX . $day );
}
return round($monthavg/TODAY, 2);
}
/*************************
*
* Name: getdayavg
* function: given the name of a file it reads all of the available data contained within and
* computes the average for that day. If the file doesn't exist it returns 0
*
* I/O: takes the name of the file to be processed, returns the average or 0.
*
**************************/
function getdayavg($filename) {
$filedata = @file( $filename );
#echo "walla: " . $data;
if ( $filedata !== false ){
$count = 0;
$average = 0;
foreach( $filedata as $line ) {
list( $time,$current,$lastfive,$lastfifteen) = explode(',',rtrim($line));
$average=$average+$lastfive;
$count=$count+1;
}
return round($average/$count,2);
} else {
return 0;
}
}
//start the main processing loop
$sysname = php_uname('n');
$currentavg = 0;
//check to see if we have an output file already
if( is_file(OUTPATH . $sysname ."/" . MONTHNAME . OUTFILEPREFIX . MONTHNAME ) == FALSE ){
$currentavg = runcatchup($sysname);
} else {
//read the current data
$filedata = @file( OUTPATH . $sysname ."/" . MONTHNAME . OUTFILEPREFIX . MONTHNAME);
$currentavg = rtrim($filedata[0]);
}
//write to the file
$fh = fopen( OUTPATH . $sysname ."/" . MONTHNAME . OUTFILEPREFIX . MONTHNAME, "w+" );
//get todays average
$currentavg = round( ($currentavg + getdayavg(OUTPATH . $sysname ."/" . MONTHNAME . OUTFILEPREFIX . TODAY))/2, 2);
if (fwrite($fh, $currentavg) === false) {
echo "Cannot write to file \n\r";
}
fclose($fh);
?>