#! /bin/sh
# Author : Ludovic Strappazon. l.strappazon@gmail.com
# Copyright : Kern Sibbald 
# Any comment, advice or enhancement are welcome :-)

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
MYSQL="/usr/bin/mysql -u bacula --password=XXXXXX"
TMP=/tmp
BACULA=/usr/local/bacula

PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
STATUS=""

. $PROGPATH/utils.sh

print_usage() {
	echo "Usage: $PROGNAME -C <client> -f <full warning threshold> -F <full critical threshold> -d <differential warning threshold>  -D <differential critical threshold> -i <incremental warning threshold> -I <incremental critical threshold>"
        echo ""
	echo "Help : $PROGNAME -h"
	echo ""
}

print_help() {
        echo ""
	print_usage
        echo "This plugin checks the age of the last valid backup of a client for each level"
        echo "Example : with $PROGNAME -C MyClient -f 31 -F 35 -d 7 -D 10 -i 1 -I 2"
	echo "you will get :"
	echo ""
	echo "- OK if the last full for MyClient occured yesterday"
        echo "- OK if the last full occured less than 31 days ago, and the last differential occured less than 7 days ago, and the last incremental occured yesterday"
        echo "- WARNING if the last full occured less than 31 days ago, and the last differential occured more than 7 days ago but less than 10, and the last incremental occured yesterday"
        echo "- CRITICAL if the last full occured less than 31 days ago, and the last differential occured less than 7 days ago, and the last incremental occured more than 2 days ago"
	echo ""
	echo "and so on."
	
	echo ""
	echo "The Information Status are the ages of the last valid backup for each level."
	echo ""
	echo "Note : it make no sense to check the last differential's age or the last incremental's age while they are greater than the last full's age. So if a full occured more recently than the differential (or the incremental), then the differential (or the incremental) takes the age off the full"
	echo ""
	echo "I think this plugin should be used in passive mode, and ran by crontab on the backup server"
	echo ""
	exit 3
}

FULL_WARNING=1000000000000
FULL_CRITICAL=9000000000000
DIFF_WARNING=1000000000000
DIFF_CRITICAL=9000000000000
INCR_WARNING=1000000000000
INCR_CRITICAL=9000000000000

NB_ARGS=$#
while getopts :c:J:f:F:d:D:i:I:h OPTION
do
  case $OPTION in
    c) CATALOG="$OPTARG"
       ;;
    J) JOB_NAME="$OPTARG"
       ;;
    f) FULL_WARNING="$OPTARG"
       ;;
    F) FULL_CRITICAL="$OPTARG"
       ;;
    d) DIFF_WARNING="$OPTARG"
       ;;
    D) DIFF_CRITICAL="$OPTARG"
       ;;
    i) INCR_WARNING="$OPTARG"
       ;;
    I) INCR_CRITICAL="$OPTARG"
       ;;	  
    h) print_help
       exit 3
       ;;
    *) print_usage
       exit 3
       ;;
  esac
done
shift $(($OPTIND - 1))

if [ "$NB_ARGS" -le 3 -o "$NB_ARGS" -gt 16 ]; then
        echo ""
        print_revision $PROGNAME 13/06/2006
        echo ""
        print_usage
        exit 3
fi

if [ "$FULL_WARNING" -gt "$FULL_CRITICAL" -o "$DIFF_WARNING" -gt "$DIFF_CRITICAL" -o "$INCR_WARNING" -gt "$INCR_CRITICAL" ]
   then 
   echo "Please provide a warning and a critical threshold for each level you want to check."
   echo "The warning threshold must be strictly inferior to the critical threshold." 
   exit 3
fi

LAST_CHECK=`ps -ef | grep check_ba[Cc]ula_backup.sh | awk {'print $5'} | uniq | wc -l`
if [ "$LAST_CHECK" -gt 1 ]; then
	echo "The last check was not complete, you should increase the check_period."
	exit 3
fi

TODAY=`date +%s`

FULL_WARNING=$[FULL_WARNING*3600*24]
FULL_CRITICAL=$[FULL_CRITICAL*3600*24]
DIFF_WARNING=$[DIFF_WARNING*3600*24]
DIFF_CRITICAL=$[DIFF_CRITICAL*3600*24]
INCR_WARNING=$[INCR_WARNING*3600*24]
INCR_CRITICAL=$[INCR_CRITICAL*3600*24]

LAST_VALID_FULL=`$MYSQL << EOF
USE $CATALOG
SELECT MAX(JobTDate) from Job where Name="$JOB_NAME" AND Level="F" AND JobStatus="T";
EOF`
LAST_VALID_FULL=`echo $LAST_VALID_FULL | cut -f 2 -d ' '`

LAST_VALID_DIFF=`$MYSQL << EOF
USE $CATALOG
SELECT Max(JobTDate) from Job where Name="$JOB_NAME" AND Level="D" AND JobStatus="T";
EOF`
LAST_VALID_DIFF=`echo $LAST_VALID_DIFF | cut -f 2 -d ' '`

LAST_VALID_INCR=`$MYSQL << EOF
USE $CATALOG
SELECT Max(JobTDate) from Job where Name="$JOB_NAME" AND Level="I" AND JobStatus="T";
EOF`
LAST_VALID_INCR=`echo $LAST_VALID_INCR | cut -f 2 -d ' '`

AGE_FULL=$[TODAY-LAST_VALID_FULL]
AGE_DIFF=$[TODAY-LAST_VALID_DIFF]
AGE_INCR=$[TODAY-LAST_VALID_INCR]

if [ "$AGE_FULL" -lt "$AGE_DIFF" ]; then AGE_DIFF=$AGE_FULL;fi
if [ "$AGE_FULL" -lt "$AGE_INCR" ]; then AGE_INCR=$AGE_FULL
elif [ "$AGE_DIFF" -lt "$AGE_INCR" ]; then AGE_INCR=$AGE_DIFF
fi

#echo "Agefull=$AGE_FULL  FW=$FULL_WARNING FC=$FULL_CRITICAL"
#echo "Agediff=$AGE_DIFF  FW=$DIFF_WARNING FC=$DIFF_CRITICAL"
#echo "Ageincr=$AGE_INCR  FW=$INCR_WARNING FC=$INCR_CRITICAL"

MESSAGE="Full: $[AGE_FULL/86400] days - Diff: $[AGE_DIFF/86400] days - Incr: $[AGE_INCR/86400] days"

if [ "$AGE_FULL" -lt "$FULL_WARNING" -a "$AGE_DIFF" -lt "$DIFF_WARNING" -a "$AGE_INCR" -lt "$INCR_WARNING" ]; then
   echo "$MESSAGE"
   exit 0
elif [ "$AGE_FULL" -lt "$FULL_CRITICAL" -a "$AGE_DIFF" -lt "$DIFF_CRITICAL" -a "$AGE_INCR" -lt "$INCR_CRITICAL" ];then
   echo "$MESSAGE"
   exit 1
else
   echo "$MESSAGE"
   exit 2
fi
exit 3
