#!/bin/bash
# /usr/lib/he-nagios/plugins/raid/check_raid /usr/lib/he-nagios/plugins/raid/cron-check_raid.sh /var/cache/he-nagios-plugins-local
# This is only a wrapper for managed hosting to use 
# the raid check from host europe sa, without cron.
#
# We only want to run raid checks if we configure
# them in nagios not every host two times a hour.

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

STATUS_DIR=/var/cache/he-nagios-plugins-local

if [[ ! -d /var/cache/he-nagios-plugins-local ]]; then
    # daemonize raid check
    sudo /usr/lib/he-nagios/plugins/raid/cron-check_raid.sh </dev/null >/dev/null 2>&1 &
    echo "Initializing..."
    exit 0
fi

/usr/lib/he-nagios/plugins/raid/check_raid
RETVAL=$?

CHANGE_TIME=$(stat -c '%Y' $STATUS_DIR)
NOW=$(date +%s)
AGE=$(( $NOW - $CHANGE_TIME ))
if (( $AGE >= 3600 )); then
    # daemonize raid check
    sudo /usr/lib/he-nagios/plugins/raid/cron-check_raid.sh </dev/null >/dev/null 2>&1 &
fi
if (( $AGE >= 14400 )); then
    # Hey this is a little bit to old
    echo "Warning: The age of $STATUS_DIR is older than 4 hours. Please check it"
    exit $STATE_WARNING 
fi

exit $RETVAL
