[project @ peter@palfrader.org-20080706094246-lgxrzpnljewux8g5]
[mirror/dsa-nagios.git] / dsa-nagios-nrpe-config / dsa-update-apt-status
1 #!/bin/sh
2
3 UPDATE_RUNS=3
4 STATUS=/var/cache/dsa/nagios/apt
5
6 run_required() {
7         local run=0
8         local norun=1
9
10         [ -e "$STATUS" ] || return $run
11         [ /var/lib/dpkg/status -nt "$STATUS" ] && return $run
12         [ /var/cache/apt/pkgcache.bin -nt "$STATUS" ] && return $run
13         grep "apt-get update failed" "$STATUS" > /dev/null && return $run
14 }
15
16 sleep_max=7200
17
18 if [ "$1" == "hourly" ] ; then
19         # on the hourly run, only update when the system changed since the last run
20         run_required || exit
21         sleep_max=900
22 fi
23
24 # sleep if called non-interactively
25 if [ -z "$TERM" -o "$TERM" = "dumb" ]; then
26         sleep $(( $RANDOM % $sleep_max ))
27 fi
28
29 # run apt-get update, retry a few times if it fails
30 count=0
31 while [ "$count" -lt "$UPDATE_RUNS" ]; do
32         apt-get update -qq
33         if [ "$?" = "0" ]; then break; fi
34         sleep $(( $RANDOM % 600 ))
35         count="$(( $count + 1 ))"
36 done
37 if [ "$count" -ge "$UPDATE_RUNS" ]; then
38         (echo "WARNING"
39          echo "apt-get update failed") > "$STATUS"
40          exit 1
41 fi
42
43 # run the apt check itself
44 tmp=`tempfile`
45 trap "rm -f '$tmp'" exit
46 /usr/share/dsa/apt-status-check --noupdate --timeout=600 > "$tmp"
47 result="$?"
48 case "$result" in
49   0)
50         st="OK"
51         ;;
52   1)
53         st="WARNING"
54         ;;
55   2)
56         st="CRITICAL"
57         ;;
58   *)
59         st="UNKNOWN"
60         ;;
61 esac
62 (echo "$st"; cat "$tmp") > "$STATUS"