#!/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)
	 ;;
  stken)
	 ;;
      *) echo \"$0 $command\" not supported on $gang
	 exit 1
	 ;;
esac

busy=/home/enstore/ADIC_DRIVE_UTILIZATION

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

case $command in
  get)
    if [ $node = $getnode ] ; then
      dinfo=/tmp/adic.dinfo
      rm -f $dinfo
      dasadmin listd2 ${getnode}p >$dinfo
    else
      echo \"$0 $command\" can only be run on $getnode
      echo because that is the one where dasadmin is setup
      exit 1
    fi
    ;;
  plot)
    if [ $node = $plotnode ] ; then
      enrcp $getnode:$busy $busy
      pltcmd=/tmp/adic.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 utilization plots for a specific ADIC 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 \"ADIC $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 ADIC 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 \"ADIC $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)
      if [ $gang = d0en ] ; then

#	nMAM1=`grep "type: M" $dinfo | grep -vc "volser:   c"`
#	aMAM1=`grep "type: M" $dinfo | grep -c  " UP "`
#	nMAM2=`grep "type: I" $dinfo | grep -vc "volser:   c"`
#	aMAM2=`grep "type: I" $dinfo | grep -c  " UP "`
	nLTO1=`grep "type: 3" $dinfo | grep 'drive: D[35]' | grep -vc "volser:   c"`
	aLTO1=`grep "type: 3" $dinfo | grep 'drive: D[35]' | grep -c  " UP "`
	nLTO2=`grep "type: 3" $dinfo | grep 'drive: D[2467]' | grep -vc "volser:   c"`
	aLTO2=`grep "type: 3" $dinfo | grep 'drive: D[2467]' | grep -c  " UP "`

	echo "$now $nLTO1 $aLTO1 $nLTO2 $aLTO2" >>$busy

      elif [ $gang = stken ] ; then

	nDLT4=`grep "type: E" $dinfo | grep -vc "volser:   c"`
	aDLT4=`grep "type: E" $dinfo | grep -c  " UP "`
	nDLT8=`grep "type: G" $dinfo | grep -vc "volser:   c"`
	aDLT8=`grep "type: G" $dinfo | grep -c  " UP "`

	echo "$now $nDLT4 $aDLT4 $nDLT8 $aDLT8" >>$busy

      fi
      ;;

    plot)
      if [ $gang = d0en ] ; then

	drive_plot /fnal/ups/prd/www_pages/enstore/LTO1_Utilization LTO1 2
	drive_plot /fnal/ups/prd/www_pages/enstore/LTO2_Utilization LTO2 4

	# the same, but with points instead of lines
	drive_plot_dl /fnal/ups/prd/www_pages/enstore/LTO1_Utilization_dl LTO1 2
	drive_plot_dl /fnal/ups/prd/www_pages/enstore/LTO2_Utilization_dl LTO2 4

      elif [ $gang = stken ] ; then

	drive_plot /fnal/ups/prd/www_pages/enstore/DLT4_Utilization DLT4000 2
	drive_plot /fnal/ups/prd/www_pages/enstore/DLT8_Utilization DLT8000 '(valid(4)?$4:1/0)'

      fi
      ;;

esac
