#!/bin/sh -u

# script to collect and plot drive utilization for the GCC tape robots.

# run as cron jobs on stkensrv2 and stkensrv4

if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

command=${1:-plot}
node=`hostname -s`

pcnt=/home/enstore/GCC_ACSLS_PROC_COUNT
busy=/home/enstore/GCC_DRIVE_UTILIZATION

gang=`gang`
plotNode=${gang}srv2
dataNode=${gang}srv4
acsNode=fntt-gcc

case $command in
  data)
    if [ $node = $dataNode ] ; then
      dinfo=/tmp/gcc.dinfo
      rm -f $dinfo
      /usr/bin/rsh $acsNode -l acsss "echo query drive all '\r' logoff | bin/cmd_proc -l -q 2>/dev/null" >$dinfo
    else
      echo \"$0 $command\" can only be run on $dataNode
      echo because that is the one that talks to $acsNode
      exit 1
    fi
    ;;
  plot)
    if [ $node = $plotNode ] ; then
      enrcp $dataNode:$pcnt $pcnt
      enrcp $dataNode:$busy $busy
      pltcmd=/tmp/gcc.du.gnu
    else
      echo \"$0 $command\" can only be run on $plotNode
      echo because that is the one that has a web server
      exit 1
    fi
    ;;
  *)
    exit 1
    ;;
esac

now=`date +"%Y-%m-%d:%H:%M:%S"`
Quarter_ago=`date --date "91 days ago" +"%Y-%m-%d:%H:%M:%S"`

# function to list LTO3 VO volumes in use
listLto3VO() {
	sed -e 's/, /,/g' |
	awk '$3 == "in" && $4 == "use" && $6 == "IBM-LTO-3" && $5 ~ /^VO/ {printf " %s", $5}' |
	cut -c2- |
	sed -e "s/ /','/g" -e "s/^/('/" -e "s/$/')/"
	      }

# function to count LTO3 volumes in use by prefix
countLto3() {
	sed -e 's/, /,/g' |
	awk '$3 == "in" && $4 == "use" && $6 == "IBM-LTO-3" {
		if ($5 ~ /^IA/) i="IA"; else
		if ($5 ~ /^VO/) i="VO"; else
		i="Other"
		Count[i]++}
	      END {for (i in Count) printf "lto3_%s=%d;", i, Count[i]}'
	     }

# function to count volumes by storage group
countbySG() {
	enstore info --query "select storage_group, count(storage_group) from volume where label in $1 group by storage_group;" |
	awk 'NR > 2 {printf "SG_%s=%d;", gensub("[^[:alnum:]]","","g",$1), $2}'
	      }

# function to generate process count plots for ACSLS oommand processes
acsls_plot() {
	plt=$1		# plot location (no extension)

	rm -f $pltcmd
	echo "set output \"$plt.ps\"
set terminal postscript color solid
set title \"ACSLS Command Process Count $now\"
set xlabel \"Date\"
set timefmt \"%Y-%m-%d:%H:%M:%S\"
set xdata time
set xrange [\"$Quarter_ago\" : ]
set ylabel \"Number in use\"
set grid
set yrange [0: ]
set format x \"%m-%d\"
set nokey
plot \"$pcnt\" using 1:2 t \"process count\" w impulses" >$pltcmd
	gnuplot $pltcmd
	convert -flatten -background lightgray -rotate 90 $plt.ps $plt.jpg
	convert -flatten -background lightgray -geometry 120x120 -modulate 80 $plt.jpg ${plt}_stamp.jpg
	     }

# function to generate utilization plots for a specific GCC drive type
drive_plot() {
	plt=$1		# plot location (no extension)
	drv=$2		# label string
	ord=$3		# y-axis index

	rm -f $pltcmd
	echo "set output \"$plt.ps\"
set terminal postscript color solid
set title \"GCC $drv Drive Utilization $now\"
set xlabel \"Date\"
set timefmt \"%Y-%m-%d:%H:%M:%S\"
set xdata time
set xrange [\"$Quarter_ago\" : ]
set ylabel \"Number in Use\"
set grid
set yrange [0: ]
set format x \"%m-%d\"
set nokey
plot \"$busy\" using 1:$ord t \"$drv in use\" w impulses" >$pltcmd
	gnuplot $pltcmd
	convert -flatten -background lightgray -rotate 90 $plt.ps $plt.jpg
	convert -flatten -background lightgray -geometry 120x120 -modulate 80 $plt.jpg ${plt}_stamp.jpg
	     }

case $command in

    data)
      nproc=`/usr/bin/rsh $acsNode -l acsss "pgrep cmd_proc" | wc -l`
      echo "$now $nproc" >>$pcnt

      eval `countLto3 <$dinfo`		# defines lto3_IA, lto3_VO, ...
      if [ -n "${lto3_VO:-}" ]; then
	VO_list=`listLto3VO <$dinfo`
	eval `countbySG "$VO_list"`	# defines SG_cms, SG_expdb, ...
      fi
      echo "$now ${SG_cms:-0} ${lto3_IA:-0} $(( ${lto3_VO:-0} - ${SG_cms:-0} ))" >>$busy
      ;;

    plot)
      acsls_plot /fnal/ups/prd/www_pages/enstore/GCC_ACSLS_Process_Count
      drive_plot /fnal/ups/prd/www_pages/enstore/GCC_LTO3_CMS_Utilization LTO3/CMS 2
      drive_plot /fnal/ups/prd/www_pages/enstore/GCC_LTO3_CDF_Utilization LTO3/CDF 3
      drive_plot /fnal/ups/prd/www_pages/enstore/GCC_LTO3_Other_Utilization LTO3/Other 4
      ;;

esac
