Created
June 16, 2016 11:26
-
-
Save grawert/d8281f56ffaf4bfd9b763d33a89babad to your computer and use it in GitHub Desktop.
nagios check file age
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
NAGIOS_EXIT_OK=0 | |
NAGIOS_EXIT_WARNING=1 | |
NAGIOS_EXIT_CRITICAL=2 | |
NAGIOS_EXIT_UNKOWN=3 | |
FILES= | |
function help_and_exit() { | |
cat << EOF | |
Usage: $0 -w [warning %] -c [critical %] filename filename ... | |
Options: | |
-w [0-99] = file age in hours | |
-c [0-99] = file age in hours | |
EOF | |
exit $NAGIOS_EXIT_UNKOWN | |
} | |
while (( $# )); do | |
case $1 in | |
--help|-h) help_and_exit ;; | |
-w) WARN="$2"; shift 2 ;; | |
-c) CRIT="$2"; shift 2 ;; | |
*) FILES+=" $1"; shift ;; | |
esac | |
done | |
# no files given as parameters | |
[[ -z "$FILES" ]] && help_and_exit | |
# defaults | |
[ -z "$WARN" ] && WARN=24 | |
[ -z "$CRIT" ] && CRIT=48 | |
TIME_WARN=$(date +%s --date="$WARN hours ago") | |
TIME_CRIT=$(date +%s --date="$CRIT hours ago") | |
EXIT_MSG=() | |
EXIT_CODE=$NAGIOS_EXIT_OK | |
for FILE in $FILES; do | |
test -s $FILE | |
case $? in | |
0) | |
FILE_AGE=$(stat --format="%Y" $FILE) | |
if [[ $FILE_AGE -lt $TIME_WARN ]]; then | |
EXIT_MSG+=("WARNING - $FILE: $(date -d @$FILE_AGE)") | |
(( EXIT_CODE |= NAGIOS_EXIT_WARNING )) | |
continue | |
fi | |
if [[ $FILE_AGE -lt $TIME_CRIT ]]; then | |
EXIT_MSG+=("CRITICAL - $FILE: $(date -d @$FILE_AGE)") | |
(( EXIT_CODE |= NAGIOS_EXIT_CRITICAL )) | |
continue | |
fi | |
EXIT_MSG+=("OK - $FILE: $(date -d @$FILE_AGE)") | |
(( EXIT_CODE |= NAGIOS_EXIT_OK )) | |
;; | |
*) | |
EXIT_MSG+=("CRITICAL - zero size or does not exist: $FILE") | |
(( EXIT_CODE |= NAGIOS_EXIT_CRITICAL )) | |
;; | |
esac | |
done | |
(( EXIT_CODE & $NAGIOS_EXIT_CRITICAL )) && EXIT_CODE=$NAGIOS_EXIT_CRITICAL | |
printf '%s\n' "${EXIT_MSG[@]}" | |
exit $EXIT_CODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment