#!/usr/local/bin/perl

# Written by Ilker TEMIR (ilker@ulak.net.tr)
# Idea taken from Senol GULGONUL (senol@satko.com.tr)
# 11.28.1998

if ($ENV{'REQUEST_METHOD'} eq "POST") { 
  read (STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
} else { 
  $buffer=$ENV {'QUERY_STRING'};
}
@pairs=split(/&/,$buffer);
foreach $pair (@pairs) {
  ($name,$value)=split (/=/,$pair);
  $value=~ tr/+/ /;
  $value=~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  $contents{$name}=$value;
}

$basedir="/export/home/ilker/private/marketweb/portmon";
$logdir="$basedir/logs";
$portname=$contents{'portname'};
$day=$contents{'day'};
$month=$contents{'month'};
$year=$contents{'year'};
if (($portname eq "") or ($day eq "") or ($month eq "") or ($year eq "")) {
	print "Content-type: text/html\n\n";
	print "<BODY BGCOLOR=#FFFFFF TEXT=#000000>";
	print "<TITLE>Customer Traffic Monitor</TITLE>";
	print "<CENTER><BR><IMG SRC=http://www.ulak.net.tr/img/logoUlakbim.gif><BR><BR><BR>\n";
	print "<FORM ACTION=portmon.pl METHOD=POST>\n";
	print "<SELECT NAME=portname>\n";
	open (cffile,"$basedir/portmon.cf");
	while ($cfline=<cffile>) {
		($connname,$portspeed,$porttraffic,$desc,$nas,$community,$network)=split (/:/,$cfline);
		print "<OPTION>$connname\n";
	}
	close (cffile);
	print "</SELECT><BR>\n";
	print "<SELECT NAME=day>\n";
	$thisday=`date +%d`;
	$thismonth=`date +%m`;
	$thisyear=`date +%Y`;
	foreach $day (1..31) {
		if ($thisday == $day) {
			print "<OPTION SELECTED>\n";
		} 
		else {
			print "<OPTION>\n";
		}
		if ($day < 10) {
			print "0$day\n";
		}
		else {
			print "$day\n";
		}
	}
	print "</SELECT>\n";
	print "<SELECT NAME=month>\n";
	foreach $month (1..12) {
		if ($thismonth == $month) {
			print "<OPTION SELECTED>\n";
		} 
		else {
			print "<OPTION>\n";
		}
		if ($month < 10) {
			print "0$month\n";
		}
		else {
			print "$month\n";
		}
	}
	print "</SELECT>\n";
	print "<SELECT NAME=year>\n";
	print "<option value=00 selected>2000\n";
	print "<option value=99>1999\n";
	print "<option value=98>1998\n";
#	foreach $year (98..99) {
#		if ($thisyear == $year) {
#			print "<OPTION SELECTED>$year\n";
#		} 
#		else {
#			print "<OPTION>$year\n";
#		}
#	}
	print "</SELECT>\n<BR>\n";
	print "Control Password: <INPUT TYPE=PASSWORD NAME=password SIZE=8><BR>\n";
	print "<INPUT TYPE=SUBMIT>\n<BR><BR><BR>\n";
	print "Port Traffic Monitor v2.0 - © 1998 by Ilker TEMIR\n";
	exit (0);
}		
	

$ysize=640;	# I prefer using x & y inverted :)
$xsize=970;

use GD;

open (authfile,"$basedir/portmon.auth");
$adminpwd=<authfile>;
chop ($adminpwd);
close (authfile);
open (cffile,"$basedir/portmon.cf");
while ($cfline=<cffile>) {
	($connname,$tempportspeed,$tempporttraffic,$desc,$nas,$community,$network,$cryptedpassword)=split (/:/,$cfline);
	if ($connname eq $portname) {
		$porttraffic=$tempporttraffic;
		$portspeed=$tempportspeed;
		$portdesc=$desc;
		$password=$cryptedpassword;
	}
}
close (cffile);
if ((crypt ($contents{'password'},$password) ne $password) and (crypt ($contents{'password'},$adminpwd) ne $adminpwd)) {
	print "Content-type: text/html\n\n";
	print "<BODY BGCOLOR=#FFFFFF TEXT=#000000>";
	print "Incorrect password supplied or insufficient priviliges.<BR>\n";
	print "Please contact <AHREF=mailto:ilker\@ulak.net.tr>ilker\@ulak.net.tr</A> if you think that this message is incorrect.<BR>\n";
	exit (1);
}
print "Content-type: image/gif\n\n";
$image = new GD::Image($ysize,$xsize);
$white = $image->colorAllocate(255,255,255);
$black = $image->colorAllocate(0,0,0);       
$gray = $image->colorAllocate(220,220,220);
$red = $image->colorAllocate(255,0,0);      
$blue = $image->colorAllocate(0,0,255);
$image->rectangle(0,0,$ysize-1,$xsize-1,$black);
$image->rectangle(2,2,$ysize-3,$xsize-3,$black);
foreach $x (0..16) {
	$image->line(30,30+$x*20,30+2*288,30+$x*20,$gray);
	$xtemp=int ((16-$x)*($portspeed/16));
	if ($xtemp<100) {
		$xtemp=" $xtemp";
	}
	if ($xtemp<10) {
		$xtemp=" $xtemp";
	}
	$image->string(gdTinyFont,12,27+$x*20,$xtemp,$black);
}
$image->string(gdTinyFont,12,19,"Speed (kbps)",$black);
foreach $y (0..24) {
	$ytemp=30+$y*24;
	$image->line($ytemp,30,$ytemp,30+16*20,$gray);
	$image->string(gdTinyFont,$ytemp-5,35+16*20,$y,$black);
}
$image->string(gdTinyFont,590,45+16*20,"Time",$black);
$image->rectangle(29,29,31+2*288,31+16*20,$black);
$image->rectangle(30,30,30+2*288,30+16*20,$black);
$image->stringUp(gdSmallFont,$ysize-15,$xsize-250,"Port Traffic Monitor v2.0 (c) by Ilker TEMIR - ilker\@ulak.net.tr",$black);
#$image->stringUp(gdSmallFont,$ysize-15,$xsize-110,"(c) by Ilker TEMIR - ilker\@ulak.net.tr",$black);

# Done with image setup. Now display traffic

$curxin=0;
$curyin=0;
$curxout=0;
$curyout=0;
$samplenum=0;

$inputmax=0;
$inputmin=$portspeed;
$inputavg=0;
$inputtraffic=0;

$outputmax=0;
$outputmin=$portspeed;
$outputavg=0;
$outputtraffic=0;
open (logfile,"$logdir/$portname/$day$month$year.bps") || goto SKIP;
while ($loginput=<logfile>) {
	$samplenum++;
 	($time,$inbps,$outbps,$space,$inputoctets,$space,$outputoctets,$space,$previnputoctets,$space,$prevoutputoctets)=split (/ /,$loginput);
	($hour,$minute)=split (/:/,$time);
	$inputtraffic=$inputtraffic+$inputoctets-$previnputoctets;
	$outputtraffic=$outputtraffic+$outputoctets-$prevoutputoctets;
	$fiveminperiod=($hour*60+$minute)/5;
	$inputavg=$inputavg+$inbps;
	if ($inbps < $inputmin) {
		$inputmin=$inbps;
	}
	if ($inbps > $inputmax) {
		$inputmax=$inbps;
	}
	$outputavg=$outputavg+$outbps;
	if ($outbps < $outputmin) {
		$outputmin=$outbps;
	}
	if ($outbps > $outputmax) {
		$outputmax=$outbps;
	}
	$tinbps=int ($inbps/1000/($portspeed/16)*20);
	$toutbps=int ($outbps/1000/($portspeed/16)*20); 
	$image->line(30+$curyin,30+16*20-$curxin,30+$fiveminperiod*2,30+16*20-$tinbps,$red);
	$image->line(30+$curyout,30+16*20-$curxout,30+$fiveminperiod*2,30+16*20-$toutbps,$blue);
	$curyin=$fiveminperiod*2;
	$curxin=$tinbps;
	$curyout=$fiveminperiod*2;
	$curxout=$toutbps;
#	print "$hour $minute $fiveminperiod $tinbps\n";
}
close (logfile);

$inputavg=int ($inputavg/$samplenum);
$outputavg=int ($outputavg/$samplenum);
$inputtraffic=int ($inputtraffic/1000000);
$outputtraffic=int ($outputtraffic/1000000);

# $image->rectangle(30,30,30+2*288,30+16*20,$black);
# Now show legend

CONTINUE:
$totaltraffic=$inputtraffic+$outputtraffic;
$image->string(gdSmallFont,30,60+16*20,"Port Name      : $portname ($portdesc)",$black);
$image->string(gdSmallFont,30,75+16*20,"Date           : $day/$month/$year",$black);
$image->string(gdSmallFont,30,90+16*20,"Input          : $inputmin/$inputavg/$inputmax",$red);
$image->string(gdSmallFont,30,105+16*20,"Output         : $outputmin/$outputavg/$outputmax",$blue);
$image->string(gdSmallFont,30,120+16*20,"Input Traffic  : $inputtraffic MB",$red);
$image->string(gdSmallFont,30,135+16*20,"Output Traffic : $outputtraffic MB",$blue);
$image->string(gdSmallFont,30,150+16*20,"Total Traffic  : $totaltraffic MB",$black);

$trafficlimit=$porttraffic;
$trafficstep=int ($trafficlimit/16);
foreach $x (0..16) {
	$image->line(30,500+30+$x*20,30+2*288,500+30+$x*20,$gray);
	$xtemp=int ((16-$x)*($trafficlimit/16));
	if ($trafficlimit>=10000) {
		$xtemp=int ($xtemp/1000);
	}
	if ($xtemp<1000) {
		$xtemp=" $xtemp";
	}
	if ($xtemp<100) {
		$xtemp=" $xtemp";
	}
	if ($xtemp<10) {
		$xtemp=" $xtemp";
	}
	$image->string(gdTinyFont,7,500+27+$x*20,$xtemp,$black);
}
if ($trafficlimit>=10000) {
	$image->string(gdTinyFont,7,500+19,"Traffic (GB)",$black);
}
else {
	$image->string(gdTinyFont,7,500+19,"Traffic (MB)",$black);
}
foreach $y (1..31) {
	$ytemp=30+18*$y;
	$image->line($ytemp,500+30,$ytemp,500+30+16*20,$gray);
	$image->string(gdTinyFont,$ytemp-5,500+35+16*20,$y,$black);
}
$image->string(gdTinyFont,590,500+45+16*20,"Day",$black);
$image->rectangle(29,500+29,31+2*288,500+31+16*20,$black);
$image->rectangle(30,500+30,30+2*288,500+30+16*20,$black);

$month=$month+1-1;
if ($month < 10) {
	$tmonth="0$month"
}
else {
	$tmonth=$month;
}
$year=$contents{'year'};
$total=0;
$min=$trafficlimit;
$max=0;
$samplenum=0;
$curx=0;
$cury=0;
foreach $dayofmonth (1..$contents{'day'}) {
	if ($dayofmonth < 10) {
		$tday="0$dayofmonth";
	}
	else {
		$tday=$dayofmonth;
	}
	open (logtotal,"$logdir/$portname/$tday$tmonth$year.bps") || goto SKIPDAY;
	$samplenum++;
	$dailytotal=0;
	while ($logline=<logtotal>) {
	 	($time,$inbps,$outbps,$space,$inputoctets,$space,$outputoctets,$space,$previnputoctets,$space,$prevoutputoctets)=split (/ /,$logline);
		$dailytotal+=$inputoctets-$previnputoctets+$outputoctets-$prevoutputoctets;
	}
	$dailytotal=int ($dailytotal/1000000);
	$total+=$dailytotal; 
	if ($dailytotal > $max) {
		$max=$dailytotal;
	}
	if ($dailytotal < $min) {
		$min=$dailytotal;
	}	
	close (logtotal);
	$image->line (30+$curx,500+30+16*20-$cury,30+$tday*18,500+30+16*20-int ($total*20/$trafficstep),$black);
	$image->line (31+$curx,500+30+16*20-$cury,31+$tday*18,500+30+16*20-int ($total*20/$trafficstep),$black);
	$curx=$tday*18;
	$cury=int ($total/$trafficstep*20);
SKIPDAY:
}
$average=int ($total/$samplenum);
$image->string (gdSmallFont,30,500+60+16*20,"Total traffic     : $total MB",$black);
$image->string (gdSmallFont,30,500+75+16*20,"Minimum traffic   : $min MB",$black);
$image->string (gdSmallFont,30,500+90+16*20,"Maximum traffic   : $max MB",$black);
$image->string (gdSmallFont,30,500+105+16*20,"Average tarffic   : $average MB",$black);
		
binmode STDOUT;
print $image->png;
exit (0);

SKIP:
$inputmin=0;
$outputmin=0;
$image->string(gdLargeFont,210,173,"!! NO DATA ON $day/$month/$year !!",$red);
goto CONTINUE;
