Cria gráficos de consumo de CPU e Memória a partir de arquivos.

Publicado por Alessandro Dias 09/04/2007

[ Hits: 7.659 ]

Download graph.php




Cria gráficos de consumo de CPU e Memória a partir de arquivos.

Esses arquivos são gerados por shell script via SNMPv3 conforme publicado anteriormente.

Procure pelo script como "SNMPv3".

  



Esconder código-fonte

<?php
//------------------------------------------------------------------Mostra gráficos Diários
//@Alessandro Dias
//
// Tamanho do grafico
$alt=600;
$comp=1000;

// Cria Quadro
$image = @imagecreate($comp,$alt) or die("Erro na img GD");

// Cores
$white     = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray      = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$lightgray = imagecolorallocate($image, 0xD0, 0xD0, 0xD0);
$darkgray  = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy      = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy  = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red       = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$blue      = imagecolorallocate($image, 0x00, 0x00, 0xFF);
$green      = imagecolorallocate($image, 0x00, 0xFF, 0x00);
$darkgreen      = imagecolorallocate($image, 0x00, 0x88, 0x00);
$darkred   = imagecolorallocate($image, 0x90, 0x00, 0x00);
$black     = imagecolorallocate($image, 0x00, 0x00, 0x00);

//------------------------------------------------------------------- Funcoes
function escreve($image,$x,$y,$texto,$color) {
   imagestring($image, 2, $x+50, 550-$y, $texto, $color);
}

function ponto($image,$x,$y,$color) {
   imageellipse($image, $x+50,550-$y, 2, 2, $color); 
}

function plota($image,$x,$y,$color) {
   $lightgray = imagecolorallocate($image, 0xE5, 0xE5, 0xE5);
   ponto($image,$x,$y,$color);
   //imageline($image, $x+50, 550-$y, 50, 550-$y, $lightgray);
   imageline($image, $x+50, 550-$y, $x+50, 550, $lightgray);
}

function liga($image,$x1,$y1,$x2,$y2,$color) {
   imageline($image, $x1+50, 550-$y1, $x2+50, 550-$y2, $color);
}

function monta_grafico($image,$alt,$comp,$divx,$divy) {

   $navy      = imagecolorallocate($image, 0x00, 0x00, 0x80);
   $blue      = imagecolorallocate($image, 0x00, 0x00, 0xFF);
   $black     = imagecolorallocate($image, 0x00, 0x00, 0x00);
   $darkgray  = imagecolorallocate($image, 0x90, 0x90, 0x90);
   $red       = imagecolorallocate($image, 0xFF, 0x00, 0x00);
   $darkgreen      = imagecolorallocate($image, 0x00, 0x88, 0x00);
   
   // Nome do Grafico
   escreve($image,($comp-100)/2-130,($alt-60),"Estatistica Diaria de Consumo CPU/MEM", $navy);
   
   // Cria Eixos
   imageline($image, 50, 50, 50, $alt-50, $darkgray);
   imageline($image, 50, $alt-50, $comp-50, $alt-50, $darkgray);
   
   //Lê o range X
   $fp=fopen("rangex","r");
   $range = explode(",",fgets ($fp,4096));
   fclose($fp);

   $nusers=$range[2];
   $clicks=$range[3];
   $delay_user=$range[4];
   $delay_click=$range[5];
   $delay_user_rand=$range[6];
   $delay_click_rand=$range[7];
   $url=$range[8];
   $ip=$range[9];
   $data=trim($range[10]);
   
   // Monta Legenda   
   escreve($image,170,-25,"Server: ".$ip, $darkgray);
   escreve($image,320,-25,"Date: ".$data, $darkgray);
   escreve($image,500,-25,"Autor: Alessandro Dias", $darkgray);

   // Marca eixo X
   escreve($image,($comp-160),-18,"Time", $navy);
   $passo=($comp-100)/$divx;
   $x=0;
   for($i = 0; $x < ($comp-100); $i += $passo) {
      $x=$i;
      $y=0;
      ponto($image,$x,$y,$black);
      escreve($image,$x-5,$y-5,($i*$range[0]/($comp-100)),$black);
   }
   
   // Marca eixo Y
   escreve($image,-10,($alt-52),"CPU(%)", $red);
   escreve($image,-10,($alt-67),"MEM(%)", $blue);
   escreve($image,-10,($alt-82),"MEM-BUF(%)", $darkgreen);
   
   $passo=($alt-100)/$divy;
   $y=0;
   for ($i = 0; $y < ($alt-100); $i += $passo) {
      $x=0;
      $y=$i;
      ponto($image,$x,$y,$black);
      escreve($image,$x-20,$y+5,round($i/5),$black);
   }
}


function gera_grafico($image,$alt,$comp,$divx,$divy,$pt,$num,$color) {

   //lê o range X
   $fp=fopen("rangex","r");
   $range = explode(",",fgets ($fp,4096));
   fclose($fp);

   $black     = imagecolorallocate($image, 0x00, 0x00, 0x00);
   for($i = 1; $i < $num; $i++) {
     //plota 2 pontos
     $pt1x = round($pt[1][$i]*($comp-100)/($range[0]),1);
     $pt1y = round($pt[2][$i]*($alt-100)/($divy*10),1);
     $pt2x = round($pt[1][$i+1]*($comp-100)/($range[0]),1);
     $pt2y = round($pt[2][$i+1]*($alt-100)/($divy*10),1);
     plota($image,$pt1x,$pt1y,$black);
     plota($image,$pt2x,$pt2y,$black);
     //liga os pontos
     liga($image,$pt1x,$pt1y,$pt2x,$pt2y,$color);
     //escreve os valores de Y
     //if ($i == $num-1) {
       escreve($image,$pt1x-15,$pt1y+15,round($pt[2][$i],1),$color);
       escreve($image,$pt2x-15,$pt2y+15,round($pt[2][$i+1],1),$color); 
     //}
   }

}


// ------------------------------ Monta Grafico
monta_grafico($image,$alt,$comp,24,10);

// ------------------------------ Gera Pontos no Grafico

  $filename1="pts_cpu";
  $fp1=fopen($filename1,"r+");
  $filename2="pts_mem";
  $fp2=fopen($filename2,"r+");
  $filename3="pts_mem_buf";
  $fp3=fopen($filename3,"r+");

  $conteudo1 = fgets ($fp1,4096);
  $num=0;

while (!feof ($fp1)) {
  $array = explode(",",$conteudo1);
  $x=$array[0];
  $y=$array[1];

  $num = $num +1;

  $pt1[1][$num]=$x;
  $pt1[2][$num]=$y;

  $conteudo1 = fgets ($fp1,4096);
}

  $conteudo2 = fgets ($fp2,4096);
  $num2=0;

while (!feof ($fp2)) {
  $array = explode(",",$conteudo2);
  $x=$array[0];
  $y=$array[1];

  $num2 = $num2 +1;

  $pt2[1][$num2]=$x;
  $pt2[2][$num2]=$y;

  $conteudo2 = fgets ($fp2,4096);
}

  $conteudo3 = fgets ($fp3,4096);
  $num3=0;

while (!feof ($fp3)) {
  $array = explode(",",$conteudo3);
  $x=$array[0];
  $y=$array[1];

  $num3 = $num3 +1;

  $pt3[1][$num3]=$x;
  $pt3[2][$num3]=$y;

  $conteudo3 = fgets ($fp3,4096);
}

  fclose($fp1);
  fclose($fp2);
  fclose($fp3);


gera_grafico($image,$alt,$comp,12,10,$pt1,$num,$red);
gera_grafico($image,$alt,$comp,12,10,$pt2,$num2,$blue);
gera_grafico($image,$alt,$comp,12,10,$pt3,$num3,$darkgreen);

// ------------------------------

// output the picture
header("Content-type: image/png");
imagepng($image);
imagedestroy($im);
?>

<html>
<head><title>Stress Test Graphic</title></head>
<body>
       <FORM name=frm method=post enctype="multipart/form-data" action="index.php">
                <HR noShade>
          <TABLE border=0><TR><TD valign=top>
                <TABLE border=0>
                  <TBODY>
                  <TR>
                         <TD align=right>Range Time:
                         <TD><INPUT name=usr_range_time size=3 value="<?=$range_time?>" type=text> ms
                  </TR><TR>
                         <TD colspan=2><center><INPUT name=enviar type=submit value="Submit IP"></center>
                  </TR>
                </TABLE>
           </TD></TR></TABLE>
                <HR noShade>
        </FORM>
</body>
</html>

Scripts recomendados

Gerador de Thumbnails

Sistema Completo Imobiliaria

Sistema de Boletos e Cobrança Online

Monitoramento de processos - TOP

Agenda de site de Baladas


  

Comentários
[1] Comentário enviado por lemuelroberto em 16/04/2007 - 21:25h

link quebrado...

[2] Comentário enviado por JoseLucas em 19/04/2007 - 21:42h

ve o codigo fonte


<?php
//------------------------------------------------------------------Mostra gráficos Diários
//@Alessandro Dias
//
// Tamanho do grafico
$alt=600;
$comp=1000;

// Cria Quadro
$image = @imagecreate($comp,$alt) or die("Erro na img GD");

// Cores
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$lightgray = imagecolorallocate($image, 0xD0, 0xD0, 0xD0);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$blue = imagecolorallocate($image, 0x00, 0x00, 0xFF);
$green = imagecolorallocate($image, 0x00, 0xFF, 0x00);
$darkgreen = imagecolorallocate($image, 0x00, 0x88, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);

//------------------------------------------------------------------- Funcoes
function escreve($image,$x,$y,$texto,$color) {
imagestring($image, 2, $x+50, 550-$y, $texto, $color);
}

function ponto($image,$x,$y,$color) {
imageellipse($image, $x+50,550-$y, 2, 2, $color);
}

function plota($image,$x,$y,$color) {
$lightgray = imagecolorallocate($image, 0xE5, 0xE5, 0xE5);
ponto($image,$x,$y,$color);
//imageline($image, $x+50, 550-$y, 50, 550-$y, $lightgray);
imageline($image, $x+50, 550-$y, $x+50, 550, $lightgray);
}

function liga($image,$x1,$y1,$x2,$y2,$color) {
imageline($image, $x1+50, 550-$y1, $x2+50, 550-$y2, $color);
}

function monta_grafico($image,$alt,$comp,$divx,$divy) {

$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$blue = imagecolorallocate($image, 0x00, 0x00, 0xFF);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$darkgreen = imagecolorallocate($image, 0x00, 0x88, 0x00);

// Nome do Grafico
escreve($image,($comp-100)/2-130,($alt-60),"Estatistica Diaria de Consumo CPU/MEM", $navy);

// Cria Eixos
imageline($image, 50, 50, 50, $alt-50, $darkgray);
imageline($image, 50, $alt-50, $comp-50, $alt-50, $darkgray);

//Lê o range X
$fp=fopen("rangex","r");
$range = explode(",",fgets ($fp,4096));
fclose($fp);

$nusers=$range[2];
$clicks=$range[3];
$delay_user=$range[4];
$delay_click=$range[5];
$delay_user_rand=$range[6];
$delay_click_rand=$range[7];
$url=$range[8];
$ip=$range[9];
$data=trim($range[10]);

// Monta Legenda
escreve($image,170,-25,"Server: ".$ip, $darkgray);
escreve($image,320,-25,"Date: ".$data, $darkgray);
escreve($image,500,-25,"Autor: Alessandro Dias", $darkgray);

// Marca eixo X
escreve($image,($comp-160),-18,"Time", $navy);
$passo=($comp-100)/$divx;
$x=0;
for($i = 0; $x < ($comp-100); $i += $passo) {
$x=$i;
$y=0;
ponto($image,$x,$y,$black);
escreve($image,$x-5,$y-5,($i*$range[0]/($comp-100)),$black);
}

// Marca eixo Y
escreve($image,-10,($alt-52),"CPU(%)", $red);
escreve($image,-10,($alt-67),"MEM(%)", $blue);
escreve($image,-10,($alt-82),"MEM-BUF(%)", $darkgreen);

$passo=($alt-100)/$divy;
$y=0;
for ($i = 0; $y < ($alt-100); $i += $passo) {
$x=0;
$y=$i;
ponto($image,$x,$y,$black);
escreve($image,$x-20,$y+5,round($i/5),$black);
}
}


function gera_grafico($image,$alt,$comp,$divx,$divy,$pt,$num,$color) {

//lê o range X
$fp=fopen("rangex","r");
$range = explode(",",fgets ($fp,4096));
fclose($fp);

$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
for($i = 1; $i < $num; $i++) {
//plota 2 pontos
$pt1x = round($pt[1][$i]*($comp-100)/($range[0]),1);
$pt1y = round($pt[2][$i]*($alt-100)/($divy*10),1);
$pt2x = round($pt[1][$i+1]*($comp-100)/($range[0]),1);
$pt2y = round($pt[2][$i+1]*($alt-100)/($divy*10),1);
plota($image,$pt1x,$pt1y,$black);
plota($image,$pt2x,$pt2y,$black);
//liga os pontos
liga($image,$pt1x,$pt1y,$pt2x,$pt2y,$color);
//escreve os valores de Y
//if ($i == $num-1) {
escreve($image,$pt1x-15,$pt1y+15,round($pt[2][$i],1),$color);
escreve($image,$pt2x-15,$pt2y+15,round($pt[2][$i+1],1),$color);
//}
}

}


// ------------------------------ Monta Grafico
monta_grafico($image,$alt,$comp,24,10);

// ------------------------------ Gera Pontos no Grafico

$filename1="pts_cpu";
$fp1=fopen($filename1,"r+");
$filename2="pts_mem";
$fp2=fopen($filename2,"r+");
$filename3="pts_mem_buf";
$fp3=fopen($filename3,"r+");

$conteudo1 = fgets ($fp1,4096);
$num=0;

while (!feof ($fp1)) {
$array = explode(",",$conteudo1);
$x=$array[0];
$y=$array[1];

$num = $num +1;

$pt1[1][$num]=$x;
$pt1[2][$num]=$y;

$conteudo1 = fgets ($fp1,4096);
}

$conteudo2 = fgets ($fp2,4096);
$num2=0;

while (!feof ($fp2)) {
$array = explode(",",$conteudo2);
$x=$array[0];
$y=$array[1];

$num2 = $num2 +1;

$pt2[1][$num2]=$x;
$pt2[2][$num2]=$y;

$conteudo2 = fgets ($fp2,4096);
}

$conteudo3 = fgets ($fp3,4096);
$num3=0;

while (!feof ($fp3)) {
$array = explode(",",$conteudo3);
$x=$array[0];
$y=$array[1];

$num3 = $num3 +1;

$pt3[1][$num3]=$x;
$pt3[2][$num3]=$y;

$conteudo3 = fgets ($fp3,4096);
}

fclose($fp1);
fclose($fp2);
fclose($fp3);


gera_grafico($image,$alt,$comp,12,10,$pt1,$num,$red);
gera_grafico($image,$alt,$comp,12,10,$pt2,$num2,$blue);
gera_grafico($image,$alt,$comp,12,10,$pt3,$num3,$darkgreen);

// ------------------------------

// output the picture
header("Content-type: image/png");
imagepng($image);
imagedestroy($im);
?>

<html>
<head><title>Stress Test Graphic</title></head>
<body>
<FORM name=frm method=post enctype="multipart/form-data" action="index.php">
<HR noShade>
<TABLE border=0><TR><TD valign=top>
<TABLE border=0>
<TBODY>
<TR>
<TD align=right>Range Time:
<TD><INPUT name=usr_range_time size=3 value="<?=$range_time?>" type=text> ms
</TR><TR>
<TD colspan=2><center><INPUT name=enviar type=submit value="Submit IP"></center>
</TR>
</TABLE>
</TD></TR></TABLE>
<HR noShade>
</FORM>
</body>
</html>

[3] Comentário enviado por diaspcf em 05/05/2007 - 00:02h

Nao sei pq o arquivo nao foi.
Vou tentar falar com o moderador.
Saudacoes.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts