#!/bin/sh

# $Id: silo-check,v 1.45 2008/09/30 21:05:48 stan Exp $

# Generate the VOLUME_CROSS_CHECK inventory web page.
# This script is very Fermilab specific.

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

node=`hostname -s`
gang=`gang`

# Make sure to set a timeout.
timeout='--timeout 10 --retries 3'

# First obtain the directory to write the output.
inventory_dir=`enstore conf $timeout --show crons html_dir`/tape_inventory
if [ ! -d $inventory_dir ]; then
    echo Inventory directory $inventory_dir not found.
    exit 1
fi

# Create the variables that point to the files to output to.
fname=VOLUME_CROSS_CHECK
output_file=$inventory_dir/$fname
temp_file=$output_file.temp	# Write to this then swap it into place.

# If we write to a temp file, and swap it in when we are done, there will
# not be any time when the page is empty because the script is still writing
# the file.
rm -f $temp_file
if [ -L $output_file ]; then
   # Should VOLUME_CROSS_CHECK be a symbolic link to the ecron .output file
   # we should handle removing it before we write an actual file there.
   rm -f $output_file
fi

# obtain list of valid libraries
enstore conf $timeout --show | sed -n "s/.*'\([a-zA-Z0-9][-a-zA-Z0-9_]*\)\.library_manager'.*/\1/ p" | sort -u >/tmp/valid-library

# This is for easier redirection of all the stdout output.
(

# Make sure we know how up-to-date this is.
echo -e Volume Cross Check Report: `date`
echo -e Brought to You by: `basename $0` "\n"

if [ $gang = "stken" ]; then

# stken 9310 silos are acs 0 & lsm 0,1
# stken 8500 rails are acs 0 & lsm 0-7

# for "query drive" (ACSLS 6), each line in the silo inventory is in one of four formats:
#   vol   home		acs, lsm, panel, row, col	type
#   vol   in drive	acs, lsm, panel, drive		type
#   vol   in transit	acs, lsm, panel, row, col	type
#   vol   in transit	acs, lsm, panel, drive		type
# where type is one of STK2P, STK2W
#
# for "display" (ACSLS 7), each line in the silo inventory has ten fields:
#   Vol_id  Acs  Lsm  Panel  Row  Column  Pool   Status    Media     Type
# where
#   Status is one of dismount, eject, enter, mount, home, in_drive, move, missing, absent, ejected
#   Media is one of STK2P, STK2W
#   Type is one of data, clean
#
# each line in the tower inventory is in this format:
#   volser xxnnnn media DECDLT attrib {Empty|Ejected|Mounted|Occupied}
#
# all lines of enstore inventory have ten fields, in this format:
#   vol   avail   system_inhibit   user_inhibit   library   wp_status	mounts   volume_family
# where each inhibit takes two fields (including parentheses)
#
# there should be no deleted volumes in VOLUMES_DEFINED

egrep -v "^$|^<|DG [0-9][0-9][0-9]" $inventory_dir/STK-VOLUMES.html |\
  sed -e 's/, /,/g' -e 's/in /in_/' |\
  awk '(NF==10 && $1~/^[A-Z][A-Z][0-9A-Z][0-9A-Z][0-9][0-9]$/) || (NF==4 && $4~/^0,[-0-9,]+$/) {print $1}' |\
  sort >/tmp/silo-volumes
egrep -v "^$|^<|(TEST|(TST|P[RS][A-Z]|IA[0-9A-Z])[0-9A-Z])[0-9][0-9]" $inventory_dir/SL8500[FG]1-VOLUMES.html |\
  sed -e 's/, /,/g' -e 's/in /in_/' |\
  awk '(NF==10 && $1~/^[A-Z][A-Z][0-9A-Z][0-9A-Z][0-9][0-9]$/ && $1!~/^SL8500$/) || (NF==4 && $4~/^0,[-0-9,]+$/) {print $1}' |\
  sort >/tmp/rail-volumes
egrep -v "^$|^<|volser 0[0-9][0-9][0-9][0-9][0-9] media (8MM|DECDLT) attrib (Empty|Occupied)" $inventory_dir/AML2-VOLUMES.html |\
  awk '{print $2}' |\
  sort >/tmp/tower-volumes
awk 'NR>3 && NF==10 && $7!~/null|disk|shelf/ {print $1}' $inventory_dir/VOLUMES_DEFINED |\
  sort >/tmp/vols-defined

# tapes in the PowderHorn 9310, but not in enstore
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in the PowderHorn 9310 (acs 0, lsm 0 and 1) that are not fully declared to enstore"
comm -23 /tmp/silo-volumes /tmp/vols-defined >/tmp/silo-not-enstore
fgrep -f /tmp/silo-not-enstore $inventory_dir/STK-VOLUMES.html

# tapes in the StreamLine 8500, but not in enstore
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in the StreamLine 8500 (acs 0, lsm 0 to 7) that are not fully declared to enstore"
comm -23 /tmp/rail-volumes /tmp/vols-defined >/tmp/rail-not-enstore
fgrep -f /tmp/rail-not-enstore $inventory_dir/SL8500[FG]1-VOLUMES.html

# tapes in the SDSS AML/2 tower, but not in enstore
# stken has only DLTs in the AML/2
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in the SDSS AML/2 tower that are not fully declared to enstore"
comm -23 /tmp/tower-volumes /tmp/vols-defined >/tmp/tower-not-enstore
fgrep -f /tmp/tower-not-enstore $inventory_dir/AML2-VOLUMES.html

#echo
#echo "--------------------------------------------------------------------------------------------------------------------"
#echo "Tapes not in any category"
#egrep -v "^$|^<|DG [0-9][0-9][0-9]" $inventory_dir/STK-VOLUMES.html | grep -v "        0, [01],"

# tapes in enstore, but not in the robots
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes declared to enstore that are not in the SL8500, the Public or CMS silo, or the SDSS tower"
sort -mu /tmp/{silo,rail,tower}-volumes >/tmp/robot-volumes
comm -23 /tmp/vols-defined /tmp/robot-volumes >/tmp/enstore-not-robot
fgrep -f /tmp/enstore-not-robot $inventory_dir/VOLUMES_DEFINED

# tapes apparently in more than one robot, whether in enstore or not, ie, duplicate names
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes that are apparently in more than one robot"
sort -m /tmp/{silo,rail,tower}-volumes | uniq -d | while read vol; do
  grep -h $vol $inventory_dir/{VOLUMES_DEFINED,{SL8500[FG]1,STK,AML2}-VOLUMES.html}
done

echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in NOACCESS or NOTALLOWED state"
awk 'NR>3 && NF==10 && $3!~/none/ && $4!~/migrated/' $inventory_dir/VOLUMES_DEFINED

echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in invalid library"
libs=`tr '\n' ' ' </tmp/valid-library`
echo "Valid libraries are: $libs"
awk -v Libs="$libs" 'BEGIN {$0=Libs; for (i=1; i<=NF; i++) libs[$i]=i}
		     NR>3 && NF==10 && $NF!~/^test\.[[:print:]]*\.[[:print:]]*/ &&
		     $7!~/null|disk|shelf/ && !($7 in libs)' $inventory_dir/VOLUMES_DEFINED
echo
echo "--------------------------------------------------------------------------------------------------------------------"


elif [ $gang = "d0en" ]; then

# d0en 9310 silos are acs 1 & lsm 0,1
# d0en 8500 rails are acs 0 & lsm 4-7, acs 1 & lsm 0-3

# for "query drive" (ACSLS 6), each line in the silo inventory is in one of four formats:
#   vol   home		acs, lsm, panel, row, col	type
#   vol   in drive	acs, lsm, panel, drive		type
#   vol   in transit	acs, lsm, panel, row, col	type
#   vol   in transit	acs, lsm, panel, drive		type
# where type is one of STK2P, STK2W
#
# for "display" (ACSLS 7), each line in the silo inventory has ten fields:
#   Vol_id  Acs  Lsm  Panel  Row  Column  Pool   Status    Media     Type
# where
#   Status is one of dismount, eject, enter, mount, home, in drive,
#		     move, missing, absent, ejected, in transit
#   Media is one of STK2P, STK2W
#   Type is one of data, clean
#
# each line in the tower inventory is in this format:
#   volser xxxnnn[L1] media 3480 attrib {Empty|Ejected|Mounted|Occupied}
#
# inventory listed by the stk media changer has 4 columns,
# after squeezing out the blanks in the comma-separated location:
#   volume type state location
# where
#   type is one of STK2P, STK2W
#   state is one of dismount, eject, enter, mount, home, in drive,
#		    move, missing, absent, ejected, in transit
#
# inventory listed by the sl8500 media changer has 4 columns,
# after squeezing out the blanks in the comma-separated location:
#   volume type state location
# where
#   type is one of LTO-CLNU, LTO-400G, LTO-800G 
#   state is one of dismount, eject, enter, mount, home, in drive,
#		    move, missing, absent, ejected, in transit
#
# inventory listed by the aml2 media changer has 4 columns:
#   volume type state location
# where
#   type is one of 3480, DECDLT
#   state is one of O,M,E
#   location is empty
#
# all lines of enstore inventory have ten fields, in this format:
#   vol   avail   system_inhibit   user_inhibit   library   wp_status	mounts   volume_family
# where each inhibit takes two fields (including parentheses)
#
# there should be no deleted volumes in VOLUMES_DEFINED

egrep -v "^$|^<|(DG |IA[0-9A-Z])[0-9][0-9][0-9]" $inventory_dir/STK-VOLUMES.html |\
  sed -e 's/, /,/g' -e 's/in /in_/' |\
  awk '(NF==10 && $1~/^[A-Z][A-Z][0-9A-Z][0-9A-Z][0-9][0-9]$/) || (NF==4 && $4~/^1,[-0-9,]+$/) {print $1}' |\
  sort >/tmp/silo-volumes
egrep -hv "^$|^<|(TEST|(TST|(VO|IA)[0-9A-Z])[0-9A-Z])[0-9][0-9]" $inventory_dir/SL8500[FG]1-VOLUMES.html |\
  sed -e 's/, /,/g' -e 's/in /in_/' |\
  awk '(NF==10 && $1~/^[A-Z][A-Z][0-9A-Z][0-9A-Z][0-9][0-9]$/ && $1!~/^SL8500$/) || (NF==4 && $4~/^[01],[-0-9,]+$/) {print $1}' |\
  sort >/tmp/rail-volumes
egrep -v "^$|^<" $inventory_dir/AML2-VOLUMES.html |\
  awk 'NF==3 && $2=="3480" && $3~/[OME]/ {print $1} \
       NF==6 && $1=="volser" {print $2}' |\
  sort >/tmp/tower-volumes
awk 'NR>3 && NF==10 && $7!~/null|disk|shelf/ {print $1}' $inventory_dir/VOLUMES_DEFINED |\
  sort >/tmp/vols-defined

# tapes in the PowderHorn 9310, but not in enstore
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in the PowderHorn 9310 (acs 1, lsm 0 and 1) that are not fully declared to enstore"
comm -23 /tmp/silo-volumes /tmp/vols-defined >/tmp/silo-not-enstore
fgrep -f /tmp/silo-not-enstore $inventory_dir/STK-VOLUMES.html

# tapes in a StreamLine 8500, but not in enstore
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in a StreamLine 8500 (acs 0, lsm 4 to 7 or acs 1, lsm 0 to 3) that are not fully declared to enstore"
comm -23 /tmp/rail-volumes /tmp/vols-defined >/tmp/rail-not-enstore
fgrep -hf /tmp/rail-not-enstore $inventory_dir/SL8500[FG]1-VOLUMES.html

# tapes in a D0 AML/2 tower, but not in enstore
# d0en has only LTOs in the AML/2
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in a D0 AML/2 tower that are not fully declared to enstore"
comm -23 /tmp/tower-volumes /tmp/vols-defined >/tmp/tower-not-enstore
fgrep -f /tmp/tower-not-enstore $inventory_dir/AML2-VOLUMES.html

#echo
#echo "--------------------------------------------------------------------------------------------------------------------"
#echo "Tapes not in any category"
#egrep -v "^$|^<|DG [0-9][0-9][0-9]" $inventory_dir/STK-VOLUMES.html | grep -v "        1, [01],"

# tapes in enstore, but not in the robots
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes declared to enstore that are not in a SL8500, a D0 silo, or a D0 tower"
sort -mu /tmp/{silo,rail,tower}-volumes >/tmp/robot-volumes
comm -23 /tmp/vols-defined /tmp/robot-volumes >/tmp/enstore-not-robot
fgrep -f /tmp/enstore-not-robot $inventory_dir/VOLUMES_DEFINED

# tapes apparently in more than one robot, whether in enstore or not, ie, duplicate names
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes that are apparently in more than one robot"
sort -m /tmp/{silo,rail,tower}-volumes | uniq -d | while read vol; do
  grep -h $vol $inventory_dir/{VOLUMES_DEFINED,{SL8500[FG]1,STK,AML2}-VOLUMES.html}
done

echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in NOACCESS or NOTALLOWED state"
awk 'NR>3 && NF==10 && $3!~/none/ && $4!~/migrated/' $inventory_dir/VOLUMES_DEFINED

echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in invalid library"
libs=`tr '\n' ' ' </tmp/valid-library`
echo "Valid libraries are: $libs"
awk -v Libs="$libs" 'BEGIN {$0=Libs; for (i=1; i<=NF; i++) libs[$i]=i}
		     NR>3 && NF==10 && $NF!~/^test\.[[:print:]]*\.[[:print:]]*/ &&
		     $7!~/null|disk|shelf/ && !($7 in libs)' $inventory_dir/VOLUMES_DEFINED
echo
echo "--------------------------------------------------------------------------------------------------------------------"


elif [ $gang = "cdfen" ]; then

# cdfen 9310 silos are acs 0 & lsm 0,1
# cdfen 8500 rails are acs 0 & lsm 0-7

# for "query drive" (ACSLS 6), each line in the silo inventory is in one of four formats:
#   vol   home		acs, lsm, panel, row, col	type
#   vol   in drive	acs, lsm, panel, drive		type
#   vol   in transit	acs, lsm, panel, row, col	type
#   vol   in transit	acs, lsm, panel, drive		type
# where type is one of STK2P, STK2W
#
# for "display" (ACSLS 7), each line in the silo inventory has ten fields:
#   Vol_id  Acs  Lsm  Panel  Row  Column  Pool   Status    Media     Type
# where
#   Status is one of dismount, eject, enter, mount, home, in_drive, move, missing, absent, ejected
#   Media is one of STK2P, STK2W
#   Type is one of data, clean
#
# all lines of enstore inventory have ten fields, in this format:
#   vol   avail   system_inhibit   user_inhibit   library   wp_status	mounts   volume_family
# where each inhibit takes two fields (including parentheses)
#
# there should be no deleted volumes in VOLUMES_DEFINED

egrep -v "^$|^<|DG [0-9][0-9][0-9]" $inventory_dir/STK-VOLUMES.html |\
  sed -e 's/, /,/g' -e 's/in /in_/' |\
  awk '(NF==10 && $1~/^[A-Z][A-Z][0-9A-Z][0-9A-Z][0-9][0-9]$/) || (NF==4 && $4~/^0,[-0-9,]+$/) {print $1}' |\
  sort >/tmp/silo-volumes
egrep -v "^$|^<|(TEST|(TST|P[RS][A-Z]|VO[0-9A-Z])[0-9A-Z])[0-9][0-9]" $inventory_dir/SL8500-VOLUMES.html |\
  sed -e 's/, /,/g' -e 's/in /in_/' |\
  awk '(NF==10 && $1~/^[A-Z][A-Z][0-9A-Z][0-9A-Z][0-9][0-9]$/ && $1!~/^SL8500$/) || (NF==4 && $4~/^0,[-0-9,]+$/) {print $1}' |\
  sort >/tmp/rail-volumes
awk 'NR>3 && NF==10 && $7!~/null|disk|shelf|-D0/ {print $1}' $inventory_dir/VOLUMES_DEFINED |\
  sort >/tmp/vols-defined

# tapes in the PowderHorn 9310, but not in enstore
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in the PowderHorn 9310 (acs 0, lsm 0 and 1) that are not fully declared to enstore"
comm -23 /tmp/silo-volumes /tmp/vols-defined >/tmp/silo-not-enstore
fgrep -f /tmp/silo-not-enstore $inventory_dir/STK-VOLUMES.html

# tapes in the StreamLine 8500, but not in enstore
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in the StreamLine 8500 (acs 0, lsm 0 to 7) that are not fully declared to enstore"
comm -23 /tmp/rail-volumes /tmp/vols-defined >/tmp/rail-not-enstore
fgrep -f /tmp/rail-not-enstore $inventory_dir/SL8500-VOLUMES.html

#echo
#echo "--------------------------------------------------------------------------------------------------------------------"
#echo "Tapes not in any category"
#egrep -v "^$|^<|DG [0-9][0-9][0-9]" $inventory_dir/STK-VOLUMES.html | grep -v "        0, [01],"

# tapes in enstore, but not in the robots
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes declared to enstore that are not in the SL8500 or a CDF silo"
sort -mu /tmp/{silo,rail}-volumes >/tmp/robot-volumes
comm -23 /tmp/vols-defined /tmp/robot-volumes >/tmp/enstore-not-robot
fgrep -f /tmp/enstore-not-robot $inventory_dir/VOLUMES_DEFINED

# tapes apparently in more than one robot, whether in enstore or not, ie, duplicate names
echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes that are apparently in more than one robot"
sort -m /tmp/{silo,rail}-volumes | uniq -d | while read vol; do
  grep -h $vol $inventory_dir/{VOLUMES_DEFINED,{SL8500,STK}-VOLUMES.html}
done

echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in NOACCESS or NOTALLOWED state"
awk 'NR>3 && NF==10 && $3!~/none/ && $4!~/migrated/' $inventory_dir/VOLUMES_DEFINED

echo
echo "--------------------------------------------------------------------------------------------------------------------"
echo "Tapes in invalid library"
libs=`tr '\n' ' ' </tmp/valid-library`
echo "Valid libraries are: $libs"
awk -v Libs="$libs" 'BEGIN {$0=Libs; for (i=1; i<=NF; i++) libs[$i]=i}
		     NR>3 && NF==10 && $NF!~/^test\.[[:print:]]*\.[[:print:]]*/ &&
		     $7!~/null|disk|shelf/ && !($7 in libs)' $inventory_dir/VOLUMES_DEFINED
echo
echo "--------------------------------------------------------------------------------------------------------------------"

fi

date

) | tee $temp_file 2>&1

mv $temp_file $output_file	# Do the temp file swap.
