scripts/monitoring/check_lastdate.sh
2023-10-19 17:53:44 +02:00

70 lines
1.3 KiB
Bash
Executable file

#!/bin/sh
# checks for a +%Y-%m-%d %H:%M:%S-formated date in the last two lines of a file
# default values:
# 60s*60m*4h
warning=14400
# 60s+60m*24h
critical=86400
OPTIND=1
while getopts "hF:C:W:" opt; do
case "$opt" in
h)
echo "Usage: $0 [-h] [-w <warning> (default: $warning)] [-c <critical> (default: $critical) -F <filename>]"
exit 3
;;
W)
warning=$OPTARG
;;
C)
critical=$OPTARG
;;
F)
file=$OPTARG
;;
*)
echo "Usage: $0 [-h] [-w <warning> (default: $warning)] [-c <critical> (default: $critical) -F <filename>]"
exit 3
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
if [ -z "${file}" ]
then
echo "ERROR: please provide filename to parse with $0 -F <filename>"
exit 3
fi
lastdate=$(tail -2 "${file}" | grep -o -E '^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}')
lastts=$(date -d "${lastdate}" +%s)
currentts=$(date +%s)
if [ "x${lastts}" != "x" ]
then
delay=$(echo "${currentts}-${lastts}" | bc)
else
echo "UNKNOWN: date not found"
exit 3
fi
message="${file} not updated since ${delay} seconds"
if [ "${delay}" -gt "${critical}" ]
then
echo "CRITICAL: ${message}"
exit 2
elif [ "${delay}" -gt "${warning}" ]
then
echo "WARNING: ${message}"
exit 1
else
echo "OK: ${message}"
exit 0
fi