dsa-check-file_age: support following symlinks and warning on empty files
[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 # Copyright © 2009 Stephen Gran <sgran@debian.org>
5 # Copyright © 2017 Peter Palfrader
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
14 #
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 FILE=''
27 INTERVAL=60
28 EXIT=0
29
30 usage(){
31         ret=$1
32
33         cat <<EOF
34 $0: usage:
35         $0 <options>
36
37         File age checker for nagios.  Will alert if the named file is
38         older than the interval (interval default is 60 minutes)
39
40         -h      This help message
41         -i      Interval in minutes at which to alert
42         -z      Empty files are not OK
43         -L      Follow symlinks
44         -f      File to check
45 EOF
46
47         exit $ret
48 }
49
50 ZEROFAIL=""
51 FOLLOW=""
52
53 while getopts f:i:hzL opt ; do
54         case "$opt" in
55                 f) FILE="$OPTARG" ;;
56                 i) INTERVAL="$OPTARG" ;;
57                 z) ZEROFAIL="1" ;;
58                 L) FOLLOW="-L" ;;
59                 h) usage 0
60         esac
61 done
62
63 if [ -z "$FILE" ]; then
64         echo "Need file argument!" >&2
65         usage 3
66 fi
67
68 if [ ! -e "$FILE" ]; then
69         printf "state file %s is missing or unreadable\n" $FILE
70         exit 2
71 fi
72
73 if [ -n "$ZEROFAIL" ] && ! [ -s "$FILE" ]; then
74         printf "state file %s is empty\n" $FILE
75         exit 2
76 fi
77
78 if [ "$(( $( date +%s ) - $(stat $FOLLOW -c %Y $FILE) ))" -gt "$(( $INTERVAL * 60 ))" ]; then
79         printf "state file %s is older than %d minutes\n" $FILE $INTERVAL
80         exit 2
81 fi
82
83 printf "state file %s OK: updated on %s\n" $FILE "$(stat $FOLLOW -c %y $FILE)"