3e3acefbb627d3839494b1ffe7c54d1a061185d7
[mirror/dsa-nagios.git] / dsa-nagios-checks / dsa-update-apt-status
1 #!/bin/sh
2
3 UPDATE_RUNS=3
4 STATUS=/var/cache/dsa/nagios/apt
5 SLEEP_MAX=$(( 15 * 60 ))
6 MAX_AGE=$(( 23 * 60 * 60 ))
7
8 # we want to run if any of the following things is true
9 #  - we have never run before
10 #  - var/lib/dpkg/status has been touched since the last run
11 #  - var/cache/apt/pkgcache.bin has been touched since the last run
12 #  - our last run ended with 'apt-get update failed'
13 #  - our last run has been more than MAX_AGE (23hrs) ago
14 run_required() {
15         local run=0
16         local norun=1
17
18         [ -e "$STATUS" ] || return $run
19         [ /var/lib/dpkg/status -nt "$STATUS" ] && return $run
20         [ /var/cache/apt/pkgcache.bin -nt "$STATUS" ] && return $run
21         grep "apt-get update failed" "$STATUS" > /dev/null && return $run
22
23         local last_mod
24         last_mod=`stat -c "%Y" "$STATUS"`
25         now=`date +%s`
26         age=$(( $now - $last_mod ))
27         [ "$age" -gt "$MAX_AGE" ] && return $run
28
29         return $norun
30 }
31
32 # do stuff only when required
33 run_required || exit
34
35 # sleep if called non-interactively
36 if [ -z "$TERM" -o "$TERM" = "dumb" ]; then
37         sleep $(( $RANDOM % $SLEEP_MAX ))
38 fi
39
40 # run apt-get update, retry a few times if it fails
41 count=0
42 while [ "$count" -lt "$UPDATE_RUNS" ]; do
43         apt-get update -qq >/dev/null >&2
44         if [ "$?" = "0" ]; then break; fi
45         sleep $(( $RANDOM % 600 ))
46         count="$(( $count + 1 ))"
47 done
48 if [ "$count" -ge "$UPDATE_RUNS" ]; then
49         (echo "WARNING"
50          echo "apt-get update failed") > "$STATUS"
51          exit 1
52 fi
53
54 # run the apt check itself
55 tmp=`tempfile`
56 trap "rm -f '$tmp'" exit
57 #/usr/share/dsa/apt-status-check --noupdate --timeout=600 > "$tmp"
58 /usr/lib/nagios/plugins/dsa-check-packages > "$tmp"
59 result="$?"
60 case "$result" in
61   0)
62         st="OK"
63         ;;
64   1)
65         st="WARNING"
66         ;;
67   2)
68         st="CRITICAL"
69         ;;
70   *)
71         st="UNKNOWN"
72         ;;
73 esac
74 (echo "$st"; cat "$tmp") > "$STATUS"