#!/bin/sh
# -*- sh -*-

: << =cut

=head1 NAME

backup_status - Plugin to monitor the backup status.

=head1 CONFIGURATION

The following environment variables are used by this plugin:

=over 4

=item backup_status_warning <float>

Threshold for when to report a warning

=item backup_status_critical <float>

Threshold for when to report a critical

=back

=head2 EXAMPLE CONFIGURATION

 [backup_status]
  env.backup_status_warning 1
  env.backup_status_critical 2

=head1 NOTES

If run with the "autoconf"-parameter, give our opinion on wether we
should be run on this system or not. This is optinal, and only used by
munin-config. In the case of this plugin, we should most probably
always be included.

=head1 MAGIC MARKERS

 #%# family=auto
 #%# capabilities=autoconf

=cut

#. $MUNIN_LIBDIR/plugins/plugin.sh
. $BACKUP_LIBDIR/backup.properties

if [ "$1" = "autoconf" ]; then
	echo yes
	exit 0
fi

# Reads the timestamp from the backup.status.log file
getBackupStatusTimeStamp() {
    cat $backup_status_log_file | sed -e 's/^\[.*\] \(.*\)$/\1/g'
}

# 0 = OK
# 1 = WARNING
# 2 = NOT_FINISHED
# 3 = NOT_STARTED
# 4 = ERROR 
getBackupStatus() {
    result=0
    backup_status_log_file=$1
    if [ ! -e $backup_status_log_file ]; then
	result=3
	
    else
	status=$(cat $backup_status_log_file | sed -e 's/^\[\(.*\)\].*/\1/g')
	case "$status" in
	    'STARTED')
		backup_started_at=$(getBackupStatusTimeStamp)
                # Check the time spent for backup
		time_spent=$(expr $(date '+%s') - $(date -d "$backup_started_at" '+%s'))
                # Backup took more then 3 hours => error
		if [ $time_spent -gt $BACKUP_DURATION_TIME_IN_SECONDS ]; then
		    result=2
		fi
		;;
	    'FINISHED')
		backup_finished_at=$(getBackupStatusTimeStamp)
                # Check the time the last backup ran
		time=$(expr $(date '+%s') - $(date -d "$backup_finished_at" '+%s'))
		
                # Last backup ran more than 25 hours ago=> error
		if [ $time -gt $BACKUP_INTERVAL_TIME_IN_SECONDS ]; then
		    result=3
		fi	        
		;;
	    'WARN')
		result=1
		;;
	    'ERROR')
		result=4;
		;;	    
	esac
    fi
	    
    echo  "backup_status.value $result"

}

# If run with the "config"-parameter, give out information on how the
# graphs should look. 
 
if [ "$1" = "config" ]; then

	# The host name this plugin is for. (Can be overridden to have
	# one machine answer for several)

	# The title of the graph
	echo 'graph_title Backup status'
	# Arguments to "rrdtool graph". In this case, tell it that the
	# lower limit of the graph is '0', and that 1k=1000 (not 1024)
	echo 'graph_args --base 1000 -l 0'
	# The Y-axis label
	echo 'graph_vlabel backup status'
	# We want Cur/Min/Avg/Max unscaled (i.e. 0.42 load instead of
	# 420 milliload)
	echo 'graph_scale no'
	# Graph category. Defaults to 'other'
	echo 'graph_category backup'
	# The fields. "label" is used in the legend. "label" is the only
	# required subfield. 
	echo 'backup_status.label backup status'

	# This one is purely to add an explanation to the web page. The first
	# one is for the graph itself, while the second one is for the field
	# "load".
	echo 'graph_info The backup status of Just Chat'
	echo 'backup_status.info The backup status (0=OK, 1=WARN, 2=NOT_FINISHED, 3=NOT_STARTED, 4=ERROR)'
	# Add the warning and critical level
	echo 'backup_status.warning 1'
	echo 'backup_status.critical 2'
	# Last, if run with the "config"-parameter, quit here (don't
	# display any data)
	exit 0
fi

# If not run with any parameters at all (or only unknown ones), do the
# real work - i.e. display the data. Almost always this will be
# "value" subfield for every data field.

getBackupStatus $BACKUP_STATUS_LOG_FILE
