Miscellaneous Code

  Home arrow Miscellaneous Code arrow RRD monitor page
MISCELLANEOUS CODE

RRD monitor page
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2003-03-06

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    Already use MRTG's? Store data with RRD?
    Try these php pages to set threshold monitoring on usage statistics.

    By : bigphish

    devmon.php ->

    <?php

    /*
    * Program Name: Device List Creater
    * Date: March 6, 2003
    * Modified: March 6, 2003
    * Author: Jason Chambers
    * File Name: devmon.php
    * email: jason.chambers@phishie.net
    *
    * Description: This page will create the list of devices to monitor by
    * going through the directories where the rrd files are
    * stored.
    *
    */

    /* Set path to base directory (where subdirectories contain the rrd files) */
    $rrd_dir = "/var/www/html/mon/";

    /* Look inside $rrd_dir for the subdirectories */
    if (is_dir($rrd_dir)){
    $fd = @opendir($rrd_dir);
    if($fd) {
    while (($part = @readdir($fd)) == true) {
    if ($part != "." && $part != "..") {
    if (is_dir($part)){
    $my_dir[] = $part;
    }
    }
    }
    }
    }

    sort($my_dir); /* Sort the array of subdirectories */
    reset($my_dir); /* Reset the pointer to the beginning of the array */

    $content = ""; /* Initialize $content */
    $content .= "<html>\n<head>\n<title>Monitor device list</title>\n</head>\n\n<body>\n\n";
    $content .= "<form name=\"mylist\" action=\"new_mon_list.php\" method=\"POST\">\n";

    for($i=0;$i<count($my_dir);$i++){
    $node_dir = $rrd_dir . $my_dir[$i];
    $content .= "<h5> in directory $my_dir[$i] </h5>\n";
    $content .= "<table>\n";
    if ($handle = opendir("$node_dir")) {
    while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
    $content .= "\t<tr>\n\t\t<td><input type=\"checkbox\" name=\"mon_me[]\" value=\"$file\"><font size=\"-1\" color=\"cornflowerblue\">$file</font></td>\n\t</tr>\n";
    }
    }
    }
    $content .= "</table>\n";
    }

    $content .= "<table>\n";
    $content .= "<tr><td><input type=\"SUBMIT\"></td></tr>\n";
    $content .= "</table>\n";
    $content .= "</form>\n";
    $content .= "</body>\n";
    $content .= "</html>\n\n";

    print "$content";


    ?>

    !-----------------------
    new_mon_list.php ->

    <?php

    /*
    * This file takes the posted data from devmon.php
    * and writes to monlist.txt
    *
    * Your webserver needs to have write access
    * to this directory
    *
    */


    $to_file = "";

    foreach($_POST as $key => $nested)
    {

    for($i=0;$i<count($nested);$i++)
    {
    $to_file .= "$nested[$i]\n";
    }
    }

    $myFile = fopen("monlist.txt","w");
    fputs($myFile,"$to_file");
    fclose($myFile);

    header("Refresh: 5; URL=../index.php");
    print "<h4><i>The file \"monlist.txt\" has been created in this directory.</i></h4>";

    ?>

    !----------------------------------
    index.php ->

    <?

    /*
    * Program Name: RRD monitor script
    * Date: March 3, 2003
    * Modified: March 5, 2003
    * Author: Jason Chambers
    * File name: index.php
    * email:jason.chambers@phishie.net
    *
    */

    header("Refresh: 600"); /* Tell the browser to refresh every 10 minutes */

    $send_alert = ''; /* Send alerts when threshold is surpassed 'on'/'off' */
    $notify_who = ''; /* Set the email address of the person to notify */
    $mail_msg = 'Hello, '; /* Initialize $mail_msg */

    /* poll list file */
    $poll_list = '/home/rrdtool/poll/list';

    /* path to rrdtool */
    $path_rrdtool = '/usr/local/bin/';

    /* path to base directory (directory that contains the subdirectories/working directories) for your rrd files */
    $path_rrdbase_dir = '/var/www/html/mon/';

    $time_now = time(); /* Epoch Timestamp for "now" */
    $time_back = $time_now - 600; /* Timestamp for 10 minutes back from "now" */

    $th_high = 4; /* if interface traffic increase by this amount */
    $th_low = 3; /* if interface traffic decreases by this amount */
    $num_trouble = 0; /* initialize num_trouble variable to 0 (no troubles) */

    echo "<h3>" . date("l, F dS, Y h:i A") . "</h3>";

    print "<table border=1 align=\"center\">\n";
    print "\t<tr>\n";
    print "\t\t<th>device</th>\n";
    print "\t\t<th>interface</th>\n";
    print "\t\t<th>ALERT? IN</th>\n";
    print "\t\t<th>ALERT? OUT</th>\n";

    $handle = fopen($poll_list,"r");
    while (!feof($handle)) {
    $from_file = fgets($handle,256);
    $from_file = chop($from_file);
    if (!$from_file == ""){


    $node = preg_split('/_/',$from_file); /* $node[] is an array on the split line, '_' from the poll file */
    $path_rrdfiles = $path_rrdbase_dir . "$node[0]"; /* path to rrd files */

    /* rrdtool fetch for now and 15 minutes ago */
    $run_now = $path_rrdtool . "rrdtool fetch $path_rrdfiles/$from_file AVERAGE -r 900 -s $time_now -e $time_now";

    $my_data_now = shell_exec($run_now); /* Array with the results of the rrdtool fetch command */
    $data0_array = preg_split('/\n/',$my_data_now); /* Split the array on new line char */
    $now_data = preg_split('/ /',$data0_array[2]);; /* Split the new array on spaces */

    /* Get old data */
    $run_old = $path_rrdtool . "rrdtool fetch $path_rrdfiles/$from_file AVERAGE -r 900 -s $time_back -e $time_back";

    $my_data_old = shell_exec($run_old); /* Array with the results of the rrdtool fetch command */
    $data1_array = preg_split('/\n/',$my_data_old); /* Split the array on new line char */
    $old_data = preg_split('/ /',$data1_array[2]); /* split the new array on spaces */

    /* Setup traffic in/out info */
    $now_traf_in = $now_data[1] * 1;
    $now_traf_out = $now_data[2] * 1;
    $now_traf_in = number_format($now_traf_in,2,'.', '');
    $now_traf_out = number_format($now_traf_out,2,'.', '');

    $old_traf_in = $old_data[1] * 1;
    $old_traf_out = $old_data[2] * 1;
    $old_traf_in = number_format($old_traf_in,2,'.', '');
    $old_traf_out = number_format($old_traf_out,2,'.', '');

    /* Setup up alert levels */
    $alert_up_in = $old_traf_in * $th_high; /* if in traffic has doubled in the last 15 minutes */
    $alert_down_in = $old_traf_in / $th_low; /* if in traffic has decreased by half */
    $alert_up_out = $old_traf_out * $th_high; /* if out traffic has doubled in the last 15 minutes */
    $alert_down_out = $old_traf_out / $th_low; /* if out traffic has decreased by half */

    /* Check for an alert */

    if ($now_traf_in>$alert_up_in||$now_traf_in<$alert_down_in||$now_traf_in==0)
    {
    $in_bg_color = "red";
    $in_trouble = "yes";
    $num_trouble = $num_trouble + 1;
    $mail_msg .= "alert detected on $node[0] port $node[1] going IN. Please check traffic on $node[0] for issues.\n";
    }
    else
    {
    $in_bg_color = "green";
    $in_trouble = "no";
    }

    if ($now_traf_out>$alert_up_out||$now_traf_out<$alert_down_out||$now_traf_out==0)
    {
    $out_bg_color = "red";
    $out_trouble = "yes";
    $num_trouble = $num_trouble + 1;
    $mail_msg .= "alert detected on $node[0] port $node[1] going OUT. Please check traffic on $node[0] for issues.\n";
    }
    else
    {
    $out_bg_color = "green";
    $out_trouble = "no";
    }

    if ($in_trouble == "yes" || $out_trouble == "yes") {
    print "\t<tr>\n";
    print "\t\t<td>$node[0]</td>\n";
    print "\t\t<td>$node[1]</td>\n";
    print "\t\t<td bgcolor=\"$in_bg_color\" align=\"center\"><font color=\"white\"><strong>:: IN ::</strong></font></td>\n";
    print "\t\t<td bgcolor=\"$out_bg_color\" align=\"center\"><font color=\"white\"><strong>:: OUT ::</strong></font></td>\n";
    print "\t</tr>\n";
    }

    }

    }

    fclose($handle);

    /* No trouble? Cool, display no trouble message. */
    if ($num_trouble == 0){

    print "\t<tr>\n";
    print "\t\t<td colspan=4 align=\"center\"> <i>no current trouble</i></td>\n";
    print "\t</tr>\n";

    }

    /* Should we send the alert by email? */
    if ($num_trouble > 0 && $send_alert == "on") {
    mail("$notify_who","Alert detected while comparing interface usage.","$mail_msg\n Thanks.");
    }


    print "</table>\n";

    /* Credits */
    print "<table align=\"center\">\n\t<tr>\n\t\t<td><font size=\"-2\"><i>Created by <a href=\"mailto:jason.chambers@phishie.net\">Jason Chambers</a></i></font></td>\n\t</tr>\n</table>\n\n";

    ?>




    Click to Download File



    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap