[rrd-users] RRDtool PHP script

Gary Danko gdanko at gmail.com
Tue Sep 13 19:28:53 MEST 2005


My transition from MRTG to RRDtool was not without a bit of pain. I spent a 
good week figuring out how it all works but I ended up with a nice PHP 
script to monitor switches and all of their ports. Some things like paths 
will need to be modified.
The script does the following:
1) Checks for an .rrd file for the each port on the switch and creates one 
if it does not exist.
2) Check each port to make sure it is not operationally down.
3) If the port is up, gather the info and stuff it into the .rrd file for 
that port.
4) Generate an html file for each port.

I also have a PHP page that serves as an index for all the ports. It scans 
the list of html files and creates links for them in a nice format. If this 
will be of use to you, email me off list and I will send the code.

If there are any newbies, maybe they can benefit from my research. If not, I 
apologize for the spam.

Oh yeah, cron it to run every 5. :)

#!/usr/bin/php
<?
$switch_ip = "your_switch_ip";
$switch_cs = "your_community_string";
$switch_name = "your_switch_name"; // Some switches do not have sysName 
populated
$switch_maintainer = "Gary Danko";
$switch_uptime = preg_replace("/^Timeticks: \(\d*\) /", "", 
snmpget($switch_ip, $switch_cs, "sysUpTime.0"));

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


// Create the .rrd file if it does not exist
$rrd_file = "/usr/local/rrdtool/cfg/" . $switch_ip . "_" . $interface . 
".rrd";
if (!file_exists($rrd_file)) {
system("/usr/local/rrdtool/bin/rrdtool create " . $rrd_file . " 
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");
}

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

// Update the .rrd file
system("/usr/local/rrdtool/bin/rrdtool update " . $rrd_file . " 
N:$octets_in:$octets_out");

// Create the images
system("/usr/local/rrdtool/bin/rrdtool graph /data2/www/html/rrdtool2/" . 
$switch_ip . "_" . $interface . "-day.png --start -1d --width 480 --height 
150 DEF:inoctets=" . $rrd_file . ":input:AVERAGE CDEF:realin=inoctets,8,\* 
DEF:outoctets=" . $rrd_file . ":output:AVERAGE CDEF:realout=outoctets,8,\* 
AREA:realin#00FF00:\"Inbound traffic\" LINE1:realout#0000FF:\"Outbound 
traffic\" --vertical \"Bits per Second\" COMMENT:\"\\n\" COMMENT:\"\\n\" 
COMMENT:\" Maximum Average Current\\n\" COMMENT:\"In \" 
GPRINT:realin:MAX:\"%7.2lf %sb/s\" GPRINT:realin:AVERAGE:\"%7.2lf %Sb/s\" 
GPRINT:realin:LAST:\"%7.2lf %Sb/s\\n\" COMMENT:\"Out \" 
GPRINT:realout:MAX:\"%7.2lf %Sb/s\" GPRINT:realout:AVERAGE:\"%7.2lf %Sb/s\" 
GPRINT:realout:LAST:\"%7.2lf %Sb/s\" > /dev/null 2>&1");

system("/usr/local/rrdtool/bin/rrdtool graph /data2/www/html/rrdtool2/" . 
$switch_ip . "_" . $interface . "-week.png --start -1w --width 480 --height 
150 DEF:inoctets=" . $rrd_file . ":input:AVERAGE CDEF:realin=inoctets,8,\* 
DEF:outoctets=" . $rrd_file . ":output:AVERAGE CDEF:realout=outoctets,8,\* 
AREA:realin#00FF00:\"Inbound traffic\" LINE1:realout#0000FF:\"Outbound 
traffic\" --vertical \"Bits per Second\" COMMENT:\"\\n\" COMMENT:\"\\n\" 
COMMENT:\" Maximum Average Current\\n\" COMMENT:\"In \" 
GPRINT:realin:MAX:\"%7.2lf %sb/s\" GPRINT:realin:AVERAGE:\"%7.2lf %Sb/s\" 
GPRINT:realin:LAST:\"%7.2lf %Sb/s\\n\" COMMENT:\"Out \" 
GPRINT:realout:MAX:\"%7.2lf %Sb/s\" GPRINT:realout:AVERAGE:\"%7.2lf %Sb/s\" 
GPRINT:realout:LAST:\"%7.2lf %Sb/s\" > /dev/null 2>&1");

system("/usr/local/rrdtool/bin/rrdtool graph /data2/www/html/rrdtool2/" . 
$switch_ip . "_" . $interface . "-month.png --start -1m --width 480 --height 
150 DEF:inoctets=" . $rrd_file . ":input:AVERAGE CDEF:realin=inoctets,8,\* 
DEF:outoctets=" . $rrd_file . ":output:AVERAGE CDEF:realout=outoctets,8,\* 
AREA:realin#00FF00:\"Inbound traffic\" LINE1:realout#0000FF:\"Outbound 
traffic\" --vertical \"Bits per Second\" COMMENT:\"\\n\" COMMENT:\"\\n\" 
COMMENT:\" Maximum Average Current\\n\" COMMENT:\"In \" 
GPRINT:realin:MAX:\"%7.2lf %sb/s\" GPRINT:realin:AVERAGE:\"%7.2lf %Sb/s\" 
GPRINT:realin:LAST:\"%7.2lf %Sb/s\\n\" COMMENT:\"Out \" 
GPRINT:realout:MAX:\"%7.2lf %Sb/s\" GPRINT:realout:AVERAGE:\"%7.2lf %Sb/s\" 
GPRINT:realout:LAST:\"%7.2lf %Sb/s\" > /dev/null 2>&1");

system("/usr/local/rrdtool/bin/rrdtool graph /data2/www/html/rrdtool2/" . 
$switch_ip . "_" . $interface . "-year.png --start -1y --width 480 --height 
150 DEF:inoctets=" . $rrd_file . ":input:AVERAGE CDEF:realin=inoctets,8,\* 
DEF:outoctets=" . $rrd_file . ":output:AVERAGE CDEF:realout=outoctets,8,\* 
AREA:realin#00FF00:\"Inbound traffic\" LINE1:realout#0000FF:\"Outbound 
traffic\" --vertical \"Bits per Second\" COMMENT:\"\\n\" COMMENT:\"\\n\" 
COMMENT:\" Maximum Average Current\\n\" COMMENT:\"In \" 
GPRINT:realin:MAX:\"%7.2lf %sb/s\" GPRINT:realin:AVERAGE:\"%7.2lf %Sb/s\" 
GPRINT:realin:LAST:\"%7.2lf %Sb/s\\n\" COMMENT:\"Out \" 
GPRINT:realout:MAX:\"%7.2lf %Sb/s\" GPRINT:realout:AVERAGE:\"%7.2lf %Sb/s\" 
GPRINT:realout:LAST:\"%7.2lf %Sb/s\" > /dev/null 2>&1");

// Create the HTML file
$html_file = "/data2/www/html/rrdtool2/" . $switch_ip . "_" . $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 
$switch_name</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%\">$switch_name</td>\n");
fwrite($html_handle, "<tr>\n <td width=\"15%\">Maintainer:</td>\n");
fwrite($html_handle, " <td width=\"85%\">$switch_maintainer</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>$switch_name</b>' had been up for 
<b>$switch_uptime</b>.\n<hr>\n");
fwrite($html_handle, "<b>'Daily' Graph (5 Minute Average)</b><br><img 
vspace=10 align=top src=" . $switch_ip . "_" . $interface . "-day.png
><br><hr>\n");
fwrite($html_handle, "<b>'Weekly' Graph (30 Minute Average)</b><br><img 
vspace=10 align=top src=" . $switch_ip . "_" . $interface . "-week.png
><br><hr>\n");
fwrite($html_handle, "<b>'Monthly' Graph (2 Hour Average)</b><br><img 
vspace=10 align=top src=" . $switch_ip . "_" . $interface . "-month.png
><br><hr>\n");
fwrite($html_handle, "<b>'Yearly' Graph (1 Day Average)</b><br><img 
vspace=10 align=top src=" . $switch_ip . "_" . $interface . "-year.png
><br><hr>\n");
fwrite($html_handle, "</body>\n</html>\n");
fclose($html_handle);
}
$i++;
}
?>

--
Unsubscribe mailto:rrd-users-request at list.ee.ethz.ch?subject=unsubscribe
Help        mailto:rrd-users-request at list.ee.ethz.ch?subject=help
Archive     http://lists.ee.ethz.ch/rrd-users
WebAdmin    http://lists.ee.ethz.ch/lsg2.cgi



More information about the rrd-users mailing list