Location: PHPKode > projects > Network Traffic Monitor > online/total.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 for all interfaces of a group 
*/
require_once("../auth.php");
require_once ("../config.php");
require_once ("HTML/Template/IT.php");
require_once "HTML/Table.php";
if($_POST[port] && $_POST[history]){
	header("Location: error.php");
	exit;
}
$iface = $_SESSION['iface'];
$uid = $_SESSION['uid'];
$logtype = $_SESSION['logtype'];
$hist = $_POST[hist];

if($_POST && ($logtype=="Administrator" || $logtype=="Finance") ){ 
	$_SESSION['grp'] = $_POST['port'];
	$_SESSION['hist'] = $_POST['hist'];
}
$gid = $_SESSION['grp'];
$db = dbLogin();
$date = date("Y");
$month = date("m");
$month = $month - $hist;
if($month < 10 && $month > 0) $month = "0$month";
if($month < 0) $month = 12 + $month;
$m = mktime (1,1,1,$month);
$mon = strftime("%B", $m);

#$month = date("F Y");
$tpl = new HTML_Template_IT(TEMPLATEDIR);
$tpl->loadTemplatefile("totals.tpl");
$tpl->setVariable("title","Usage Total");
	$groups = getGroup($db,$gid);
if($logtype == "Administrator" || $logtype == "Finance"){
	$groupcombo ="";
	$combo = "<form action=\"total.php\" method=\"post\"><select name=\"port\">";
	$group = setCombo($db);
	$grpend = "</select>";
	$hister = "<select name=\"hist\">".setHist()."</select>";
	$end ="&nbsp;<br /><input type=\"submit\" value=\"submit\" /></form>";
	$groupcombo = $combo.$group.$grpend.$hister.$end;
	$tpl->setVariable("groupselect",$groupcombo);
	$tpl->setVariable("grouplabel","$groups $date");
	$tpl->setVariable("groupmonthlabel"," $groups $mon");
}
else if ($logtype == "User"){
	$gid = $_SESSION['grp'];
	$groupcombo ="";
	$combo = "<form action=\"total.php\" method=\"post\">";
	$hister = "<select name=\"hist\">".setHist()."</select>";
	$end ="&nbsp;<br /><input type=\"submit\" value=\"submit\" /></form>";
	$groupcombo = $combo.$hister.$end;
	
	$tpl->setVariable("groupselect",$groupcombo);
	$tpl->setVariable("grouplabel","$groups $date");
	$tpl->setVariable("groupmonthlabel"," $groups $mon");
}

if($groups != "Administrator" || $groups !="Finance"){
	$perform = "SELECT DISTINCT ports.portname, ports.pid FROM ports
			INNER JOIN hourtraffic ON hourtraffic.pid=ports.pid
			INNER JOIN portgroups ON portgroups.pid=ports.pid
			WHERE portgroups.gid=$gid
			ORDER BY ports.portname";

	$perf = $db->query($perform);

	$i = 0;
	$j = 0;
	while($row = $perf->fetchrow()){
		$pname = $row[0];
		$pid = $row[1];
		$year = date("Y");
			$sql = "SELECT SUM(hourtraffic.incoming) AS in, SUM (hourtraffic.outgoing) FROM hourtraffic INNER JOIN ports on ports.pid=hourtraffic.pid WHERE hourtraffic.pid=$pid AND times LIKE '$year%'";
		# $sql = "SELECT SUM(hourtraffic.incoming) AS in, SUM (hourtraffic.outgoing) AS out 
			# FROM porttraffic 
			# WHERE porttraffic.pid=$pid AND times like '$year' INNER JOIN ports ON ports.pid = porttraffic.pid";

		$dater = "$year-$month";
			$sqlm = "SELECT sum(hourtraffic.incoming) AS in, SUM (hourtraffic.outgoing) FROM hourtraffic INNER JOIN ports ON ports.pid=hourtraffic.pid WHERE hourtraffic.pid=$row[1] AND times LIKE'$dater%'";


		# $sqlm = "SELECT sum(porttraffic.incoming) AS in, SUM (porttraffic.outgoing) AS out
			# FROM porttraffic
			# WHERE porttraffic.pid=$row[1] AND times like '$dater%' INNER JOIN ports ON ports.pid = porttraffic.pid";

		$temp=$db->query($sql);
		
		while ($row = $temp->fetchrow()){
			$ifacetotal = $row[0] + $row[1];
			$intotal += $row[0];
			$outtotal += $row[1];

			$iref = setReadable($row[0]);
			$oref = setReadable($row[1]);
			$ifacetotal = setReadable($ifacetotal);


			$arr = array($pname, $iref, $oref,$ifacetotal);
			$data[$i]=$arr;
			$i++;

		}//while fetchrow

		$temp2 = $db->query($sqlm);
		
		while ($row = $temp2->fetchrow()){
			$ifacetotal2 = $row[0] + $row[1];
			$intotal2 += $row[0];
			$outtotal2 += $row[1];

			$iref2 = setReadable($row[0]);
			$oref2 = setReadable($row[1]);
			$ifacetotal2 = setReadable($ifacetotal2);
			$arr2 = array($pname, $iref2, $oref2,$ifacetotal2);
			$data2[$j]=$arr2;
			$j++;

		}//while fetchrow

	}#while perf->fetchrow
	$year = date("Y");
/*	$sql ="SELECT ports.portname, SUM(porttraffic.incoming ) AS in, SUM(porttraffic.outgoing ) AS out
			FROM ports
			INNER JOIN porttraffic ON porttraffic.pid = ports.pid
			INNER JOIN portgroups ON portgroups.pid=ports.pid 
			WHERE times LIKE '$year%' AND portgroups.gid = $gid 
			GROUP BY ports.portname
			ORDER BY ports.portname";
*/
	$dater = "$year-$month";
/*	$sqlm ="SELECT ports.portname, SUM(porttraffic.incoming ) AS in, SUM(porttraffic.outgoing ) AS out
			FROM ports
			INNER JOIN porttraffic ON porttraffic.pid = ports.pid
			INNER JOIN portgroups ON portgroups.pid=ports.pid 
			WHERE times LIKE '$dater%' AND portgroups.gid = $gid 
			GROUP BY ports.portname
			ORDER BY ports.portname";
*/
/*
#Yearly table config
	$temp = $db->query($sql);
	$i = 0;
	while ($row = $temp->fetchrow()){
		$ifacetotal = $row[1] + $row[2];
		$intotal += $row[1];
		$outtotal += $row[2];

		$iref = setReadable($row[1]);
		$oref = setReadable($row[2]);
		$ifacetotal = setReadable($ifacetotal);



		$arr = array($row[0], $iref, $oref,$ifacetotal);
		$data[$i]=$arr;
		$i++;

	}//while fetchrow
*/

	$sum = $intotal + $outtotal;
	$sum = setReadable($sum);
	$intotal = setReadable($intotal);
	$outtotal = setReadable($outtotal);

	$data[$i]=array("<b>Total</b>",$intotal,$outtotal,$sum);
#create table
	$tableAttrs = array("width" => "600");
	$table = new HTML_Table($tableAttrs);
	$table -> setAutoGrow(true);

	for($nr = 0 ; $nr < count($data); $nr++) {
		for($i = 0; $i < 4; $i++) {  
			if("" != $data[$nr][$i])
			$table -> setCellContents( $nr+1, $i+1, $data[$nr][$i]);
 		}//for
	}//for
	$altRow = array("bgcolor"=>"silver");	
	$table -> altRowAttributes(1, null, $altRow);
	$table -> setHeaderContents(0, 1, "Interface");
	$table -> setHeaderContents(0, 2, "Incoming");
	$table -> setHeaderContents(0, 3, "Outgoing");
	$table -> setHeaderContents(0, 4, "Total");
	$hrAttrs = array("bgcolor" => "silver");
	$table -> setRowAttributes(0, $hrAttrs, true);
	
	$tpl->setVariable("totaldata",$table->toHTML());

# monthly table 

/*	$temp2 = $db->query($sqlm);
	$i=0;
	while ($row = $temp2->fetchrow()){
		$ifacetotal2 = $row[1] + $row[2];
		$intotal2 += $row[1];
		$outtotal2 += $row[2];

		$iref2 = setReadable($row[1]);
		$oref2 = setReadable($row[2]);
		$ifacetotal2 = setReadable($ifacetotal2);

		$arr2 = array($row[0], $iref2, $oref2,$ifacetotal2);
		$data2[$i]=$arr2;
		$i++;
#echo "$row[0] , $iref2 , $oref2, $ifacetotal2 <br />";

	}//while fetchrow
*/
	$sum2 = $intotal2 + $outtotal2;
	$sum2 = setReadable($sum2);
	$intotal2 = setReadable($intotal2);
	$outtotal2 = setReadable($outtotal2);
	$data2[$j]=array("<b>Total</b>",$intotal2,$outtotal2,$sum2);

# create monthly table
	$tableAttrs = array("width" => "600");
	$table2 = new HTML_Table($tableAttrs);
	$table2 -> setAutoGrow(true);
	for($nr = 0 ; $nr < count($data2); $nr++) {
		for($i = 0; $i < 4; $i++) {  
			if("" != $data2[$nr][$i])
			$table2 -> setCellContents( $nr+1, $i+1, $data2[$nr][$i]);
 		}//for
	}//for

	$table2 -> altRowAttributes(1, null, $altRow);
	$table2 -> setHeaderContents(0, 1, "Interface");
	$table2 -> setHeaderContents(0, 2, "Incoming");
	$table2 -> setHeaderContents(0, 3, "Outgoing");
	$table2 -> setHeaderContents(0, 4, "Total");
	$hrAttrs = array("bgcolor" => "silver");
	$table2 -> setRowAttributes(0, $hrAttrs, true);
	
	$tpl->setVariable("totalmonthdata",$table2->toHTML());






}//if group

$content = $tpl->get();
fillFrame($db, $content);

function setCombo($db){
	$sql = "SELECT gid, groupname FROM groups WHERE groupname NOT LIKE 'Administrators' ORDER BY groupname";
	$result = $db->query($sql);

	while ($row = $result->fetchRow()){
		if (!$_SESSION[grp]) $_SESSION[grp] = $row[0];
		$selected = ($row[0] == $_SESSION[grp]) ? " selected": "";
    		$groupc .= "<option name=\"grp\" value=\"$row[0]\" $selected>$row[1]</option>";


	}//while

	return $groupc;

}//function setCombo
function setHist(){
	for($i=0;$i<12;$i++){
		if (!$_SESSION[hist]) $_SESSION[hist] = $row[0];
		$selected = ($i == $_SESSION[hist]) ? " selected": "";
    		$histrc .= "<option name=\"hist\" value=\"$i\">$i months ago</option>";
	}#for
	return $histrc;
}
function getGroup($db,$gid){
	$sql = "SELECT groupname FROM groups WHERE gid LIKE '$gid' ORDER BY groupname";
	$result = $db->getOne($sql);
	return $result;
}//getGroup

dblogout($db);


?> 
Return current item: Network Traffic Monitor