
#!/bin/ksh
# File	: get_tape_info.ksh
# By	: M. de Boer, 051017
# Subject	: Count the number of empty slots & empty tapes in lib.
# Mod(0.2), MdB; chk FREEPOOL, $MAILFILE added
# Mod(0.3), MdB(060102); omnirpt  FREEPOOL & LIBRARY, MAILTO added
# Mod(0.4), MdB(090112); mc <> OK for last slots (from 1850), BARCODELIST
# Mod(0.5), MdB(090126); Mod some text
# Mod(0.6) , MdB(100831): Some text Mod's & added .ksh to scripts name

PGM="`basename $0|cut -d\. -f1`"
TMP="/tmp/$PGM.$$"
VERSION="0.6"
FREEPOOL="Free LTO-Ultrium"
MAILFILE="$TMP.mail"
LIBRARY=""
MAILTO="maarten.deboer@atosorigin.com"
HOSTNAME="`hostname`"

case `hostname` in
  srv87ddc) LIBRARY=/dev/rscsi/picker ;;
  nlxsl069) LIBRARY=/dev/rac/esllib ;;
esac

date +%Y-%m-%d" "%H:%M:%S| tee -a $MAILFILE

/opt/omni/bin/omnirpt -report pool_list -tab | grep -v \#|awk -F\t '{print $1}'|grep Free | while read FREEPOOL
do
  # Get info about FREEPOOL
  echo "- From '/opt/omni/bin/omnirpt -report pool_list '"|tee -a $MAILFILE
  echo $FREEPOOL
  NROFTAPES="`/opt/omni/bin/omnimm -list_pool \"$FREEPOOL\"|grep -vi offsite|grep Good|grep -c None ` "
  echo "$NROFTAPES Good tapes in $FREEPOOL" | tee -a $MAILFILE
  NROFTAPES="`/opt/omni/bin/omnimm -list_pool \"$FREEPOOL\"|grep -vi offsite|grep Fair|grep -c None ` "
  echo "$NROFTAPES Fair tapes in $FREEPOOL" | tee -a $MAILFILE
  NROFTAPES="`/opt/omni/bin/omnimm -list_pool \"$FREEPOOL\"|grep -vi offsite|grep Poor|grep -c None ` "
  echo "$NROFTAPES Poor tapes in $FREEPOOL" | tee -a $MAILFILE
done

# If omnimm -list_pool not works;
/opt/omni/bin/omnimm -list_pool  "Free LTO-Ultrium" | awk -F: '{print $2}' | grep '\]' | wc -l


# Get info out of HP-DP and put into a barcodelist in /tmp
echo "Getting tape-lib. info using repository_barcode_scan. Can take a while ..."
/opt/omni/bin/omnidownload -list_libraries|grep `hostname`|awk '{print $1}'|while read LIBNAME
do
  BARCODELIST="/tmp/$LIBNAME.barcodes"
  /opt/omni/bin/omnimm -repository_barcode_scan $LIBNAME > $BARCODELIST
  echo "- From '/opt/omni/bin/omnimm -repository_barcode_scan'"|tee -a $MAILFILE
  NROFUNKNOWN="`grep Unknown $BARCODELIST |wc -l`"
  TOTNR="`grep '\[' $BARCODELIST |wc -l`"
  echo "$NROFUNKNOWN (/$TOTNR) Unknown (of total) tapes in lib. ($LIBNAME) " | tee -a $MAILFILE
done

# Get Info from Tape-lib.
#echo "Getting info from tape-lib. ($LIBRARY) using mc. Can tape a while ..."
#/appl/tsu/bin/sudo /usr/sbin/mc -p $LIBRARY -rS > $TMP
#NROFEMPTY="`grep EMPTY $TMP | wc -l`"
#TOTNR="`cat $TMP | wc -l`"
#echo "$NROFEMPTY (/$TOTNR) empty slots in lib. ($LIBRARY)" | tee -a $MAILFILE
# New counting methode, because mc is not "betrouwbaar". Therefore we going to use output of barcode-scan
echo "" | tee -a $MAILFILE
echo "mc command is not accurate (anymore). Use nr of total-slots (/ x) from above" | tee -a $MAILFILE


echo "Mailing to info to $MAILTO ..."
mailx -s ":$HOSTNAME: $PGM [$PGM v$VERSION]" $MAILTO < $MAILFILE

rm $TMP $MAILFILE

