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

command=${1:-plot}
node=`hostname -s`
gang=`gang`
case $gang in
   d0en) silo_node=fntt
	 ;;
  stken) silo_node=fntt
	 ;;
  cdfen) silo_node=fntt2
	 ;;
      *) echo \"$0 $command\" not supported on $gang
	 exit 1
	 ;;
esac

pcnt=/home/enstore/STK_ACSLS_PROC_COUNT
busy=/home/enstore/STK_DRIVE_UTILIZATION

getnode=${gang}srv4
plotnode=${gang}srv2

case $command in
  get)
    if [ $node = $getnode ] ; then
      dinfo=/tmp/stk.dinfo
      rm -f $dinfo
      /usr/bin/rsh $silo_node -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 $getnode
      echo because that is the one that talks to $silo_node
      exit 1
    fi
    ;;
  plot)
    if [ $node = $plotnode ] ; then
      enrcp $getnode:$pcnt $pcnt
      enrcp $getnode:$busy $busy
      pltcmd=/tmp/stk.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 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 STK 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 \"STK $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
	     }

# function to generate utilization plots for a specific STK drive type
# using points instead of lines
drive_plot_dl() {
	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 \"STK $drv DL 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\" " >$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

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

      if [ $gang = d0en ] ; then

	n9940a=`grep "T9940A" $dinfo | grep "   1, 1,"    | grep -c "in use"`
	a9940a=`grep "T9940A" $dinfo | grep "   1, 1,"    | grep -c "online"`
	n9940b=`grep "T9940B" $dinfo | grep "   1, [01]," | grep -c "in use"`
	a9940b=`grep "T9940B" $dinfo | grep "   1, [01]," | grep -c "online"`

	echo "$now $n9940a $a9940a $n9940b $a9940b" >>$busy

      elif [ $gang = stken ] ; then

	n9840=`grep " 9840" $dinfo | grep -c "in use"`
	a9840=`grep " 9840" $dinfo | grep -c "online"`
	n9940a=`grep "T9940A" $dinfo | grep "   0, 0,"    | grep -c "in use"`
	a9940a=`grep "T9940A" $dinfo | grep "   0, 0,"    | grep -c "online"`
	n9940b=`grep "T9940B" $dinfo | grep "   0, [01]," | grep -c "in use"`
	a9940b=`grep "T9940B" $dinfo | grep "   0, [01]," | grep -c "online"`

	echo "$now $n9840 $a9840 $n9940a $a9940a $n9940b $a9940b" >>$busy

      elif [ $gang = cdfen ] ; then

	n9940a=`grep "T9940A" $dinfo | grep "   0, [01]," | grep -c "in use"`
	a9940a=`grep "T9940A" $dinfo | grep "   0, [01]," | grep -c "online"`
	n9940b=`grep "T9940B" $dinfo | grep "   0, [01]," | grep -c "in use"`
	a9940b=`grep "T9940B" $dinfo | grep "   0, [01]," | grep -c "online"`

	echo "$now 0 0 $n9940a $a9940a $n9940b $a9940b" >>$busy

      fi
      ;;

    plot)
      acsls_plot /fnal/ups/prd/www_pages/enstore/ACSLS_Process_Count

      if [ $gang = d0en ] ; then

	drive_plot /fnal/ups/prd/www_pages/enstore/T9940A_Utilization T9940A 2
	drive_plot /fnal/ups/prd/www_pages/enstore/T9940B_Utilization T9940B '(valid(4)?$4:1/0)'

	# the same, but with points instead of lines
	drive_plot_dl /fnal/ups/prd/www_pages/enstore/T9940A_Utilization_dl T9940A 2
	drive_plot_dl /fnal/ups/prd/www_pages/enstore/T9940B_Utilization_dl T9940B '(valid(4)?$4:1/0)'

      elif [ $gang = stken ] ; then

	drive_plot /fnal/ups/prd/www_pages/enstore/9840_Utilization 9840 2
	drive_plot /fnal/ups/prd/www_pages/enstore/T9940A_Utilization T9940A 4
	drive_plot /fnal/ups/prd/www_pages/enstore/T9940B_Utilization T9940B '(valid(6)?$6:1/0)'

      elif [ $gang = cdfen ] ; then

	drive_plot /fnal/ups/prd/www_pages/enstore/T9940A_Utilization T9940A 4
	drive_plot /fnal/ups/prd/www_pages/enstore/T9940B_Utilization T9940B '(valid(6)?$6:1/0)'

	# the same, but with points instead of lines
	drive_plot_dl /fnal/ups/prd/www_pages/enstore/T9940A_Utilization_dl T9940A 4
	drive_plot_dl /fnal/ups/prd/www_pages/enstore/T9940B_Utilization_dl T9940B '(valid(6)?$6:1/0)'

      fi
      ;;

esac
