#!/bin/bash -

# Checks if ISPConfig cronjob "cron.sh" ran recently.
RUNFILE=/var/log/ispconfig/cron.run
if [ ! -f "${RUNFILE}" ]; then
  echo "UNKNOWN - Unable to stat file [${RUNFILE}]: No such file or directory" >&2
  exit 3
fi

WARN_DAYS=3
CRIT_DAYS=7
: $(( last_mod=`stat -c %X "${RUNFILE}"` ))
: $(( diff_days=( `date +"%s"` -  last_mod ) / 60 / 60 / 24 ))

if [ $diff_days -gt $CRIT_DAYS ]; then
  echo "CRITICAL - last run is more than $CRIT_DAYS days ago [${diff_days}]" >&2
  exit 2
fi

if [ $diff_days -gt $WARN_DAYS ]; then
  echo "WARNING - last run is more than $WARN_DAYS days ago [${diff_days}]" >&2
  exit 1
fi

echo "OK - ispconfig cron is healthy"
exit 0

