<?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 takes care of drawing the graphs
* JPgraph is used
* check the jpgraph directory 4 documentation and examples
* parameters can be passed with the image tag as get values
* One get value is used to define if it is a daily, monthly of yearly graph
*/
require_once("../auth.php");
include ("jpgraph.php");
include ("jpgraph_bar.php");
include ("jpgraph_line.php");
include ('../config.php');
$db = dblogin();
$uid = $_SESSION[uid];
$iface = $_GET['i'];
$hist = 0;
$speed = $_GET['s'];
$logtype = $_SESSION['logtype'];
$sql;
$higher = date("Y-m-d H:");
$h = date(" H:");
$d = date("d");
$d -= 1;
if($d < 10) $d = "0$d";
if($d == 00){
$m = date("m") -1;
$y = date("Y");
if($m < 10) $m="0$m";
if(checkdate($m,31,$y)) $lower = "$y-$m-31$h";
else if(checkdate($m,30,$y)) $lower = "$y-$m-30$h";
else if(checkdate($m,29,$y)) $lower = "$y-$m-29$h";
else if(checkdate($m,28,$y)) $lower = "$y-$m-28$h";
}
else{
$ym = date("Y-m-");
$lower = $ym.$d.$h;
}
$sql = "SELECT date_part('hour', porttraffic.times) as timer, (porttraffic.incoming * 8 / 300), (porttraffic.outgoing * 8 / 300), date_part('epoch', times) AS controll, porttraffic.times FROM ports
INNER JOIN porttraffic ON porttraffic.pid = ports.pid
INNER JOIN portgroups ON portgroups.pid=ports.pid
INNER JOIN groups ON portgroups.gid = groups.gid
WHERE porttraffic.times BETWEEN '$lower%' AND '$higher%' AND ports.pid=$iface
ORDER BY porttraffic.times ASC";
$sqlval = "SELECT avg(porttraffic.incoming*8/300),avg(porttraffic.outgoing*8/300), max(porttraffic.incoming*8/300), max(porttraffic.outgoing*8/300), min(porttraffic.incoming*8/300), min(porttraffic.outgoing*8/300) FROM ports
INNER JOIN porttraffic
ON porttraffic.pid = ports.pid
INNER JOIN portgroups ON portgroups.pid=ports.pid
INNER JOIN groups ON portgroups.gid = groups.gid
WHERE porttraffic.times BETWEEN '$lower%' AND '$higher%'
AND ports.pid=$iface";
#echo "sqlval: $sqlval<br />";
$day = date("d");
$day -= 1;
#take in account: last day of month, last day of year!!
if($day < 10) $day="0$day";
$month = date("m");
$year = date("Y");
$hour = date("H");
$min = date ("i");
# echo "$year: $month: $day $hour<br />";
$startvalue = mktime($hour,0,0,$month,$day,$year);
# echo "create startv: $startvalue<br />";
#echo "sqlval: $sqlval<br />";
$val = $db->getRow($sqlval);
$avin = convertBW($val[0]);
$avout = convertBW($val[1]);
$maxin = convertBW($val[2]);
$maxout= convertBW($val[3]);
$minin = convertBW($val[4]);
$minout= convertBW($val[5]);
$result = $db->query($sql);
$j=0;
$xvalues = array();
$yvalues = array();
while($row = $result->fetchrow())
{
if($j == 0){
$previous = $row[3];
}
$timing = $row[3] - $previous;
if($timing < 330){
$xvalues[] = $row[3];
$in[] = $row[1];
$out[] = $row[2];
$per[] = $percentile;
}
else{
$previous +=5;
$xvalues[] = $previous;
$in[] = 0;
$out[] = 0;
$per[]=$percentile;
$previous = $row[3] - 5;
$xvalues[]= $previous;
$in[]=0;
$out[]=0;
$per[]=$percentile;
$xvalues[] = $row[3];
$in[] = $row[1];
$out[] = $row[2];
$per[] = $percentile;
}
if($type=="month" || $type=="year"){
#echo "month timing: $timing<br />";
if($timing<4000){
$xvalues[] = $row[3];
$in[] = $row[1];
$out[] = $row[2];
$per[] = $percentile;
$timeref = $xvalues[0];
}
else{
$previous +=5;
$xvalues[] = $previous;
$in[] = 0;
$out[] = 0;
$per[]=$percentile;
$previous = $row[3] - 5;
$xvalues[] = $previous;
$in[]=0;
$out[]=0;
$per[]=$percentile;
$xvalues[]=$row[3];
$in[] = $row[1];
$out[] = $row[2];
$per[] = $percentile;
}
}//if type year or month
$previous = $row[3];
$j++;
}
if($j == 0){
echo "No data available<br />";
}
else{
$graph = new Graph(405,250,"filename");
$graph->img->SetImgFormat("png");
$graph->img->SetMargin(60,15,10,130);
# $graph->SetShadow();
if(count($in)==0) $in[0]=0;
if(count($out)==0) $out[0]=0;
if(count($xvalues)==0) $xvalues[0]=0;
$inplot = new LinePlot($in, $xvalues);
$oplot = new LinePlot($out, $xvalues);
$percent = new LinePlot($per, $xvalues);
$inplot->SetColor("blue");
$oplot->SetColor("darkgreen");
$percent->SetColor("red");
$inplot->SetLegend("In avg: $avin max: $maxin min: $minin");
$oplot->SetLegend("Out avg: $avout max: $maxout min: $minout");
$perce = convertBW($percentile);
$percent->SetLegend("95 Percentile: $perce");
$graph->legend->Pos(0.005,0.96,"left","bottom");
# echo "scale startv: $startvalue<br />";
$graph->SetScale("intint",0,0,$startvalue,$startvalue + (3600*24));
$graph->xaxis->SetLabelFormatCallback('TimeCallback');
$graph->xaxis->SetLabelAngle(90);
$graph->xaxis->scale->ticks->Set(2*3600,3600);
$graph->xaxis->title->Set("Hour");
$graph->xaxis->SetTitlemargin(35);
$graph->yaxis->SetTitlemargin(45);
$graph->xaxis->SetTickLabels($time);
$graph->yaxis->title->Set("Bits Per Second");
$graph->ygrid->Show();
$graph->xgrid->show();
$graph->yaxis->SetLabelFormatCallback('yScaleCallback');
$graph->Add($inplot);
$graph->Add($oplot);
$graph->Stroke();
}
function yScaleCallback($aVal) {
# $ref = $aVal % 1000;
$ref = $aVal;
if($ref > 999999 ){
$val = $aVal / (1000 * 1000);
$scale = "$val M";
}//if mega
else if($ref > 999){
$val = $aVal/1000;
$scale = "$val K";
}
else if($ref > 0){
$val = $aVal;
$scale = "$val";
}
return $scale;
}
function TimeCallback($aVal) {
return Date('H:i',$aVal);
}
function DayCallback($aVal) {
return Date('D:d',$aVal);
}
function MonthCallback($aVal) {
return Date('M:m',$aVal);
}
function calculatePercentile($db, $sqlp){
global $writer;
$query = $db->query($sqlp);
$i=0;
while($row = $query->fetchrow())
{ if($i != 0){
$inter = $previous - $row[0];
$writer .= "time: $inter : value: $value ";
if($inter < 390 && $inter > 210){
if($row[1]>$row[2]){
$value = $row[1]/$inter;
$values[] = $value;
$writer .="row1 added \n";
}
if($row[2]>$row[1]){
$value = $row[2]/$inter;
$values[] = $value;
$writer .="row2 added \n";
}
if($row[1]==$row[2]){
$value = $row[2]/$inter;
$values[] = $value;
$writer .="equa added \n";
}
}//if $inter correct
$wirter .="\n";
}//if $i != 0
$previous = $row[0];
$i++;
$writer .="\n";
}//while
$writer .="\n";
$arraysize = count($values);
#echo "arraysize: $arraysize)<br />";
if(count($values)==0){$values[0]=0;}
$arraysize=count($values);
#echo "new arraysize: $arraysize<br />";
$writer .= "arraysize: $arraysize \n";
$rn = (($arraysize)* 0.95);
$rn -= 1;
$sorted = $values;
sort($sorted);
$writer .="----------------------";
$i=0;
foreach ($sorted as $element){$writer .= "$i: $element \n"; $i++;}
$elements = count ($sorted);
$val = $elements / 20;
$rest = $elements % 20;
$frn = floor($rn) ;
$crn = $frn + 1;
$interpol = ($rn - $frn) * ($sorted[$crn] - $sorted[$frn]);
$percentile = $sorted[$frn] + $interpol;
$writer .="rn $rn, crn: $crn frn: $frn \n";
$calculation = "interpol = ($rn - $frn) * ($sorted[$crn] - $sorted[$frn]);\n percentile = $sorted[$frn] + $interpol = $percentile\n";
$debug = fopen("/tmp/95debug","w");
$writer .= ("sql: $sqlp \n arraysize: $arraysize elements: $elements --- rijnr: $val --- rest: $rest \nfrn: $frn --- crn: $crn --- percentile: $percentile \n calculation: $calculation\n");
$writer .="\n \n floorvalue= $sorted[$frn] ceiling: $sorted[$crn]\n";
$writer .= "rownr: $rn\n";
fwrite($debug, $writer);
return $percentile;
}//function calculatePercentile
function convertBW($iref){
if($iref >= 0){
if($iref > 1024*1024*1024) {
$ref = $iref/(1024*1024*1024);
$iref = round ($ref, 2);
$iref = "$iref Gbps";
}
else if ($iref > 1024*1024 ){
$ref = $iref/(1024*1024);
$iref = round ($ref, 2);
$iref = "$iref Mbps";
}
else if ($iref > 1024){
$ref =$iref/1024;
$iref = round ($ref, 2);
$iref = "$iref Kbps";
}
else {
$iref = round($iref,2);
$iref = "$iref bps";
}
}
return $iref;
}
dblogout($db);
?>