Skip to content

Instantly share code, notes, and snippets.

@jehiah
Created March 31, 2015 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jehiah/d5edf12be6dda1078c90 to your computer and use it in GitHub Desktop.
Save jehiah/d5edf12be6dda1078c90 to your computer and use it in GitHub Desktop.
Monitor files for recent timestamps
#!/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