blob: 8a6f2bb3d7477e8395b98aea89a906e0c4ddb9c3 [file] [log] [blame]
<?php
/********************************
* Name: get_hour_offset
* Function: since the time via update on the nodes is not 24hour it needs to be adjusted
* for processing in the following special cases: if it's morning and the hour is 12 then the
* the result needs to be 0. Also if it's afternoon/evening and the hour is NOT 12, then the result
* needs to be +12.
*
* I/O: takes the current hour(from the data not local time) and the minutes(which has am/pm stuck on)
* returns the modifier(0(default(AM)), -12(12am) or +12(!12PM))
*
*
*/
function get_hour_offset( $hour, $indicator ){
//is it 12 am?
if( ( $hour == 12) && ( preg_match('/am/i',$indicator) == 1) ){
return -12;
}
//how about some time after lunch?
else if( ( $hour != 12) && ( preg_match('/pm/i',$indicator) == 1) ){
return 12;
} else {
return 0;
}
}
define('DEBUG',0);
define('SPACE',3.5);
define('YOFFSET',179);
define('XOFFSET', 40);
define('FONT', 3);
define('BARWIDTH', 3.29);
//this is the server we want to draw for(full path)
$serv = $_REQUEST['server'];
//this allows us to use this code to draw the current,last5 and last15(1,2,3)
//$resolution = $_REQUEST['resolution'];
//check that hte request is a number, if it isn't then set it to 0
//if( is_numeric($resolution) === FALSE)
$resolution = 2;
//check to see if someone passed in a request for a specific day
if( isset($_REQUEST['day']) && $_REQUEST['day'] != '' && is_numeric($_REQUEST['day']) && $_REQUEST['day'] >= 0 && $_REQUEST['day'] <= 31 ){
$today = $_REQUEST['day'];
}else{ //if whatever the passed failed the above get the current day so that we can open the files.
$today = date ( 'd' );
}
//check to see if someone passed in a request for a specific month
if( isset($_REQUEST['month']) && $_REQUEST['month'] != '' && is_numeric($_REQUEST['month']) && $_REQUEST['month'] >= 0 && $_REQUEST['month'] <= 12 ){
$month = $_REQUEST['month'];
}else{ //if whatever the passed failed the above get the current month so that we can open the files.
$month = date ('n');
}
//check to see if someone passed in a request for a specific year
if( isset($_REQUEST['year']) && $_REQUEST['year'] != '' && is_numeric($_REQUEST['year']) && $_REQUEST['year'] >= 2007 && $_REQUEST['year'] <= 3000 ){
$year = $_REQUEST['year'];
}else{ //if whatever the passed failed the above get the current year so that we can open the files.
$year = date ('Y');
}
//get the Current month 3 letter short form
$month_name = date( 'M', mktime( 0,0,0,$month,$today,$year));
//break down the request string so that the name can be extracted
$name = explode('/',$serv);
//open the data file for today.
$data = @file( $serv . "/" . $year . "/" . $month_name . "/loadavgfor." . $today);
#echo "walla: " . $data;
if ( $data !== false ){
//set the start point in so that ther
$x = XOFFSET+1;
header("Content-type: image/png");
$im = imagecreate(1000, 200);
//now compute the y offset based on the font height
$fonthieght = (imagefontheight( FONT ))/2;
//Allocate colors
$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 240, 70, 70);
$blue = imagecolorallocate($im, 70, 70, 240);
$green = imagecolorallocate($im, 70, 240, 70);
$black = imagecolorallocate($im, 0, 0, 0);
// Background
imagefilledrectangle($im, 0, 0, imagesx($im), imagesy($im), $white);
// Title
$title = "Load for: " . end($name) . " on " . $month_name . " " . $today . " " . $year;
//compute the middle of the image(i know it should be 'static')
$title_x = (imagesx($im)/2 - 40) - (strlen($title)) ;
//now compute the y offset based on the font height
$title_y = ($fonthieght - 4);
imagestring($im, FONT, $title_x, $title_y, $title, $black);
$index = 0;
$maxload= 0.1;
$minload= 10000;
foreach( $data as $line ) {
//list( $time,$current,$lastfive,$lastonefive) = explode(',',$line);
list( $load_data[$index][0],$load_data[$index][1],$load_data[$index][2],$load_data[$index][3]) = explode(',',rtrim($line));
$maxload = ($load_data[$index][$resolution] < $maxload) ? $maxload : $load_data[$index][$resolution];
$minload = ($load_data[$index][$resolution] > $minload) ? $minload : $load_data[$index][$resolution];
$index=$index+1;
}// Draw time
$title_y = 190 - $fonthieght ;
$Start = $load_data[0][0];
$Stop = $load_data[$index-1][0];
//this is the 'start'
list($hour,$minutes) = explode(':',$Start);
$title_x = XOFFSET + ((($hour+get_hour_offset($hour,$minutes)) * 12) + ( $minutes/5)) * BARWIDTH;
imagestring($im, FONT, $title_x, $title_y ,$Start,$black);
//this is the 'stop'
list($hour,$minutes) = explode(':',$Stop);
$title_x = XOFFSET + ((($hour+get_hour_offset($hour,$minutes)) * 12) + ( $minutes/5)) * BARWIDTH;
imagestring($im, FONT, $title_x, $title_y ,$Stop,$black);
//now one to seperate the scale
imageline($im, XOFFSET-1, 10, XOFFSET-1, 180, $black);
//now work out where the end of the graph will be
$title_x = XOFFSET + ( 24 * 12 ) * BARWIDTH;
//now a line to mark the right side end
imageline($im, $title_x, 10, $title_x, 180, $black);
//draw a base line to seperate the lower text
imageline($im, 40, 180, $title_x, 180, $black);
//now ticks to mark each hour
for( $loop = 1; $loop <=24; $loop++){
$datax = XOFFSET + ( $loop * 12 ) * BARWIDTH;
imageline($im, $datax, 175, $datax, 185, $black);
//mark every 3 hours
if ( ( $loop % 3 ) == 0 )
if( $loop > 12)
imagestring($im, FONT, $datax, (190-$fonthieght), $loop-12, $black);
else
imagestring($im, FONT, $datax, (190-$fonthieght), $loop, $black);
}
//change the processing based on resolution( 0 is current, 1 is last five and 2 is last 15)
if( $resolution == 1 ){
$color = $green;
}else if( $resolution == 2 ){
$color = $blue;
}else if( $resolution == 3 ){
$color = $red;
}
//compute the offset mult(dynamically adjust the hieghts of the bars)
$mult = (YOFFSET - 15)/$maxload;
$index = 0;
foreach( $load_data as $load){
//find out where the hell this item goes
//first we explode the hours and minutes out of the time stamp
// the hours * 12 * BARWIDTH are where this bar should go.
//but the fine tune is all that + ((minutes/5) * BARWIDTH
//catch is minute values that fall out of mod 5 will get in the way so we will
//use them to control wether or not we process this value
list($hour,$minutes) = explode(':',$load[0]);
if ( ( $minutes % 5 ) == 0) {
$datax = XOFFSET + ((($hour+get_hour_offset($hour,$minutes)) * 12) + ( $minutes/5) ) * BARWIDTH;
//testing local $datax = (XOFFSET + 1) + ( ( $hour * 12 ) + ( $minutes/5) ) * BARWIDTH;
$load_y = YOFFSET - ( $load[$resolution] * $mult);
imagefilledrectangle($im, $datax, $load_y, $datax+BARWIDTH, YOFFSET, $color);
if( DEBUG == 1 )
imagestring($im, FONT, $datax, $minutes, get_hour_offset($hour,$minutes), $black);
}
}
//display the max value
$max_y = (YOFFSET - ($maxload * $mult )) - $fonthieght;
imagestring($im, FONT, 0, $max_y, $maxload, $red);
//now for a marker line
$max_y = $max_y + $fonthieght;
imageline($im, 40, $max_y, $datax,$max_y, $black);
//now the min value
$min_y = (YOFFSET - ($minload * $mult )) - $fonthieght;
imagestring($im, FONT, 0, $min_y, $minload, $green);//another marker line
$min_y = $min_y + $fonthieght;
imageline($im, 40, $min_y, $datax, $min_y, $black);
//draw some ticks for the load scale
$loadscale = $maxload /5;
for( $mark = 1; $mark <= 4; $mark++) {
$scale =YOFFSET -( $loadscale * $mult * $mark);
if ( ( $scale > ($max_y - ($fonthieght*2)) ) && ( $scale < ($max_y + ($fonthieght*2)) ) ){
continue;
}
else if ( ( $scale > ($min_y - ($fonthieght*2)) ) && ( $scale < ($min_y + ($fonthieght*2)) ) ){
continue;
}
imageline($im, 35, $scale, $datax, $scale, $black);
imagestring($im, FONT, 0, ($scale- $fonthieght), round( ($loadscale * $mark), 2), $black);
}
// Display image
imagepng($im);
// Release allocated resources
imagedestroy($im);
} else {
header("Content-type: image/png");
$im = imagecreate(800, 200);
//now compute the y offset based on the font height
$fonthieght = (imagefontheight( FONT ))/2;
//Allocate colors
$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 240, 70, 70);
// Background
imagefilledrectangle($im, 0, 0, imagesx($im), imagesy($im), $white);
// Title
$title = "Unable to find the requested data for: " . $month_name . " " . $today . " " . $year;
//compute the middle of the image(i know it should be 'static')
$title_x = (imagesx($im)/2 ) - (strlen($title)) ;
//now compute the y offset based on the font height
$title_y = (imagesy($im)/2 ) - ($fonthieght - 4);
imagestring($im, FONT, $title_x, $title_y, $title, $red);
// Display image
imagepng($im);
// Release allocated resources
imagedestroy($im);
}
?>