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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
04-26-2010, 03:33 PM
#1
Member
Registered: Jan 2008
Posts: 55
Rep:
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_HOSTNAME , DB_USERNAME , DB_PASSWORD , DB_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 , $opts , count ( $opts )); if( $ret == 0 ) { $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 == 0 ) { $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 ++; } ?>
Last edited by mossy464; 04-26-2010 at 05:20 PM .
All times are GMT -5. The time now is 08:24 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News