<?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();
$type = $_GET['graph'];
$uid = $_SESSION[uid];
$iface = $_GET['i'];
# $p = $_GET['p'];
$hist = $_GET['h'];
$speed = $_GET['s'];
$logtype = $_SESSION['logtype'];
$sql;
switch ($type){
case '24h':{
$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;
}
# date_part('hour',
$sql = "SELECT (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 />";
}
break;
case 'day':{
$today = date("Y-m-d");
$sql = "SELECT date_part('hour', porttraffic.times) as times, (porttraffic.incoming * 8 / 300), (porttraffic.outgoing * 8 / 300), date_part('epoch', times) AS controll 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 LIKE '%$today%' 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 LIKE '%$today%' AND ports.pid=$iface";
$day = date("d");
$month = date("m");
$year = date("Y");
$startvalue = mktime(0,0,0,$month,$day,$year);
}//case day
break;
case'month':{
$month = date("m")-$hist;
if($month < 10) $month = "0".$month;
$today = date("Y-").$month;
$yr=date("Y-");
$verify = $yr."0";
if($today==$verify)$today = 12;
$lastmonth = $month - 1;
$prevday = date("d") - 1;
#echo "lastmont: $lastmonth prevday: $prevday today: $today<br />";
# if($prevday < 10) $prevday = 0."$prevday";
# if($lastmonth < 10) $lastmonth = 0."$lastmonth";
$lastlog = date("Y").$lastmonth.("-").$prevday.(" 23:55");
$sql = "SELECT date_part('day', hourtraffic.times) AS times, (hourtraffic.incoming * 8 / 3600), (hourtraffic.outgoing * 8 / 3600 ), date_part('epoch', times) AS controll
FROM ports
INNER JOIN hourtraffic ON hourtraffic.pid = ports.pid
INNER JOIN portgroups ON portgroups.pid=ports.pid
INNER JOIN groups ON portgroups.gid = groups.gid
WHERE hourtraffic.times LIKE '%$today%' AND ports.pid=$iface ORDER BY hourtraffic.times ASC";
$sqlval = "SELECT avg(hourtraffic.incoming *8 /3600),avg(hourtraffic.outgoing*8/3600), max(hourtraffic.incoming*8/3600), max(hourtraffic.outgoing*8/3600), min(hourtraffic.incoming*8/3600), min(hourtraffic.outgoing*8/3600) FROM ports
INNER JOIN hourtraffic
ON hourtraffic.pid = ports.pid
INNER JOIN portgroups ON portgroups.pid=ports.pid
INNER JOIN groups ON portgroups.gid = groups.gid
WHERE hourtraffic.times LIKE '%$today%' AND ports.pid=$iface";
$sqlp = "SELECT DISTINCT date_part('epoch', times) AS times, (porttraffic.incoming * 8 ), (porttraffic.outgoing * 8 )
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 LIKE '%$today%' AND ports.pid=$iface OR porttraffic.times LIKE '%$lastlog%' ORDER BY times DESC ";
$month = date("m");
$month = $month - $hist;
if($month < 10) $month = "0".$month;
$year = date("Y");
$startvalue = mktime(0,0,0,$month,1,$year);
$dayinMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
}//month
break;
case 'year':{
$today = date("Y");
$sql = "SELECT date_part('month', hourtraffic.times) as times, (hourtraffic.incoming * 8 / 3600), (hourtraffic.outgoing * 8 / 3600), date_part('epoch', times) AS controll
FROM ports
INNER JOIN hourtraffic ON hourtraffic.pid = ports.pid
INNER JOIN portgroups ON portgroups.pid=ports.pid
INNER JOIN groups ON portgroups.gid = groups.gid
WHERE hourtraffic.times LIKE '%$today%' AND ports.pid=$iface ORDER BY hourtraffic.times ASC";
$sqlval = "SELECT avg(hourtraffic.incoming * 8 / 3600),avg(hourtraffic.outgoing * 8 / 3600), max(hourtraffic.incoming * 8 / 3600), max(hourtraffic.outgoing * 8 / 3600), min(hourtraffic.incoming * 8 / 3600), min(hourtraffic.outgoing * 8 / 3600) FROM ports
INNER JOIN hourtraffic
ON hourtraffic.pid = ports.pid
INNER JOIN portgroups ON portgroups.pid=ports.pid
INNER JOIN groups ON portgroups.gid = groups.gid
WHERE hourtraffic.times LIKE '%$today%' AND ports.pid=$iface";
$year = date("Y");
$startvalue = mktime(0,0,0,1,1,$year);
if(date("L") == 0) $numYear = 365;
if(date("L") == 1) $numYear = 366;
}//year
}//switch
$writer .=" sql: $sql \n";
$burst = $db->getOne("SELECT burstable from ports WHERE pid=$iface");
if($type == 'month'){
if($logtype == 'Administrator' || $logtype == 'Finance' || $burst=='t'){
$percentile = calculatePercentile($db, $sqlp);
$percentile = round($percentile, 3);
}
}//
#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{
# if($p=="home"){
# $graph = new Graph (300, 150,"filename");
# }
# else{
$graph = new Graph(600,300,"filename");
# }
$graph->img->SetImgFormat("png");
$graph->img->SetMargin(60,40,20,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");
# $oplot->setFillColor("hide@address.com");
$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.03,0.96,"left","bottom");
switch ($type){
case '24h':{
# 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");
}#case 24h
case 'day':{
//12 samples / hour
$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");
}//case day
break;
case 'month':{
$graph->SetScale("intint",0,0,$startvalue,$startvalue + (3600 *24 * $dayinMonth));
$graph->xaxis->SetLabelFormatCallback('DayCallback');
$graph->xaxis->SetLabelAngle(90);
$graph->xaxis->scale->ticks->Set(3600 *24 * 2 ,3600);
$graph->xaxis->title->Set("Day");
if ($logtype == "Administrator" || $logtype =="Finance" || $burst=="t"){
$graph->Add($percent);
}
}//case month
break;
case 'year':{
$graph->SetScale("intint",0,0,$startvalue,$startvalue + (3600 *24 * $numYear));
$graph->xaxis->SetLabelFormatCallback('MonthCallback');
$graph->xaxis->SetLabelAngle(90);
$graph->xaxis->scale->ticks->Set(3600 *24 * 31 /*12 *2*/ ,3600);
$graph->xaxis->title->Set("Month");
}//case year
break;
}//switch scales
$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);
?>