8eebe1acf513914672d5cf17b996947e7c47da65
[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 STATUS=/var/cache/dsa/nagios/apt
26 SLEEP_MAX=$(( 15 * 60 ))
27 MAX_AGE=$(( 23 * 60 * 60 ))
28
29 # we want to run if any of the following things is true
30 #  - we have never run before
31 #  - var/lib/dpkg/status has been touched since the last run
32 #  - var/cache/apt/pkgcache.bin has been touched since the last run
33 #  - our last run ended with 'apt-get update failed'
34 #  - our last run has been more than MAX_AGE (23hrs) ago
35 run_required() {
36         local run=0
37         local norun=1
38
39         [ -e "$STATUS" ] || return $run
40         [ /var/lib/dpkg/status -nt "$STATUS" ] && return $run
41         [ /var/cache/apt/pkgcache.bin -nt "$STATUS" ] && return $run
42         grep "apt-get update failed" "$STATUS" > /dev/null && return $run
43
44         local last_mod
45         last_mod=`stat -c "%Y" "$STATUS"`
46         now=`date +%s`
47         age=$(( $now - $last_mod ))
48         [ "$age" -gt "$MAX_AGE" ] && return $run
49
50         return $norun
51 }
52
53 # do stuff only when required, or when asked to
54 if [ "${1:-""}" != "-f" ] ; then
55         run_required || exit 0
56 fi
57
58 # sleep if called non-interactively
59 if [ -z "$TERM" -o "$TERM" = "dumb" ]; then
60         sleep $(( $RANDOM % $SLEEP_MAX ))
61 fi
62
63 # run apt-get update, retry a few times if it fails
64 count=0
65 while [ "$count" -lt "$UPDATE_RUNS" ]; do
66         flock -e /var/lib/apt/lists apt-get update -qq >/dev/null >&2
67         if [ "$?" = "0" ]; then break; fi
68         sleep $(( $RANDOM % 600 ))
69         count="$(( $count + 1 ))"
70 done
71 if [ "$count" -ge "$UPDATE_RUNS" ]; then
72         (echo "WARNING"
73          echo "apt-get update failed") > "$STATUS"
74          exit 1
75 fi
76
77 # run the apt check itself
78 tmp=`tempfile`
79 trap "rm -f '$tmp'" exit
80 #/usr/share/dsa/apt-status-check --noupdate --timeout=600 > "$tmp"
81 /usr/lib/nagios/plugins/dsa-check-packages > "$tmp"
82 result="$?"
83 case "$result" in
84   0)
85         st="OK"
86         ;;
87   1)
88         st="WARNING"
89         ;;
90   2)
91         st="CRITICAL"
92         ;;
93   *)
94         st="UNKNOWN"
95         ;;
96 esac
97 (echo "$st"; cat "$tmp") > "$STATUS"