scripts/monitoring/check_lastdate.sh

71 lines
1.3 KiB
Bash
Raw Normal View History

#!/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)
2021-02-25 23:27:24 +01:00
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
;;
2023-10-19 17:53:44 +02:00
*)
echo "Usage: $0 [-h] [-w <warning> (default: $warning)] [-c <critical> (default: $critical) -F <filename>]"
exit 3
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
2023-10-19 17:53:44 +02:00
if [ -z "${file}" ]
then
echo "ERROR: please provide filename to parse with $0 -F <filename>"
exit 3
fi
2023-10-19 17:53:44 +02:00
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
2023-10-19 17:53:44 +02:00
message="${file} not updated since ${delay} seconds"
2023-10-19 17:53:44 +02:00
if [ "${delay}" -gt "${critical}" ]
then
echo "CRITICAL: ${message}"
exit 2
2023-10-19 17:53:44 +02:00
elif [ "${delay}" -gt "${warning}" ]
then
echo "WARNING: ${message}"
exit 1
else
echo "OK: ${message}"
exit 0
fi