#! /bin/bash

# default libexecdir used to bootstrap scripts
libexecdir=${GLOBUS_LOCATION}/libexec

# load GRIS common code and initialization
. ${libexecdir}/grid-info-common

MDS_INSTALL_PATH=$GLOBUS_LOCATION
gridftp_perf_log=`awk '/loglocation/ {print $2}' $MDS_INSTALL_PATH/etc/gridftp-resource.conf` 

ips=`cat $gridftp_perf_log | awk '{
	ind = index($13, "["); 
	ind1 = index($13, "]"); 
	ip = substr($13, ind+1, length($13)-ind-1);
	print ip;
}' | sort | uniq`

source_list=`echo $ips | sed 's/ /;/g'`

echo "" | awk '
BEGIN {
	gridftp_conf = "'$MDS_INSTALL_PATH'""/etc/gridftp-resource.conf";
	split("'$source_list'", ip_list, ";");
}

function process_static(filename)
{
	i = 1;
	while(ip_list[i]) {
		while((getline < filename) > 0) {
			if($1 == "dn:") {
				dn = "dn: ""client_ip="ip_list[i]","$2"'$_suffix'";
				print dn;
				print "cn: ", ip_list[i];
			}
			else {
				if($0 != "") {
					print;
				}
			}
		}
		process_results("'$gridftp_perf_log'", ip_list[i]);
		print "";
		close(filename);
		i++;
	}
}

function process_results(filename, ip)
{
	server_max_rd = 0;
	server_min_rd = 999999;
	server_sum_rd = 0;
	server_count_rd = 0;
	server_max_wr = 0;
	server_min_wr = 999999;
	server_sum_wr = 0;
	server_count_wr = 0;

	while((getline < filename) > 0) {

		# parse IP
		ind = 0;
		ind1 = 0;
		ind = index($13, "[");
		ind1 = index($13, "]");
		log_entry_ip = substr($13, ind+1, length($13)-ind-1);

		# parse start and end times
		ind = index($5, "="); 
		ind1 = index($5, "."); 
		s_sec = substr($5, ind+1, ind1-ind-1);
		s_usec = substr($5, ind1+1, length($5)-ind1);
		ind = index($1, "="); 
		ind1 = index($1, "."); 
		e_sec = substr($1, ind+1, ind1-ind-1);
		e_usec = substr($1, ind1+1, length($1)-ind1);

		if(s_sec != e_sec) {
			usec = (((999999-s_usec)+e_usec)/1000000.0);
			xfer_sec = ((e_sec-(s_sec+1))+usec);
		}
		else {
			xfer_sec = ((e_usec-s_usec)/1000000.0);
		}

		# parse file size
		ind = index($9, "=");
		file_sz = substr($9, ind+1, length($9)-ind);
		bandwidth = (file_sz / xfer_sec) / 1000;

		if(((log_entry_ip == ip) && (index($14, "RETR") > 0))) { 
			server_sum_rd += bandwidth;
			server_count_rd++;
			if(bandwidth > server_max_rd) {
				server_max_rd = bandwidth;
			}
			if(bandwidth < server_min_rd) {
				server_min_rd = bandwidth;
			}
			server_last_rd = bandwidth;
		}

		if(((log_entry_ip == ip) && (index($14, "STOR") > 0))) { 
			server_sum_wr += bandwidth;
			server_count_wr++;
			if(bandwidth > server_max_wr) {
				server_max_wr = bandwidth;
			}
			if(bandwidth < server_min_wr) {
				server_min_wr = bandwidth;
			}
			server_last_wr = bandwidth;
		}
	}
	close(filename);

	if(server_count_rd != 0) {
		print "Mds-Gridftp-Stats-maxrdbandwidth: ", int(server_max_rd), "KB/Sec";
		print "Mds-Gridftp-Stats-avgrdbandwidth: ", int(server_sum_rd / server_count_rd), "KB/Sec";
		print "Mds-Gridftp-Stats-minrdbandwidth: ", int(server_min_rd), "KB/Sec";
		print "Mds-Gridftp-Stats-lastrdbandwidth: ", int(server_last_rd), "KB/Sec";
		print "Mds-Gridftp-Stats-numrd: ", server_count_rd;
	}
	if(server_count_wr != 0) {
		print "Mds-Gridftp-Stats-maxwrbandwidth: ", int(server_max_wr), "KB/Sec";
		print "Mds-Gridftp-Stats-avgwrbandwidth: ", int(server_sum_wr / server_count_wr), "KB/Sec";
		print "Mds-Gridftp-Stats-minwrbandwidth: ", int(server_min_wr), "KB/Sec";
		print "Mds-Gridftp-Stats-lastwrbandwidth: ", int(server_last_wr), "KB/Sec";
		print "Mds-Gridftp-Stats-numwr: ", server_count_wr;
	}
	if((server_count_rd != 0) || (server_count_wr != 0)) {
		print "objectClass: MdsGridftpStats";
	}
}

{
	process_static(gridftp_conf);
}
END {
}'

echo ""
echo "RESULT"
echo "code: 0"
exit 0

