Location: PHPKode > projects > Network Traffic Monitor > sampling/snmpCheck.php
#!/usr/bin/php

<?php

#Network Traffic Monitor is an application to monitor the network traffic on all SNMP managable devices.
#
#Copyright (c) 2004, Daimler Trust nv <hide@address.com> - Jan Van Hees <hide@address.com>
# 
#This file is part of Network Traffic Monitor.
#
#    Network Traffic Monitor is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    Network Traffic Monitor is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Network Traffic Monitor; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
include ('config.php');

$sql1 = "SELECT ports.pid,ports.portnr,devices.host,devices.community from ports
INNER JOIN devices ON devices.did = ports.did";



//$db->query($sql1) returns an object
//run fetchrow on object
$result = $db->query($sql1);
$date = date("Y-m-d G:i:s");

// Get each row of data on each iteration until
// there are no more rows
while ($row = $result->fetchRow()) {
echo "------------------------------------------------------------\n";
echo "fetching data\n";
    for($i=0;$i<7;$i++){
	$id = "$row[$i] ";
    }//for
    //get maximum portspeed
    $sqlsp = "SELECT speed.speed FROM speed INNER JOIN ports ON ports.speed=speed.spid WHERE ports.pid=$row[0]";
    $maxspeed = $db->getOne($sqlsp);
    //get hostname from db
    $host = $row[2];
    //get community from db
    $community = $row[3];
    //get portnr from db
    $port = $row[1];
echo "host: $host  port: $port   pid: $row[0]\n";
    //define snmp input / output paraeters
    $usageIn =  "interfaces.ifTable.ifEntry.ifInOctets.$port";
    $usageOut = "interfaces.ifTable.ifEntry.ifOutOctets.$port";
    $PID=$row[0];
    $sql2= "select * from porttraffic where pid = $PID";
    //snmp checkouts
	$snmpIn = snmpget("$host","$community","$usageIn");
	$snmpOut = snmpget("$host","$community","$usageOut");
	
	//filter the wanted numeric values from output
	$loadIn = split (": ","$snmpIn");
	$loadOut = split(": ","$snmpOut");
	//get values from array into variable
	$loadIn = $loadIn[1];
	$loadOut = $loadOut[1];
	
	$alive=$db->query($sql2);
	$sql4 = "SELECT incoming,outgoing from lastvalues WHERE pid=$PID";
	$res = $db->query($sql4);
	$lastIn;
	$lastOut;
	while ($row2 = $res->fetchRow()) {
		$lastIn =  $row2[0];
		$lastOut = $row2[1];
	}//while row2
echo "loadIn:  $loadIn     lastIn:  $lastIn\n";
echo "loadOut: $loadOut    lastOut: $lastOut\n\n";
	$sql5 = "UPDATE lastvalues set incoming=$loadIn, outgoing=$loadOut WHERE pid=$PID";


echo "DEFAULT PRINT OF VALUES\n lastin: $lastIn;  loadin: $loadIn;  lastout: $lastOut;   loadout: $loadOut;   \n";
	if($lastIn == 0){
echo "lastin=0\n";
		//if lastin = 0, insert the recent selected value an insert as last value
		$db->query($sql5) or die("Nops, won't work...");
	}// if lastin = 0
	else if($lastIn > $loadIn || $lastOut > $loadOut){
		if($lastIn > $loadIn){
echo "lastin>loadin \n";
			$t = "incoming";
			$over = checkOverflow($maxspeed, $lastIn, $loadOut);
			if($over != 0){
				$sqli = "INSERT INTO porttraffic (pid, incoming, outgoing, times) VALUES ($PID, $over, $loadOut, '$date')";
			}//if over ! 0

		}//incoming overload
echo "lastout>loadout\n";
		if($lastOut > $loadOut){
			$t = "outgoing";
			$over = checkOverflow($maxspeed, $lastOut, $loadOut);
			if($over != 0){
				$sqli = "INSERT INTO porttraffic (pid, incoming, outgoing, times) VALUES ($PID, $loadIn, $over, '$date')";
			}//if over != 0
		}//outgoing overload
echo "\noverflow insert: $sqli \n\n";
#		$db->query($sqli);

		// Update last value
		$db->query($sql5);
	}//if lastin < loadin switch has been reset
	else{
echo "normal behaviour\n";
		// normal behaviour, update lastvalues, calculate difference, insert that diff in traffic table
		$db->query($sql5);
		$loadIn = $loadIn - $lastIn;
		$loadOut = $loadOut - $lastOut;
		if($PID != $PPID){
			$sql3 = "insert into porttraffic (pid,incoming,outgoing,times) VALUES($PID,$loadIn,$loadOut,'$date');";
			$db->query($sql3) or die ("insert error");
		echo "insert sql: $sql3\n";
		}
		$PPID = $PID;
	}// else: lastin < loadin: normal function
echo "\n\n";
}

function checkOverflow($maxspeed, $last, $load){
echo "#################### CHECKING OVERFLOW ########################\n";
 	$value = pow(2,32) - $last + $load;
	$reference = $maxspeed * 300;
	return ($value<=$reference) ? $value : 0;
}//function checkoverflow
?>
Return current item: Network Traffic Monitor