Created
March 31, 2015 01:18
-
-
Save jehiah/d5edf12be6dda1078c90 to your computer and use it in GitHub Desktop.
Monitor files for recent timestamps
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 | |
WARN="180" | |
CRITICAL="300" | |
USE_MODIFY_TIME=false # default to use the contents of the file | |
while [ "$1" != "" ]; do | |
param=${1%%=*} | |
value=${1#*=} | |
case $param in | |
--warn) | |
WARN=$value | |
;; | |
--critical) | |
CRITICAL=$value | |
;; | |
--use-modify-time) | |
USE_MODIFY_TIME=true | |
;; | |
--file) | |
FILE=$value | |
;; | |
--find-dir) | |
FIND_DIR=$value | |
;; | |
--find-name) | |
FIND_NAME=$value | |
;; | |
esac | |
shift | |
done | |
[ -z "$FILE" ] && [ -z "$FIND_DIR" ] && echo "missing --file or --find-name && --find-dir" && exit 3; | |
[ -n "$FIND_DIR" ] && [ -z "$FIND_NAME" ] && echo "missing --find-name" && exit 3 | |
NOW=`date -u +%s` | |
WARN_MIN=$(($NOW - $WARN)) | |
CRITICAL_MIN=$(($NOW - $CRITICAL)) | |
if [ -n "$FILE" ]; then | |
[ ! -f "$FILE" ] && echo "file $FILE does not exist" && exit 3 | |
if [ $USE_MODIFY_TIME ]; then | |
DT=`stat --format=%Y "$FILE"` | |
else | |
DT=`cat "$FILE"` | |
fi | |
DIFF=$(($NOW - $DT)) | |
echo "$FILE is $DIFF seconds old (warn at $WARN critical at $CRITICAL)" | |
if [ $DT -lt $CRITICAL_MIN ]; then | |
exit 2; | |
fi | |
if [ $DT -lt $WARN_MIN ]; then | |
exit 1; | |
fi | |
exit 0; | |
else | |
status="" | |
files_found=0 | |
warning_exit="false" | |
critical_exit="false" | |
for filename in `find -L $FIND_DIR -name "$FIND_NAME" -print -type f`; do | |
if [ $USE_MODIFY_TIME ]; then | |
DT=`stat --format=%Y "$filename"` | |
else | |
DT=`cat "$filename"` | |
fi | |
DIFF=$(($NOW - $DT)) | |
files_found=$(( $files_found + 1 )) | |
if [ $DT -lt $CRITICAL_MIN ]; then | |
status="$status $filename (${DIFF}s old)" | |
critical_exit="true" | |
elif [ $DT -lt $WARN_MIN ]; then | |
status="$status $filename (${DIFF}s old)" | |
warning_exit="true" | |
fi | |
done | |
if [ -n "$status" ]; then | |
echo $status | |
else | |
echo "$files_found files OK" | |
fi | |
[ $critical_exit == "true" ] && exit 2 | |
[ $warning_exit == "true" ] && exit 1 | |
exit 0; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment