retire da-backup checks
[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 ZEROFAIL=''
30 FOLLOW=''
31
32 set -e
33 set -u
34
35 usage(){
36         ret=$1
37
38         cat <<EOF
39 $0: usage:
40         $0 <options>
41
42         File age checker for nagios.  Will alert if the named file is
43         older than the interval (interval default is 60 minutes)
44
45         -h      This help message
46         -i      Interval in minutes at which to alert
47         -z      Empty files are not OK
48         -L      Follow symlinks
49         -f      File to check
50 EOF
51
52         exit $ret
53 }
54
55 while getopts f:i:hzL opt ; do
56         case "$opt" in
57                 f) FILE="$OPTARG" ;;
58                 i) INTERVAL="$OPTARG" ;;
59                 z) ZEROFAIL="1" ;;
60                 L) FOLLOW="-L" ;;
61                 h) usage 0
62         esac
63 done
64 shift $(($OPTIND - 1))
65
66
67 if [ -z "$FILE" ] && [ "$#" = 0 ]; then
68         echo "Need file argument!" >&2
69         usage 3
70 fi
71
72 if [ -n "$FILE" ]; then
73         set dummy "$FILE" "$@"
74         shift
75 fi
76
77 msg=""
78 ok=""
79 failed=""
80 total=0
81
82 while [ "$#" -gt 0 ]; do
83         f="$1"; shift
84         total=$((total + 1))
85
86         if [ ! -e "$f" ]; then
87                 msg="${msg}state file $f is missing or unreadable\n"
88                 EXIT=2
89                 failed="$f $failed"
90         elif [ -n "$ZEROFAIL" ] && ! [ -s "$f" ]; then
91                 msg="${msg}state file $f is empty\n"
92                 EXIT=2
93                 failed="$f $failed"
94         elif [ "$(( $( date +%s ) - $(stat $FOLLOW -c %Y "$f") ))" -gt "$(( $INTERVAL * 60 ))" ]; then
95                 msg="${msg}state file $f is older than $INTERVAL minutes (updated on $(stat $FOLLOW -c %y "$f"))\n"
96                 EXIT=2
97                 failed="$f $failed"
98         else
99                 msg="${msg}state file $f OK: updated on $(stat $FOLLOW -c %y "$f")\n"
100                 ok="$f $ok"
101         fi
102 done
103
104 if [ "$total" = 1 ]; then
105         echo -n $msg
106 else
107         if [ -n "$failed" ]; then
108                 echo -n "FAIL: $failed "
109         fi
110         if [ -n "$ok" ]; then
111                 echo -n "OK: $ok "
112         fi
113         echo
114         echo -n $msg
115 fi
116 exit $EXIT