Generalize (and simplify) the puppet file age check so that we can reuse
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-file_age
1 #!/bin/sh
2 # vim: set fileencoding=utf-8 ai noet sts=8 sw=8 tw=0:
3
4 FILE=''
5 INTERVAL=60
6 EXIT=0
7
8 usage(){
9         ret=$1
10
11         cat <<EOF
12 $0: usage:
13         $0 <options>
14
15         File age checker for nagios.  Will alert if the named file is
16         older than the interval (interval default is 60 minutes)
17
18         -h      This help message
19         -i      Interval in minutes at which to alert
20         -f      File to check
21 EOF
22
23         exit $ret
24 }
25
26 while getopts f:i:h opt ; do
27         case "$opt" in
28                 f) FILE="$OPTARG" ;;
29                 i) INTERVAL="$OPTARG" ;;
30                 h) usage 0
31         esac
32 done
33
34 if [ -z "$FILE" ]; then
35         echo "Need file argument!" >&2
36         usage 1
37 fi
38
39 if [ ! -r "$FILE" ]; then
40         printf "state file %s is missing or unreadable\n" $FILE
41         exit 2
42 fi
43
44 if [ "$(( $( date +%s ) - $(stat -c %Y $FILE) ))" -gt "$(( $INTERVAL * 60 ))" ]; then
45         printf "state file %s is older than %d minutes\n" $FILE $INTERVAL
46         exit 2
47 fi
48
49 printf "state file %s OK: updated on %s\n" $FILE "$(stat -c %y $FILE)"