#!/bin/bash
# TINA  plugin for Nagios
# Written by M.Koettenstorfer (mko@lihas.de)
# Last Modified: 27-11-2008
#
# Description:
#
# This plugin will check the status of local
# TINA Backups.
#

# get tina enviroment
RUNTINA="/usr/Atempo/tina/Bin/runtina"
TINABACKUPSTATE="$RUNTINA  tina_acct  -output_format  csv  -csv_separator  :   -v_platform  -v_status  -v_folder  -v_jobtype"

# Nagios return codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

if [ ! -x "$RUNTINA" ]
then
echo "UNKNOWN: $TINALISTTAPES not found or is not executable by the nagios user"
exitstatus=$STATE_UNKNOWN
exit $exitstatus
fi

PROGNAME=`basename $0`

check_backup_state()
{
	result=$(echo $FULLRESULT| grep -v -i ok | grep -v -i running)
		if [ -n "$result" ]
			then
				MESSAGE="TINA CRITICAL Backup failed - $FULLSTRING"
				exitstatus=$STATE_CRITICAL

			else
				MESSAGE="TINA OK BACKUPS SUCESSFULL - $FULLSTRING"
				exitstatus=$STATE_OK
		fi

}

exitstatus=$STATE_UNKNOWN #default

JOBTMP=`mktemp -t tina_backup_state.XXXXXX` # Create a tmpfile to store full results
TMP=`mktemp -t tina_state.XXXXXX` # Create a tmpfile to store the test results
$TINABACKUPSTATE | tr ':' ' ' | grep -v  Platform | grep -v  "backup - catalog"|    awk '{ print $2 }'  > $TMP 
FULLRESULT=`< $TMP` # populate test result
$TINABACKUPSTATE | tr ':' ' ' > $JOBTMP 
FULLSTRING=`< $JOBTMP` # populate Fullstring 
check_backup_state
rm -rf $JOBTMP
rm -rf $TMP

echo $MESSAGE
exit $exitstatus


