Version: v1
Type: Full Script
Category: Other
License: GNU General Public License
Description: Already use MRTG's? Store data with RRD? Try these php pages to set threshold monitoring on usage statistics. Three files, index.php, devmon.php, and new_mon_list.php
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";
?>