dsa-update-apt-status: it's not an error to exit when we don't need to run
[mirror/dsa-nagios.git] / dsa-nagios-checks / sbin / dsa-update-apt-status
1 #!/bin/bash
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, or when asked to
33 if [ "${1:-""}" != "-f" ] ; then
34         run_required || exit 0
35 fi
36
37 # sleep if called non-interactively
38 if [ -z "$TERM" -o "$TERM" = "dumb" ]; then
39         sleep $(( $RANDOM % $SLEEP_MAX ))
40 fi
41
42 # run apt-get update, retry a few times if it fails
43 count=0
44 while [ "$count" -lt "$UPDATE_RUNS" ]; do
45         apt-get update -qq >/dev/null >&2
46         if [ "$?" = "0" ]; then break; fi
47         sleep $(( $RANDOM % 600 ))
48         count="$(( $count + 1 ))"
49 done
50 if [ "$count" -ge "$UPDATE_RUNS" ]; then
51         (echo "WARNING"
52          echo "apt-get update failed") > "$STATUS"
53          exit 1
54 fi
55
56 # run the apt check itself
57 tmp=`tempfile`
58 trap "rm -f '$tmp'" exit
59 #/usr/share/dsa/apt-status-check --noupdate --timeout=600 > "$tmp"
60 /usr/lib/nagios/plugins/dsa-check-packages > "$tmp"
61 result="$?"
62 case "$result" in
63   0)
64         st="OK"
65         ;;
66   1)
67         st="WARNING"
68         ;;
69   2)
70         st="CRITICAL"
71         ;;
72   *)
73         st="UNKNOWN"
74         ;;
75 esac
76 (echo "$st"; cat "$tmp") > "$STATUS"