Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

RRDTool - PHP - Possible Incorrect Graphs

  • 27-04-2010 3:43pm
    #1
    Registered Users, Registered Users 2 Posts: 1,146 ✭✭✭


    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]
    <?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++;
    }

    ?>
    [/PHP]

    Sorry for the big chunk of code. The graphs are created at all the $opts arrays.


Advertisement