3 # Copyright 2009 Peter Palfrader
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:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
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.
25 STATUS=/var/cache/dsa/nagios/apt
26 SLEEP_MAX=$(( 15 * 60 ))
27 MAX_AGE=$(( 23 * 60 * 60 ))
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
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
45 last_mod=`stat -c "%Y" "$STATUS"`
47 age=$(( $now - $last_mod ))
48 [ "$age" -gt "$MAX_AGE" ] && return $run
53 # do stuff only when required, or when asked to
54 if [ "${1:-""}" != "-f" ] ; then
55 run_required || exit 0
58 # sleep if called non-interactively
59 if [ -z "$TERM" -o "$TERM" = "dumb" ]; then
60 sleep $(( $RANDOM % $SLEEP_MAX ))
63 # run apt-get update, retry a few times if it fails
65 while [ "$count" -lt "$UPDATE_RUNS" ]; do
66 apt-get update -qq >/dev/null >&2
67 if [ "$?" = "0" ]; then break; fi
68 sleep $(( $RANDOM % 600 ))
69 count="$(( $count + 1 ))"
71 if [ "$count" -ge "$UPDATE_RUNS" ]; then
73 echo "apt-get update failed") > "$STATUS"
77 # run the apt check itself
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"
97 (echo "$st"; cat "$tmp") > "$STATUS"