#!/bin/sh

SUDO=/usr/local/bin/sudo

MTX=/usr/local/sbin/mtx-changer
CHANGER=pass11
DRIVE=sa0


SLOT=`${SUDO} ${MTX} /dev/${CHANGER} loaded 0 /dev/${DRIVE} 0`

LINE=`${SUDO} ${MTX} /dev/${CHANGER} list | grep ^${SLOT}:`

VOLUME=`echo $LINE | awk 'BEGIN { FS = ":" } ; {print $2}'`


# do this so we are in the right dir
cd `dirname $0`

. dlt-functions

CONSOLE=/usr/local/sbin/bconsole

# Grab the stats for the current DLT tape

#echo ${DRIVE}

ERRORS_PER_GB=`${SUDO} /home/dan/bin/dlt-write ${DRIVE} | grep 'Total corrected errors / GB' | awk '{print $7}'`

echo `${SUDO} /home/dan/bin/dlt-write ${DRIVE} | grep 'Total corrected errors / GB' `

#echo "Errors per GB is '${ERRORS_PER_GB}'"

# What tape is now in the tape drive?


# Get the status of the storage device
STATUS=`echo 'status storage=DLT' | ${CONSOLE} | egrep 'Device "DLT" \(/dev/nsa0\) is mounted with Volume=".*" Pool=".*"'`
echo 'STATUS is ' ${STATUS}
STATUS=`echo 'status storage=DLT' | ${CONSOLE} | egrep '    Volume:(.*)'`

# Get the line with the VOLUME on it
echo 'STATUS is ' ${STATUS}
#VOLUMEPHRASE=`echo ${STATUS} | awk '{print $2}'`
#VOLUME=`echo ${VOLUMEPHRASE} | sed -e 's:Volume="::g' -e 's:"::g'`

TIMESTAMP=`date "+%Y/%m/%d% %H:%M"`

echo ${VOLUME}

echo -e "${TIMESTAMP}\t${VOLUME}\t${ERRORS_PER_GB}"  >> /home/dan/bacula/dlt-statistics
