#!/bin/bash

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin
export PATH

function s_load_processes() {
 awk '{ 
 split($4,a,"/");
 printf("load avg: \t\t\t%2.2f %2.2f %2.2f \nprocesses (active/total):\t\t%2.0f/%3.0f\n",$1,$2,$3,a[1],a[2]) ;}' < /proc/loadavg
}

function s_connections() {
echo -e "active rsync connections: \t\t`pidof rsyncd|wc -w`"
echo -e "active  http connections: \t\t`w3m -dump http://172.16.10.1/server-status | awk '/currently being processed/ { print $1 - 1; }'`"
ftpsessions=$(pidof vsftpd|wc -w)
ftpsessions=$(( $ftpsessions / 2 ))
echo -e "active   ftp connections: \t\t$ftpsessions"
}

function getstats_all() {
{
for i in $interfaces; do
  ip -s link show $i
done
} | \
        awk '
        /RX:/ { getline; rbytes=rbytes + $1; rpackets=rpackets + $2; }
        /TX:/ { getline; sbytes=sbytes + $1; spackets=spackets + $2; }
        END { printf("sbytes=%s\nspackets=%s\nrbytes=%s\nrpackets=%s\n", sbytes, spackets, rbytes, rpackets ); }
        '
}

function s_bw() {

interfaces=""

for i in $( cd /proc/sys/net/ipv4/conf; ls -1); do
	case $i in
		all|lo|default)
		;;
		*)
		interfaces="$interfaces $i"
		;;
	esac
done

export interfaces


eval `getstats_all`

arb=$rbytes
arp=$rpackets
asb=$sbytes
asp=$spackets

sleep 1

eval `getstats_all`

bin=`echo "(${rbytes}-${arb})/1024" | bc`
pin=`echo "${rpackets}-${arp}" | bc`
bout=`echo "(${sbytes}-${asb})/1024" | bc`
pout=`echo "${spackets}-${asp}" | bc`

echo -e "current  input: \t\t$bin kB/s ($pin packets/s)"
echo -e "current output: \t\t$bout kB/s ($pout packets/s)"

}

function s_bandwidth() {
declare -a a
a=(`
iptables -L -vnx|awk '/OUTPUT/ { n=0; m=0; while ( $m != "bytes)" ) { n=n+1; m=n+1; }; print $n; next; } 
                      /INPUT/ { n=0; m=0; while ( $m != "bytes)" ) { n=n+1; m=n+1; }; print $n; next; } 
                      {next;}'
sleep 1
iptables -L -vnx|awk '/OUTPUT/ { n=0; m=0; while ( $m != "bytes)" ) { n=n+1; m=n+1; }; print $n; next; }
                      /INPUT/ { n=0; m=0; while ( $m != "bytes)" ) { n=n+1; m=n+1; }; print $n; next; }
                      {next;}'
`)

aout=${a[0]}
ain=${a[1]}
bout=${a[2]}
bin=${a[3]}

out=`echo "${bout}-${aout}" |bc`
out=`echo "$out/1024"|bc`

in=`echo "${bin}-${ain}" |bc`
in=`echo "$in/1024"|bc`

echo -e "current output: \t\t\t$out kB/s"
echo -e "current  input: \t\t\t$in kB/s"
}

s_load_processes
s_connections
s_bw
pos

echo "==========================================="
rcon.pl-nexctf "status 1"
echo "==========================================="
rcon.pl-nexuiz "status 1"
echo "==========================================="
rcon.pl-nex1v1 "status 1"
