#!/bin/sh
# Written by Patrick Nguyen - MegaFork.
# Description:
# Adjust the stats files ( you may want to put this in a crontab ).
# WARNING: both INSTALL and GETFILE logging must be enabled. Will remove
# comments written in the stat files. Need fsp.271mf ....
# Wrote it on request. I may not provide support for it.
# Bugs: will do a big mess if ppl up/dl files named INSTALL or GETFILE
#       will not take into account files/sites(!) containing ERROR.
#       doesn't look like bc likes bignums.
# Where you log the transfers.
FSP_LOGFILE=/home/megafork/fsp/home/.log

# where should the stats go. Directory must exist.
FSP_STATDIR=/home/megafork/fsp/stat # shouldn't contain '_' char(s)

# Not used. Your FSP home.( if you want to put automatic warnings, this should
# be useful
FSP_HOME=/home/megafork/fsp/home

# the needed ratio.
MIN_RATIO=3 	# Minimum Up/Dl ratio, in percent.( xx uploads per download )

# You shouldn't need to modify anything below that line.
for type in up down
do
  echo ${type}loads:
  if [ $type = up ] ;then ACTION=INSTALL ; else ACTION=GETFILE ;fi
  grep $ACTION $FSP_LOGFILE|grep -v ERROR|cut -d' ' -f6,10
  HOST=`grep INSTALL $FSP_LOGFILE|grep -v ERROR|cut -d' ' -f6`
  IP=`nslookup $HOST|head -5|tail -1|cut -d: -f2`
  IP=`echo $IP` # Strip off the spaces.. prolly not necessary
  if [ -f $FSP_STATDIR/${IP}_${type}loads ] ;then
	STAT=`cat $FSP_STATDIR/${IP}_${type}loads|head -1`
  else  STAT=0
  fi
  ADD=`grep $ACTION $FSP_LOGFILE|grep -v ERROR|cut -d' ' -f10` # We've () around.
							# but who cares. not bc
  if [ $ADD. = . ] ;then ADD=0 ;fi
  echo `echo "$STAT" + "$ADD"|bc` >$FSP_STATDIR/${IP}_${type}loads
done
echo Now for the ratios:
for i in `/bin/ls -1 $FSP_STATDIR/*_uploads`
do
    IP=`basename $i|cut -d_ -f1`
    if [ ! -f "$FSP_STATDIR/${IP}_downloads" ] ;then
        echo 0 >$FSP_STATDIR/${IP}_downloads
    fi
done
for i in `/bin/ls -1 $FSP_STATDIR/*_downloads` 
do
    IP=`basename $i|cut -d_ -f1`
    if [ ! -f $FSP_STATDIR/${IP}_uploads ] ;then
       echo 0 >$FSP_STATDIR/${IP}_uploads
    fi
    HOST=`nslookup $IP|head -4|tail -1|cut -d: -f2` 
    echo Host $HOST \( $IP \)
    UP=`head -1 $FSP_STATDIR/${IP}_uploads`
    DOWN=`head -1 $FSP_STATDIR/${IP}_downloads`
    if [ $DOWN = 0 ] ;then
        RATIOSTR="-n/a- "
    else 
        # we have bignums..
        #RATIO=`echo $UP/$DOWN\*100|bc`
 	UPxx=`echo $UP/10000|bc`
        DOWNmbs=`echo $DOWN/1000000|bc`
        RATIO=`echo $UPxx/$DOWNmbs|bc`
        RATIOSTR="$RATIO percents"
    fi
    echo Uploads: `echo $UP/1000000|bc` Mb. Downloads: `echo $DOWN/1000000|bc` Mb. Up/Dl: $RATIOSTR.
    if [ $DOWN != 0 ] ;then
      if [ $RATIO -lt $MIN_RATIO ] ;then
         if [ $DOWN -gt 30000000 ] ;then # Dl more than 30 Mb, upped less than
				       # 30*$MIN_RATIO/100 Mb.
	   echo WARNING: BIG DOWNLOADER \!
		# If you are running a `restricted' site, you may want to :
		# echo "D:$IP Too much download">>$FSP_CONF, and restart fspd.
	 else
	   echo \(big download, but ok for now\) 
	 fi
       fi
     fi
done
echo Done making stats, flushing Infos from logfile, in case we run from a cron.
grep -v INSTALL $FSP_LOGFILE|grep -v GETFILE >.tmplog$$
cat .tmplog$$ > $FSP_LOGFILE
rm .tmplog$$
echo Stat Script finished.
exit 0
