#!/usr/local/bin/perl
# wping (c) Richard@x-router.com
#
# - LICENSE
#
# GPL, you know the deal
#
#  - INSTALL
#
# Requires the following:
# Net::Ping -  http://www.perldoc.com/perl5.8.0/lib/Net/Ping.html
# Time::HiRes - http://www.perldoc.com/perl5.8.0/lib/Time/HiRes.html
# libgd &  gd.pm - http://www.boutell.com/gd/ - http://stein.cshl.org/WWW/software/GD/
#
# Ok get the above, chances are your system will have all but the gdlib & gd.pm
# Edit the config file to suit your needs then chuck this in the cron eg:
# 0-59 * * * * /home/richard/work/wping/src/wping.pl /home/richard/work/wping/src/wping.conf > /dev/null
# Jobs a goodun
#
# - Docs
#
# This took me a few hours to write and i may turn it into something more usefull at some point.
# Can't be arsed to document it past that tbh

use Net::Ping;
use Time::HiRes;
use GD;
use IO::File;

$ver="0.1a";

print "Config File @ARGV[0]\n";

do "@ARGV[0]" or die "Arg no config file";


sub ping_host {
	$p = Net::Ping->new("icmp");
	$p->hires();
	$i1=0;
	$tot=0;
	while($i1<5) {
		($ret, $duration, $ip) = $p->ping($_[0], 5.5);
		$tot+=$duration;
		$i1=$i1+1;
	}
	$tot=$tot/5;
	printf("$_[0] [ip: $ip] is alive (packet return time: %.2f ms)\n", 1000 * $tot);
	$p->close();
	return $tot * 1000;
}

sub draw_log {
	$fname=$_[0];
	$graph=new GD::Image($loglevel,200);
	$gwhite = $graph->colorAllocate(255,255,255);
	$ggreen = $graph->colorAllocate(0,255,0);
	$gred = $graph->colorAllocate(255,0,0);
	$gyellow = $graph->colorAllocate(255,238,0);
	$gtitleb =$graph->colorAllocate(7,4,163);
	$gtitlebg =$graph->colorAllocate(184,183,232);
	$graph->transparent($gwhite);
	$graph->interlaced('true');
	system("tail -$loglevel $fname > $fname.tail");
	system("mv $fname.tail $fname");
	open(LFP,"<$fname");
	
	$x=0;
	while (<LFP>) {
		$data=$_;
		$col=$ggreen;
		if($data>190) {$data=190;}
		if($data>50) {$col=$gyellow;}
		if($data>100) {$col=$gred;}
		$graph->line($x,199,$x,199-int($data),$col);
		$x=$x+1;
	}
	close(LFP);
	printf "Processed $x Results from the log\n";

	$graph->filledRectangle(0,0,($size*20),11,$gtitleb);
	$graph->filledRectangle(1,1,($size*20)-1,10,$gtitlebg);
	$graph->string(gdTinyFont,4,1,"wping v$ver (c) Richard\@x-router.com",$gtitleb);
	open(FP,">$fname.png");
	binmode FP;
	print FP $graph->png;
	close(FP);
	
}

sub draw_ping {
	$pos=$_[0];
	$ping=$_[1];
	$hid=$_[2];
	$fping=sprintf("%.2f",$_[1]);
	$img->rectangle($pos*20,199,($pos*20)+20,199-$ping,$black);
	$fcol=$green;
	if($ping>30) { $fcol=$yellow; }
	if($ping>50) { $fcol=$red; }
	if($ping>1) { $img->fill(($pos*20)+1,198,$fcol); }
	$img->stringUp(gdTinyFont,$pos*20,130,"$hid",$black);
	$img->stringUp(gdTinyFont,($pos*20)+10,130,"$fping ms",$blue);
}

sub main {
	$size=@host;
	printf "wping v$ver Hosts $size\n";
	if ($size < 10) {
		$size=10;
	}
	$img=new GD::Image(($size*20)+1,200);
	$white = $img->colorAllocate(255,255,255);
	$black = $img->colorAllocate(0,0,0);
	$red = $img->colorAllocate(255,0,0);
	$blue = $img->colorAllocate(0,0,255);
	$green = $img->colorAllocate(0,255,0);
	$yellow = $img->colorAllocate(255,238,0);
	$titleb =$img->colorAllocate(7,4,163);
	$titlebg =$img->colorAllocate(184,183,232);
	$img->transparent($white);
	$img->interlaced('true');
	$i=0;
	foreach (@host) {
		$pres=ping_host ($_);
		$dataf=new IO::File;
		$dataf->open(">>$outdir/data/$_");
		print $dataf "$pres\n";
		$dataf->close;
		draw_ping ($i,$pres,$_);
		draw_log ("$outdir/data/$_");
		$i++;
	}
	$img->filledRectangle(0,0,($size*20),11,$titleb);
	$img->filledRectangle(1,1,($size*20)-1,10,$titlebg);
	$img->string(gdTinyFont,4,1,"wping v$ver (c) Richard\@x-router.com",$titleb);

	open(FP,">$outdir/pings.png");
	binmode FP;
	print FP $img->png;
	close(FP);
}

sub htmls {
	open(HFP,">$outdir/index.html");
	printf HFP "<html><head><title>W:P:I:N:G</title></head><body bgcolor=#003333>";
	printf HFP "<center><table bgcolor=257f05><tr><td><img src=pings.png></td></tr></table><p>";
	foreach (@host) {
		printf HFP "<table><tr><td bgcolor=257f05>$_</td></tr><tr><td><img src=%s/data/%s.png></td></tr></table><br>",$url,$_;
	}
	printf HFP "</center></body></html>";
	close(HFP);
}

htmls();
main();
