#!/bin/bash 

# check_tsm_backup.sh: A script to check the status of TSM backups on a server

# Set up some variables
EXEC_MODE="normal"
DSMC_CMD="/usr/bin/dsmc"

usage()
{
cat << EOF
usage: $0 options

This script checks the status of TSM backups on this local server.

OPTIONS:
   -h	Displays this message
   -w	Number of days a backup can fail before a WARNing alert is triggered
   -c	Number of days a backup can fail before a CRITICAL alert is triggered
   -t   Test mode. (Specify a date and the script will use that as the last backup date)

EOF
}

remediate()
{
sudo $DSMC_CMD incr > /dev/null
}



while getopts "c:ht:w:" OPTION
do
   case $OPTION in
	h)
		usage
		exit 1;;

	w)	WARN_DAYS=$OPTARG
		WARN_EPOCH=`date +%s --date "$OPTARG days ago"`;;

	c)	CRIT_DAYS=$OPTARG
		CRIT_EPOCH=`date +%s --date "$OPTARG days ago"`;;

	t)	BACKUP_DATE=$OPTARG
		EXEC_MODE="test"
		echo "Using" $BACKUP_DATE "as test last backup date.";;
   esac
done

if [ -z "$WARN_DAYS" ] || [ -z "$CRIT_DAYS" ]
then
   usage
   exit 1
fi

# Let's start checking to see if this script is gonna run.

if [ ! -f $DSMC_CMD ]; then
	echo "UNKNOWN - DSMC binary not found"
	exit 3
fi



# Lets chew through the output of dsmc to see what we've got backed up 

if [ "$EXEC_MODE" == "test" ]; then
		DSMCQ_RESULTS=`echo -e $BACKUP_DATE "\t/testmode"`
	else
		#dsmc q f | sed -e 's/^[ \t]*//' -ne '/^[0-9]/p' | awk '{print $2 "\t" $5}' > $DSMCQ_RESULTS
		DSMCQ_RESULTS=`sudo $DSMC_CMD q f | sed -e 's/^[ \t]*//' -ne '/^[0-9]/p' | awk '{print $2 "\t" $5}'`
fi

# TODO - this string will tell us our local filesystems
#cat /etc/fstab | sed -ne '/^\//p' -e '/^LABEL/p' | awk '{print $2}' | sed '/\/tmp/d' | sed '/swap/d' > $LOCALFS

# Let's see if our backups happened

while read line
do
	BACKUP_DATE=`echo $line | awk '{print $1}'`
	BACKUP_EPOCH=`date +%s --date "$BACKUP_DATE"`
	BACKUP_FS=`echo $line | awk '{print $2}'`
	
	if [ "$BACKUP_EPOCH" -le "$CRIT_EPOCH" ]; then
		CRIT=("${CRIT[@]}" $BACKUP_FS "[`date +"%m-%d-%y" -d @$BACKUP_EPOCH`]")
	elif [ "$BACKUP_EPOCH" -le "$WARN_EPOCH" ]; then
		WARN=("${WARN[@]}" $BACKUP_FS "[`date +"%m-%d-%y" -d @$BACKUP_EPOCH`]")
	else
		OK=("${OK[@]}" $BACKUP_FS "[`date +"%m-%d-%y" -d @$BACKUP_EPOCH`]")
	fi

done <<< "$DSMCQ_RESULTS"

# Now, to generate our error messages and exit the script with the proper exit code.

if [ ${#CRIT[@]} -ne 0 ]; then
	echo "CRITICAL - One or more TSM backups has failed: ${CRIT[@]}"
	remediate &
	exit 2
elif [ ${#WARN[@]} -ne 0 ]; then
	echo "WARNING - One or more TSM backups has failed: ${WARN[@]}"
	remediate &
	exit 1
else
	echo "OK - ${OK[@]}"
fi
