7919f71a1523e2e52cd2259948ff148197e09052
[mirror/dsa-nagios.git] / dsa-nagios-checks / sbin / dsa-update-apt-status
1 #!/bin/bash
2
3 # Copyright 2009 Peter Palfrader
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 UPDATE_RUNS=3
25 STATUSDIR=/var/cache/dsa/nagios
26 STATUS="${STATUSDIR}"/apt
27 SLEEP_MAX=$(( 15 * 60 ))
28 MAX_AGE=$(( 23 * 60 * 60 ))
29
30 # we want to run if any of the following things is true
31 #  - we have never run before
32 #  - var/lib/dpkg/status has been touched since the last run
33 #  - var/cache/apt/pkgcache.bin has been touched since the last run
34 #  - our last run ended with 'apt-get update failed'
35 #  - our last run has been more than MAX_AGE (23hrs) ago
36 run_required() {
37         local run=0
38         local norun=1
39
40         [ -e "$STATUS" ] || return $run
41         [ /var/lib/dpkg/status -nt "$STATUS" ] && return $run
42         [ /var/cache/apt/pkgcache.bin -nt "$STATUS" ] && return $run
43         grep "apt-get update failed" "$STATUS" > /dev/null && return $run
44
45         local last_mod
46         last_mod=`stat -c "%Y" "$STATUS"`
47         now=`date +%s`
48         age=$(( $now - $last_mod ))
49         [ "$age" -gt "$MAX_AGE" ] && return $run
50
51         return $norun
52 }
53
54 if [ ! -d "${STATUSDIR}" ]; then
55     mkdir -p "${STATUSDIR}"
56 fi
57
58 # do stuff only when required, or when asked to
59 if [ "${1:-""}" != "-f" ] ; then
60         run_required || exit 0
61 fi
62
63 # sleep if called non-interactively
64 if [ -z "$TERM" -o "$TERM" = "dumb" ]; then
65         sleep $(( $RANDOM % $SLEEP_MAX ))
66 fi
67
68 # run apt-get update, retry a few times if it fails
69 count=0
70 while [ "$count" -lt "$UPDATE_RUNS" ]; do
71         flock -e /var/lib/apt/lists apt-get update -qq >/dev/null >&2
72         if [ "$?" = "0" ]; then break; fi
73         sleep $(( $RANDOM % 600 ))
74         count="$(( $count + 1 ))"
75 done
76 if [ "$count" -ge "$UPDATE_RUNS" ]; then
77         (echo "WARNING"
78          echo "apt-get update failed") > "$STATUS"
79          exit 1
80 fi
81
82 # run the apt check itself
83 tmp=`tempfile`
84 trap "rm -f '$tmp'" exit
85 #/usr/share/dsa/apt-status-check --noupdate --timeout=600 > "$tmp"
86 /usr/lib/nagios/plugins/dsa-check-packages > "$tmp"
87 result="$?"
88 case "$result" in
89   0)
90         st="OK"
91         ;;
92   1)
93         st="WARNING"
94         ;;
95   2)
96         st="CRITICAL"
97         ;;
98   *)
99         st="UNKNOWN"
100         ;;
101 esac
102 (echo "$st"; cat "$tmp") > "$STATUS"