Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Created October 30, 2012 23:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaytaylor/3983717 to your computer and use it in GitHub Desktop.
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
#!/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
@wettenhj
Copy link

wettenhj commented Dec 5, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment