blob: 70ae7652178754340bd9f2ad33faa24bcb18de63 [file] [log] [blame]
<?php
define('DEBUG',0);
define('SPACE',3.5);
define('YOFFSET',179);
define('XOFFSET', 40);
define('CALXOFFSET', 9.36);
define('FONT', 3);
define('BARWIDTH', 30.64);
//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 month
if( isset($_REQUEST['month']) && $_REQUEST['month'] != '' && is_numeric($_REQUEST['month']) && $_REQUEST['month'] >= 1 && $_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,1,$year));
//break down the request string so that the name can be extracted
$name = explode('/',$serv);
//get the number of days in this month
$numberofdays = date('t',mktime(0,0,0,$month,1,$year));
header("Content-type: image/png");
$im = imagecreate(1000, 200);
//now compute the y offset based on the font height
$fonthieght = (imagefontheight( FONT ))/2;
$fontwidth = imagefontwidth( FONT );
//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) . " durring " . $month_name . " " . $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;
//check tha thte request month exists
$checkdir = $serv ."/" . $year ."/" . $month_name;
if( is_dir( $checkdir ) == true) {
if( DEBUG == 1) {
$msg="Dir exists!";
imagestring($im, FONT, 50, 20, $msg, $black);
}
//start the day processing loop
for( $days = 1; $days <= $numberofdays; $days++) {
// open the data file for today.
$day_code = date( 'd', mktime( 0,0,0,$month,$days,$year));
$data = @file( $serv ."/" . $year . "/" . $month_name . "/loadavgfor." . $day_code);
if ( $data !== false ){
$count = 0;
$total = 0;
foreach( $data as $line ) {
list( $load_data[$index][0],$load_data[$index][1],$load_data[$index][2],$load_data[$index][3]) = explode(',',rtrim($line));
$total = $total+ $load_data[$index][$resolution];
$count=$count+1;
}
//compute the average for this day
$dayavg[$days] = $total/$count;
} else {
$dayavg[$days] = 0;
}
$maxload = ($dayavg[$days] < $maxload) ? $maxload : $dayavg[$days];
if ( $dayavg[$days] != 0 )
$minload = ($dayavg[$days] > $minload) ? $minload : $dayavg[$days];
}
//clean up max vaule
$maxload = round( $maxload, 2);
$minload = round( $minload, 2);
$mult = (YOFFSET - 15)/$maxload;
$index = 1;
for( $days = 1; $days <= $numberofdays; $days++) {
//find out wher the hell this item goes
$datax = CALXOFFSET + $days * BARWIDTH;
$load_y = YOFFSET - ( $dayavg[$days] * $mult);
imagefilledrectangle($im, $datax, $load_y, $datax+BARWIDTH, YOFFSET, $blue);
$textx = $datax + (15-$fontwidth);
imagestring($im, FONT, $textx, 185, $days, $black);
if( DEBUG == 1 ){
imagestring($im, FONT, $datax, $minutes, $datax, $black);
}
}
//now work out where the end of the graph will be
$title_x = XOFFSET + ( $numberofdays ) * 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);
//display the max value
$max_y = (YOFFSET - ($maxload * $mult )) - $fonthieght;
imagestring($im, FONT, 0, $max_y, $maxload, $red);
//liine
$max_y = $max_y + $fonthieght;
imageline($im, 40, $max_y, $title_x,$max_y, $black);
//the min value
$min_y = (YOFFSET - ($minload * $mult )) - $fonthieght;
imagestring($im, FONT, 0, $min_y, $minload, $green);
$min_y = $min_y + $fonthieght;
imageline($im, 40, $min_y, $title_x,$min_y, $black);
//now one to seperate the scale
imageline($im, XOFFSET-1, 10, XOFFSET-1, 180, $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, 990, $scale, $black);
imagestring($im, FONT, 0, ($scale- $fonthieght), round( ($loadscale * $mark), 2), $black);
}
// Display image
imagepng($im);
// Release allocated resources
imagedestroy($im);
} else {
// Background
imagefilledrectangle($im, 0, 0, imagesx($im), imagesy($im), $white);
// Title
$title = "Unable to find the requested data for: " . $month_name . " " . $year;
//compute the middle of the image(i know it should be 'static')
$title_x = (imagesx($im)/2 ) - (strlen($title)+XOFFSET) ;
//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);
}
?>