Created
October 30, 2012 23:15
-
-
Save jaytaylor/3983717 to your computer and use it in GitHub Desktop.
Nagios NRPE plugin: Check the number of seconds since a file or directory was last modified
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
#!/usr/bin/env bash | |
## | |
# @author Jay Taylor [@jtaylor] | |
# | |
# @date 2012-10-30 | |
# | |
# @description Nagios NRPE plugin: Check the number of seconds since a file | |
# or directory was last modified. | |
# | |
DEFAULT_WARN_SECONDS=60 | |
DEFAULT_CRIT_SECONDS=120 | |
while getopts 'hp:c:w:' option; do | |
case $option in | |
h) help=1;; | |
p) path=$OPTARG;; | |
c) crit=$OPTARG;; | |
w) warn=$OPTARG;; | |
esac | |
done | |
if [ -n "$help" ] || [ -z "$path" ]; then | |
echo "usage: $0 -p [path (required)] -w [warning threshhold seconds] -c [critical threshhold seconds]" 1>&2 | |
exit 4 | |
elif ! [ -e "$path" ]; then | |
echo "fatal: invalid or unreadable file path provided: $path" 1>&2 | |
exit 3 | |
fi | |
if [ -z "$warn" ]; then | |
warn=$DEFAULT_WARN_SECONDS | |
fi | |
if [ -z "$crit" ]; then | |
crit=$DEFAULT_CRIT_SECONDS | |
fi | |
seconds=$(($(date +'%s') - $(stat --format='%Y' $path))) | |
if [ $seconds -gt $crit ]; then | |
echo "CRITICAL: $path was last modified $seconds seconds ago" | |
exit 1 | |
elif [ $seconds -gt $warn ]; then | |
echo "WARNING: $path was last modified $seconds seconds ago" | |
exit 2 | |
else | |
echo "OK: $path was last modified $seconds seconds ago" | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found this useful, thanks! But I think the exit codes for CRITICAL and WARNING are the wrong way around - it should be "exit 2" for CRITICAL and "exit 1" for WARNING: http://nagios-plugins.org/doc/guidelines.html#AEN78