#!/usr/bin/perl

$VERSION="1.0.1";

if (@ARGV < 2)
{
	print "bw_ping v$VERSION (c) Mikko Korkalo 2007\n";
	print "Creates a CSV statistic containing interface traffic amount,\n";
	print "number of packets/s and latencies to specified hosts.\n";
	print "Usage: $0 <IP_ADDRESS>[,IP_ADDRESS] <INTERFACE> [OPTIONS]\n";
	print "OPTIONS:\n";
	print "--count=N       quit after N number of pings\n";
	print "--interval=N    ping interval\n";
	print "Example: $0 1.2.3.4 tun0 --interval=1\n";
	exit 1;
}

$count = -1;
$interval = 1;

for ($k=2; $k < @ARGV; $k++)
{
	if ($ARGV[$k] =~ /^\-\-count\=(.*)$/)
	{
		$count = int($1);
		if ($count < 1)
		{
			print "Invalid count $1\n";
			exit 1;
		}
	}
	elsif ($ARGV[$k] =~ /^\-\-interval\=(.*)$/)
	{
		$interval = int($1);
		if ($interval < 1)
		{
			print "Invalid interval $1\n";
			exit 1;
		}
	}
	else
	{
		print "Invalid argument $ARGV[$k]\n";
		exit 1;
	}
}

$interface = @ARGV[1];
my @ping_ips = split(",", @ARGV[0]);

sub gtime
{
	$date=`date +%d.%m.%Y\\ %H:%M:%S`;
	chop $date;
	return $date;
}

sub get_if_stats($)
{
	my ($if) = @_;
	
	#Inter-|   Receive                                                |  Transmit
	# face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
	#    lo:3080982167 9612147    0    0    0     0          0         0 3080982167 9612147    0    0    0     0       0          0
	#  eth0:       0       0    0    0    0     0          0         0 10154512   40475    0    0    0     0       0          0
	#  eth1:1593437645 244397066   87    0    0     0          0         0 474158275 184996662    5    0    1 5370273       4          0
	#  brg0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0

	$tmp = `cat /proc/net/dev`;
	#print $tmp;
	my @arr = (0, 0);	#rx bytes, tx bytes
	$regex = '\s*' . $if . ':\s*(\d+)\s+(\d+)\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)\s+(\d+)';
	#print "\n\nregex=$regex\n\n";
	if ($tmp =~ $regex) {
		$arr[0] = $1;
		$arr[1] = $2;
		$arr[2] = $3;
		$arr[3] = $4;
		#print "\n\nresult: $1 / $2\n\n";
		return @arr;
	}
	die "Error parsing /proc/net/dev";
}

print "Time of day;Seconds passed;Download rate (Kbps);Upload rate (Kbps);Download packets/s;Upload packets/s";

foreach $ipnow (@ping_ips) {
	print ";$ipnow (ms)"
}

print "\r\n";

$n=0;
@last_stats = get_if_stats($interface);
$last_stats_time = time();
$start_time = time();

while ($count == -1 || $n <= $count)
{
	sleep($interval);
	
	$n = $n + 1;
	print gtime();							#timestamp

	print ";" . (time() - $start_time);				#seconds passed

	my @stats = get_if_stats($interface);
	$time_now = time();
	$time_passed = $time_now - $last_stats_time;
	
	print ";" . int(($stats[0] - $last_stats[0]) / $time_passed * 8);	#rx bits/s
	print ";" . int(($stats[2] - $last_stats[2]) / $time_passed * 8);	#tx bits/s
	print ";" . int(($stats[1] - $last_stats[1]) / $time_passed);	#rx packets/s
	print ";" . int(($stats[3] - $last_stats[3]) / $time_passed);	#tx packets/s
	
	@last_stats = @stats;
	$last_stats_time = $time_now;
	
	foreach $ipnow (@ping_ips)
	{
		print ";";
		$pingret=`ping $interfaceparam2 $ipnow -c 1 -w 1`;
		#example:
		#64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.162 ms
		if ($pingret =~ /time\=(.*)\ ms/)
		{
			$pingtime = $1;
			$pingtime =~ s/\./\,/g;
			print $pingtime;
		}
		else
		{
			print "timeout/error";
		}
	}
	print "\r\n";
}

