1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
function imagickHisto ($max, $eth = 'eth0', $up_down = 'down') {
$datas = parseData ("/home/pi/ifstat/eth0.log", $up_down);
$width = 304;
$height = 100;
$padding = 1;
$ticks = 5;
$background_color = '#000';
$axes_color = '#555';
if($up_down == 'down'){
$data_color = '#1D1';
}
else{
$data_color = '#D11';
}
$nb_values = $width - 2*$padding - 2;
$max_value = $height - 2*$padding - 4;
$nb_datas = sizeof($datas);
$trim = $nb_values - $nb_datas;
if($trim < 0){$trim = 0;}
$image = new Imagick();
$image ->newImage( $width, $height, new ImagickPixel($background_color) );
$draw = new ImagickDraw();
$draw->setStrokeColor( new ImagickPixel($axes_color) );
$xx1 = $padding;
$xy1 = $height - $padding - 1;
$xx2 = $width - $padding - 1;
$xy2 = $xy1;
$yx1 = $xx1;
$yy1 = $xy1;
$yx2 = $yx1;
$yy2 = $padding;
$half_y = $height/2;
$half_x = $width/2;
$draw->line ( $xx1, $xy1, $xx2, $xy2 );
$draw->line ( $yx1, $yy1, $yx2, $yy2 );
$draw->line ( $yx1, $yy2, $yx1+$ticks, $yy2 );
$draw->line ( $yx1, $half_y, $yx1+$ticks, $half_y );
$draw->setStrokeColor( new ImagickPixel($data_color) );
$first_x = $xx1 + 1 + $trim;
$last_x = $xx2 - 1;
$first_y = $xy1 - 1;
$last_y = $yy2 + 1;
for($i=0;$i<$nb_values;$i++){
if(isset($datas[$i])){
$value = $datas[$i]*$max_value/$max;
$value_y = $first_y - $value;
$value_x = $first_x + $i;
$draw->line ( $value_x, $first_y, $value_x, $value_y );
}
}
$image->drawImage( $draw );
// TEXT
$text_draw = new ImagickDraw();
$text_draw->setFillColor($axes_color);
$text_draw->setFontSize( 12 );
$image->annotateImage($text_draw, $half_x-20, $padding+10, 0, "$eth - $up_down");
$image->setImageFormat( "png" );
header( "Content-Type: image/png" );
echo $image;
exit;
}
function parseData ($stat_file, $up_down) {
$datas = array();
if(filemtime($stat_file) < time()-10){return $datas;}
$stats = fopen($stat_file, 'r');
while (($line = fgets($stats)) !== false) {
$explode_line = str_word_count($line, 1, "0123456789.");
if($up_down == 'down') {
$datas[] = $explode_line[0];
}
else{
$datas[] = $explode_line[1];
}
}
fclose($stats);
$datas = array_slice($datas, -300);
return $datas;
} |
Partager