LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-26-2010, 03:33 PM   #1
mossy464
Member
 
Registered: Jan 2008
Posts: 55

Rep: Reputation: 15
RRDTool - PHP - Possible Incorrect Graphs


Hi all,

I wrote a script from various tutorials to create rrdtool graphs. Now it is creating graphs but i think they could be slightly incorrect.

Im running the script every 5 minutes and i ran it for a day but there didnt seem to be much on the graph. I have attached one of the graph images also.

Here is the script. Can anyone see anything wrong with it especially where i create the rrd file and create graphs?

PHP Code:
<?php

// Include required functions file and mysql config file
require_once('/var/www/functions.inc.php');
require_once(
'/var/www/config.inc.php');
    
$db_link mysql_connect(DB_HOSTNAMEDB_USERNAMEDB_PASSWORDDB_DATABASE);
    
mysql_select_db("bwmgr");
    
    
// sending query
$result mysql_query("SELECT ipaddress FROM hosts");
if (!
$result) {
die(
"Query to show fields from table failed");
}
$num mysql_num_rows($result);

mysql_close();
$k=0;
while(
$k<$num)
{
$hostip mysql_result($result,$k,"ipaddress");
$hostcomm "public";
$hostname "Home"
$hostmaintainer "Maurice Mullany";
$hostuptime preg_replace("/^Timeticks: \(\d*\) /"""
snmpget($hostip$hostcomm"sysUpTime.0"));

$interface_list snmpwalk($hostip$hostcomm"ifIndex");
echo 
$interface_list;
$num_interfaces count($interface_list);
echo 
$num_interfaces;
$i 0;
while ( 
$i $num_interfaces )
{
$interface str_replace("INTEGER: """$interface_list[$i]);
$interface_status str_replace("INTEGER: """snmpget($hostip
$hostcomm"ifOperStatus.$interface"));
if (!
preg_match("/down/"$interface_status))
{
$interface_descr str_replace("STRING: """snmpget($hostip
$hostcomm"ifDescr.$interface"));
if (
preg_match("/^Null.*$/"$interface_descr)) {
$i++; continue;
}
$interface_type str_replace("INTEGER: """snmpget($hostip
$hostcomm"ifType.$interface"));
//$interface_name = str_replace("STRING: ", "", snmpget($hostip, 
//$hostcomm, "ifName.$interface"));
$interface_speed str_replace("Gauge32: """snmpget($hostip
$hostcomm"ifSpeed.$interface"));


// Create the .rrd file if it does not exist
$rrd_file $hostip "_" $interface .".rrd";

if (!
file_exists($rrd_file))
{

$opts = array( "--step""300""--start",  0,
           
"DS:input:COUNTER:600:U:U",
           
"DS:output:COUNTER:600:U:U",
           
"RRA:AVERAGE:0.5:1:600",
           
"RRA:AVERAGE:0.5:6:700",
           
"RRA:AVERAGE:0.5:24:775",
           
"RRA:AVERAGE:0.5:288:797",
           
"RRA:MAX:0.5:1:600",
           
"RRA:MAX:0.5:6:700",
           
"RRA:MAX:0.5:24:775",
           
"RRA:MAX:0.5:288:797"
        
);

$ret rrd_create($rrd_file,  $optscount($opts));

if( 
$ret == )
{
    
$err rrd_error();
    echo 
"Create error: $err\n";
}
}

// Get the data via SNMP
$octets_in str_replace("Counter32: """snmpget($hostip$hostcomm
"interfaces.ifTable.ifEntry.ifInOctets.$interface"));
$octets_out str_replace("Counter32: """snmpget($hostip$hostcomm
"interfaces.ifTable.ifEntry.ifOutOctets.$interface"));

// Update the .rrd file
$ret rrd_update($rrd_file,  "N:$octets_in:$octets_out");

if( 
$ret == )
{
  
$err rrd_error();
  echo 
"ERROR occurred: $err\n";
}

// Create the images

//create daily graph
$optsd = array( "--start""-1d""--vertical-label=B/s",
                 
"DEF:inoctets=$rrd_file:input:AVERAGE",
                 
"DEF:outoctets=$rrd_file:output:AVERAGE",
                 
"AREA:inoctets#00FF00:In traffic",
                 
"LINE1:outoctets#0000FF:Out traffic\\r",
                 
"CDEF:inbits=inoctets,8,*",
                 
"CDEF:outbits=outoctets,8,*",
                 
"COMMENT:\\n",
                 
"GPRINT:inbits:AVERAGE:Avg In traffic\: %6.2lf %Sbps",
                 
"COMMENT:  ",
                 
"GPRINT:inbits:MAX:Max In traffic\: %6.2lf %Sbps\\r",
                 
"GPRINT:outbits:AVERAGE:Avg Out traffic\: %6.2lf %Sbps",
                 
"COMMENT: ",
                 
"GPRINT:outbits:MAX:Max Out traffic\: %6.2lf %Sbps\\r"
               
);
               
$retd rrd_graph("" $hostip "_" $interface "-day.gif"$optsd,  count($optsd));

  if( !
is_array($retd) )
  {
    
$err rrd_error();
    echo 
"rrd_graph() ERROR: $err\n";
  }

//create weekly graph
$optsw = array( "--start""-1w""--vertical-label=B/s",
                 
"DEF:inoctets=$rrd_file:input:AVERAGE",
                 
"DEF:outoctets=$rrd_file:output:AVERAGE",
                 
"AREA:inoctets#00FF00:In traffic",
                 
"LINE1:outoctets#0000FF:Out traffic\\r",
                 
"CDEF:inbits=inoctets,8,*",
                 
"CDEF:outbits=outoctets,8,*",
                 
"COMMENT:\\n",
                 
"GPRINT:inbits:AVERAGE:Avg In traffic\: %6.2lf %Sbps",
                 
"COMMENT:  ",
                 
"GPRINT:inbits:MAX:Max In traffic\: %6.2lf %Sbps\\r",
                 
"GPRINT:outbits:AVERAGE:Avg Out traffic\: %6.2lf %Sbps",
                 
"COMMENT: ",
                 
"GPRINT:outbits:MAX:Max Out traffic\: %6.2lf %Sbps\\r"
               
);
               
$retw rrd_graph("" $hostip "_" $interface "-week.gif"$optsw,  count($optsw));

  if( !
is_array($retw) )
  {
    
$err rrd_error();
    echo 
"rrd_graph() ERROR: $err\n";
  }
  
//create monthly graph   
$optsm = array( "--start""-1m""--vertical-label=B/s",
                 
"DEF:inoctets=$rrd_file:input:AVERAGE",
                 
"DEF:outoctets=$rrd_file:output:AVERAGE",
                 
"AREA:inoctets#00FF00:In traffic",
                 
"LINE1:outoctets#0000FF:Out traffic\\r",
                 
"CDEF:inbits=inoctets,8,*",
                 
"CDEF:outbits=outoctets,8,*",
                 
"COMMENT:\\n",
                 
"GPRINT:inbits:AVERAGE:Avg In traffic\: %6.2lf %Sbps",
                 
"COMMENT:  ",
                 
"GPRINT:inbits:MAX:Max In traffic\: %6.2lf %Sbps\\r",
                 
"GPRINT:outbits:AVERAGE:Avg Out traffic\: %6.2lf %Sbps",
                 
"COMMENT: ",
                 
"GPRINT:outbits:MAX:Max Out traffic\: %6.2lf %Sbps\\r"
               
);
               
$retm rrd_graph("" $hostip "_" $interface "-month.gif"$optsm,  count($optsm));

  if( !
is_array($retm) )
  {
    
$err rrd_error();
    echo 
"rrd_graph() ERROR: $err\n";
  }


//create yearly graph
$optsy = array( "--start""-1y""--vertical-label=B/s",
                 
"DEF:inoctets=$rrd_file:input:AVERAGE",
                 
"DEF:outoctets=$rrd_file:output:AVERAGE",
                 
"AREA:inoctets#00FF00:In traffic",
                 
"LINE1:outoctets#0000FF:Out traffic\\r",
                 
"CDEF:inbits=inoctets,8,*",
                 
"CDEF:outbits=outoctets,8,*",
                 
"COMMENT:\\n",
                 
"GPRINT:inbits:AVERAGE:Avg In traffic\: %6.2lf %Sbps",
                 
"COMMENT:  ",
                 
"GPRINT:inbits:MAX:Max In traffic\: %6.2lf %Sbps\\r",
                 
"GPRINT:outbits:AVERAGE:Avg Out traffic\: %6.2lf %Sbps",
                 
"COMMENT: ",
                 
"GPRINT:outbits:MAX:Max Out traffic\: %6.2lf %Sbps\\r"
               
);
               
$rety rrd_graph("" $hostip "_" $interface "-year.gif"$optsy,  count($optsy));

  if( !
is_array($rety) )
  {
    
$err rrd_error();
    echo 
"rrd_graph() ERROR: $err\n";
  }
  
// Create the HTML file
$html_file "/var/www/html/" $hostip "_" $interface 
".html";
if (
file_exists($html_file ))
unlink($html_file); }
$html_handle fopen($html_file"w+");
fwrite($html_handle"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML
4.01Transitional//EN\" \"
http://www.w3.org/TR/html4/loose.dtd\">\n"
);
fwrite($html_handle"<html>\n<head>\n<title>Interface statistics for 
$interface_descr</title>\n");
fwrite($html_handle"<meta http-equiv=\"Content-Type\" content=\"text/html; 
charset=iso-8859-1\">\n"
);
fwrite($html_handle"<meta http-equiv=\"Refresh\" content=\"300\">\n");
fwrite($html_handle"<meta http-equiv=\"Pragma\" content=\"no-cache\">\n");
fwrite($html_handle"<meta http-equiv=\"Cache-Control\" 
content=\"no-cache\">\n"
);
fwrite($html_handle"<meta http-equiv=\"Expires\" content=\"" date("D, d 
M Y H:i:s"
) ." GMT\">\n");
fwrite($html_handle"<meta http-equiv=\"Generator\" content=\"Gary 
Danko\">\n"
);
fwrite($html_handle"<meta http-equiv=\"Date\" content=\"" date("D, d M Y 
H:i:s"
) ." GMT\">\n");
fwrite($html_handle"<style type=\"text/css\">\nbody {background-color: 
white}\n"
);
fwrite($html_handle"\thtml {color: black; font-size: 100%}\n");
fwrite($html_handle"\th1 {font-size: 175%}\n");
fwrite($html_handle"</style>\n</head>\n<body>");
fwrite($html_handle"<h1>Traffic Analysis for $interface_descr on 
$hostname</h1><br>\n");
fwrite($html_handle"<table width=\"450\" border=\"0\" cellspacing=\"3\" 
cellpadding=\"3\">\n"
);
fwrite($html_handle"<tr>\n <td width=\"15%\">System:</td>\n");
fwrite($html_handle" <td width=\"85%\">$hostname</td>\n");
fwrite($html_handle"<tr>\n <td width=\"15%\">Maintainer:</td>\n");
fwrite($html_handle" <td width=\"85%\">$hostmaintainer</td>\n");
fwrite($html_handle"<tr>\n <td width=\"15%\">Description:</td>\n");
fwrite($html_handle" <td width=\"85%\">$interface_descr</td>\n");
fwrite($html_handle"<tr>\n <td width=\"15%\">ifType:</td>\n");
fwrite($html_handle" <td width=\"85%\">$interface_type</td>\n");
fwrite($html_handle"<tr>\n <td width=\"15%\">ifName:</td>\n");
fwrite($html_handle" <td width=\"85%\">$interface_name</td>\n");
fwrite($html_handle"</tr>\n</table>\n<hr>\n");
fwrite($html_handle"The statistics were last updated <b>" date ("n-d-Y 
g:i:s A"
filemtime($html_file)) . "</b>,<br>\n");
fwrite($html_handle"at which time '<b>$hostname</b>' had been up for 
<b>
$hostuptime</b>.\n<hr>\n");
fwrite($html_handle"<b>'Daily' Graph (5 Minute Average)</b><br><img 
vspace=10 align=top src=" 
$hostip "_" $interface "-day.gif
><br><hr>\n"
);
fwrite($html_handle"<b>'Weekly' Graph (30 Minute Average)</b><br><img 
vspace=10 align=top src=" 
$hostip "_" $interface "-week.gif
><br><hr>\n"
);
fwrite($html_handle"<b>'Monthly' Graph (2 Hour Average)</b><br><img 
vspace=10 align=top src=" 
$hostip "_" $interface "-month.gif
><br><hr>\n"
);
fwrite($html_handle"<b>'Yearly' Graph (1 Day Average)</b><br><img 
vspace=10 align=top src=" 
$hostip "_" $interface "-year.gif
><br><hr>\n"
);
fwrite($html_handle"</body>\n</html>\n");
fclose($html_handle);
}
$i++;

}
$k++;
}

?>
Attached Thumbnails
Click image for larger version

Name:	192.168.1.15_65540-day.PNG
Views:	20
Size:	11.1 KB
ID:	3447  

Last edited by mossy464; 04-26-2010 at 05:20 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Incorrect Graphs ?? kdelover Linux - Networking 2 04-24-2010 09:42 PM
[SOLVED] RRDTool With PHP on Ubuntu mossy464 Linux - Software 2 04-14-2010 05:58 AM
Unable to create graphs with rrdtool athreyavc Linux - General 1 09-06-2009 09:00 AM
Bigsister Hell: My rrdtool graphs aren't working geokker Linux - Software 0 05-11-2006 05:45 AM
reversing rrdtool graphs whysyn Linux - Software 0 06-05-2005 04:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration