| <?php |
| |
| define('DEBUG',0); |
| define('SPACE',3.5); |
| define('YOFFSET',179); |
| define('XOFFSET', 40); |
| define('FONT', 3); |
| define('BARWIDTH', 79); |
| //this is the server we want to draw for(full path) |
| $serv = $_REQUEST['server']; |
| //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'); |
| } |
| //break down the request string so that the name can be extracted |
| $name = explode('/',$serv); |
| |
| 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) . " over the last 12 months of $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; |
| $minload= 10000; |
| |
| for($monthloop=1; $monthloop <= 12; $monthloop++) { |
| $month_name = date( 'M', mktime( 0,0,0,$monthloop,1,$year)); |
| // open the data file for today. |
| $data = @file( $serv ."/" . $year . "/" . $month_name . "/loadavgfor." . $month_name); |
| if ( $data !== false ){ |
| $monthavg[$monthloop] = $data[0]; |
| } else { |
| $monthavg[$monthloop] = 0; |
| } |
| // $textx = XOFFSET + $monthloop * BARWIDTH; |
| // imagestring($im, FONT, $textx, 50, $monthavg[$monthloop], $black); |
| $maxload = ( $monthavg[$monthloop] < $maxload) ? $maxload : $monthavg[$monthloop]; |
| if ( $monthavg[$monthloop] != 0 ) |
| $minload = ( $monthavg[$monthloop] > $minload) ? $minload : $monthavg[$monthloop]; |
| } |
| //clean up max vaule |
| $maxload = round( $maxload, 2); |
| $minload = round( $minload, 2); |
| $mult = (YOFFSET - 15)/$maxload; |
| //now compute the y offset based on the font height |
| $title_y = YOFFSET + $fonthieght; |
| |
| for( $monthloop=1; $monthloop <= 12; $monthloop++) { |
| //find out where the hell this item goes |
| $datax = XOFFSET + (($monthloop-1) * BARWIDTH ); |
| $load_y = YOFFSET - ( $monthavg[$monthloop] * $mult); |
| imagefilledrectangle($im, $datax, $load_y, $datax+BARWIDTH, YOFFSET, $blue); |
| //now write the month |
| $month_name = date( 'M', mktime( 0,0,0,$monthloop,1,$year)); |
| $title_x = $datax + 36.5; |
| imagestring($im, FONT, $title_x, $title_y, $month_name, $black); |
| } |
| //now work out where the end of the graph will be |
| $title_x = XOFFSET + 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); |
| //display the max value |
| $max_y = (YOFFSET - ($maxload * $mult )) - $fonthieght; |
| imagestring($im, FONT, 0, $max_y, $maxload, $red); |
| $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); |
| |
| ?> |