Location: PHPKode > projects > Network Traffic Monitor > online/number.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

/*
* This script provides a user interface 2 give a numeric representation of the network usage 
*/
require_once("../auth.php");
require_once ("../config.php");
require_once ("HTML/Template/IT.php");

$iface = $_SESSION['iface'];
$uid = $_SESSION['uid'];
$gid = $_SESSION['grp'];

$hist = $_POST['history'];
if ($hist){
	$_SESSION['history']=$hist;
	$mon = date("n");
	if($hist==0) {$hist=$mon;}
	else {$mon = $mon - $hist;}
}
else {
	$mon = date("n");
	$_SESSION['history']=0;
	$hist=0;
}
	$m = mktime (1,1,1,$mon);
	$month = strftime("%B", $m);



$db = dbLogin();

$tpl = new HTML_Template_IT(TEMPLATEDIR);
$tpl->loadTemplatefile("number.tpl");
$tpl->setVariable("title", "Usage Numbers");
//set daly
$date = date ("l: d-m-Y");
$tpl->setVariable("daylabel",$date);
$tpl->setVariable("day", setDaily($db,$uid,$iface));
$tpl->setVariable("monthlabel",$month);
$tpl->setVariable("month", setMonthly($db,$uid,$iface));
$date = date("Y");
$tpl->setVariable("yearlabel",$date);
$tpl->setVariable("year", setYearly($db,$uid,$iface));
$content = $tpl->get();

fillFrame($db, $content);

$burst = $db->getOne("SELECT burstable from ports WHERE pid=$iface");

dblogout($db);



function setDaily($db,$uid,$iface){
global $gid;
	$today = date("Y-m-d");

	$sqlday = "SELECT ports.portname, SUM(porttraffic.incoming) AS in, SUM(porttraffic.outgoing) AS out
			FROM ports 
			INNER JOIN porttraffic ON porttraffic.pid = ports.pid
 			WHERE ports.pid=$iface AND  times LIKE '%$today%'
		 	GROUP BY ports.portname ORDER BY ports.portname";
	$data = fill($db,$sqlday);

	return $data;
}//function setdaily

function setmonthly($db,$uid,$iface){
global $hist;
	$today = date("Y-m");

	if ($hist > 0) {
		$today = date("Y-m", strtotime("-$hist months"));
	}

	$sqlmonth = "SELECT ports.portname, SUM(porttraffic.incoming) AS in, SUM(porttraffic.outgoing) AS out
			FROM ports
			INNER JOIN porttraffic ON porttraffic.pid = ports.pid
			WHERE times LIKE '%$today%' AND ports.pid=$iface
			GROUP BY ports.portname ORDER BY ports.portname";


	$data = fill($db,$sqlmonth);
	return $data;

}//function setMonthly

function setyearly($db,$uid,$iface){
	$thisyear = date("Y");
	$sqlyear = "SELECT ports.portname, SUM(porttraffic.incoming) AS in, SUM(porttraffic.outgoing) AS out
			FROM ports
			INNER JOIN porttraffic ON porttraffic.pid = ports.pid
			WHERE times LIKE '%$thisyear%' AND ports.pid=$iface
			GROUP BY ports.portname ORDER BY ports.portname";


	$data = fill($db,$sqlyear);
	return $data;

}//function setyearly

function fill($db,$sql){
#<table border=\"1\" align = \"center\">
	$result = $db->query($sql);
	$data="	<tr>
		<td>interface</td>
		<td>Incoming</td>
		<td>Outgoing</td>
		<td>total</td>
	</tr>";
	$dayUp=0;
	$dayDown=0;
	$total=0;

	while ($row = $result->fetchRow()){
		$total = $total + $row[1] + $row[2];
		$iref = setReadable($row[1]);
		$oref = setReadable($row[2]);
		$total = setReadable($total);
		
		$data=$data."<tr><td>$row[0]</td><td><i>$iref</i></td><td><i>$oref</i></td><td><i>$total</i></td></tr>";
	}//while fetchrow

	return $data;

}//fill
?> 
Return current item: Network Traffic Monitor