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