#!/bin/sh
set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

# bin/$RCSfile$  $Revision$
# returns rate from a mover

opts_wo_args=''
opts_w_args='update_delay|udp_timeout|udp_retries'
USAGE="`basename $0` mover_name  [--{$opts_w_args} <arg>] [--{$opts_wo_args}]"
if [ $# -lt 1 ];then
    echo "$USAGE"
    exit 1
else
    mover=$1
    shift 1
fi
while opt=`expr "${1-}" : '--\(.*\)'`;do
    shift
    eval "case \$opt in
    \\?) echo \"$USAGE\"; exit 0;;
#uncomment next 2 lines if we get some opts_wo_args
#NO#$opts_wo_args)
#NO#    eval opt_\$opt=1;;
    $opts_w_args)
        if [ $# = 0 ];then echo option $opt requires argument; exit 1; fi
        eval opt_\$opt=\"'\$1'\";shift ;;
    *)  echo \"invalid option: \$opt\"; exit 1;;
    esac"
done

nodes=`conf.sh`
host=`echo "$nodes" | grep -i $mover | grep mover| cut -f2 -d:`
port=`echo "$nodes" | grep -i $mover | grep mover| cut -f3 -d:`
mvr=`echo "$nodes" | grep -i $mover | grep mover| cut -f1 -d:| cut -f1 -d.`
case $mvr in
  *DLT*) rate=5.0  ;;
  *MAM*) rate=2.8  ;;
  *AIT*) rate=2.7  ;;
  *stk*) rate=11.0 ;;
 *null*) rate=8.5  ;;
      *) rate=100. ;;
esac

#echo $host
#echo $port

if [ -z "${host-}" -o -z "${port-}" ] ; then
  echo $mover not found in
  echo "$nodes"
  exit 1
fi

python -c '
import sys
import time
import traceback
import udp_client
import errno
u = udp_client.UDPClient()
tfmt="%a %m-%d-%Y-%H:%M:%S"
while 1==1:
  try:
    #print "sending to ('$host','$port')"
    s = u.send( {"work":"status"}, ("'$host'",'$port'), '${opt_udp_timeout:-5}', '${opt_udp_retries:-2}' )
    #print "s=",s
    sts = 0
    if s["state"]=="idle":
      print "%s %s wr=%.2f rr=%.2f w=%.10i r=%.10i l2w=%.10i dt=%.1f %8.8s %i %s %s %i" %\
             (time.strftime(tfmt,time.localtime(time.time())), "         ", 0.,      0.,      0,       0,       0,       0.,\
              "idle", s["no_xfers"],"'$mvr'","'$host'",'$port')
    elif s["mode"]=="w" and s["state"]=="busy":
      dt = s["time_stamp"]-s["zlast_status"]["time_stamp"]
      written = s["wr_bytes"]-s["zlast_status"]["wr_bytes"]
      readten = s["rd_bytes"]-s["zlast_status"]["rd_bytes"]
      left2write = s["rd_bytes"]-s["wr_bytes"]
      wr_rate = written/dt/1024./1024.
      rd_rate = readten/dt/1024./1024.
      blocksize=102400
      if wr_rate*1.05 < '$rate' and wr_rate>0:
        if left2write>blocksize:
          if dt < 4.75:
            condition = "TAPE TLOW"
          else:
            condition = "TAPE SLOW"
        else:
          if dt < 4.75:
            condition = "USER TLOW"
          else:
            condition = "USER SLOW"
      else:
        condition = "         "
      print "%s %s wr=%.2f rr=%.2f w=%.10i r=%.10i l2w=%.10i dt=%.1f %8.8s %i %s %s %i" %\
             (time.strftime(tfmt,time.localtime(time.time())), condition, wr_rate, rd_rate, written, readten, left2write, dt,\
              "writing", s["no_xfers"],"'$mvr'","'$host'",'$port')
    elif s["mode"]=="r" and s["state"]=="busy":
      dt = s["time_stamp"]-s["zlast_status"]["time_stamp"]
      written = s["wr_bytes"]-s["zlast_status"]["wr_bytes"]
      readten = s["rd_bytes"]-s["zlast_status"]["rd_bytes"]
      left2write = s["rd_bytes"]-s["wr_bytes"]
      wr_rate = written/dt/1024./1024.
      rd_rate = readten/dt/1024./1024.
      blocksize=102400
      if rd_rate*1.05 < '$rate' and rd_rate>0:
        if left2write>blocksize:
          if dt < 4.75:
            condition = "USER TLOW"
          else:
            condition = "USER SLOW"
        else:
          if dt < 4.75:
            condition = "TAPE TLOW"
          else:
            condition = "TAPE SLOW"
      else:
        condition = "         "
      print "%s %s rr=%.2f wr=%.2f r=%.10i w=%.10i l2w=%.10i dt=%.1f %8.8s %i %s %s %i" %\
             (time.strftime(tfmt,time.localtime(time.time())), condition, rd_rate, wr_rate, readten, written, left2write, dt,\
              "reading", s["no_xfers"],"'$mvr'","'$host'",'$port')
    else:
      print "unexpected, mode, state=",s["mode"],s["state"]
  except errno.errorcode[errno.ETIMEDOUT]:
    print "%s %s wr=%.2f rr=%.2f w=%.10i r=%.10i l2w=%.10i dt=%.1f %8.8s %i %s %s %i" %\
           (time.strftime(tfmt,time.localtime(time.time())), "         ", 0.,      0.,      0,       0,       0,       0.,\
            "timeout", 0,"'$mvr'","'$host'",'$port')
  except:
    traceback.print_exc()
    sts=1
    break
  sys.stdout.flush()
  time.sleep('${opt_update_delay:-5}')

del u
sys.exit( sts )
'
