diff options
Diffstat (limited to 'check-hosts')
-rwxr-xr-x | check-hosts | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/check-hosts b/check-hosts new file mode 100755 index 0000000..bcdddd8 --- /dev/null +++ b/check-hosts @@ -0,0 +1,127 @@ +#!/bin/bash + +STALE_DIR="${HOME}/var/stale/" + +get_names() { + bcfg2-reports "$@" --fields=name | sed 's/ *$//' +} + +get_extra() { + get_names | while read i ; do test -z "$(bcfg2-reports -e $i)" || echo $i ; done +} + +get_stale() { + get_names --stale | sed 's/ *$//' +} + +get_all() { + get_names -a | sed 's/ *$//' +} + +get_bad() { + get_names -d | sed 's/ *$//' +} + +get_clean() { + get_names -c | sed 's/ *$//' +} + +get_count() { + clean=$(get_clean | wc -l) + bad=$(get_bad | wc -l) + extra=$(get_extra | wc -l) + stale=$(get_stale | wc -l) + + echo "clean=$clean bad=$bad extra=$extra stale=$stale" +} + +get_stale_once() { + get_stale | while read host; do + STALE_FILE="${STALE_DIR}/${host}" + test -f "${STALE_FILE}" || ( + touch "${STALE_FILE}" + echo $host + ) + done + + comm -13 <(get_stale | sort) <(get_all | sort) | while read host; do + STALE_FILE="${STALE_DIR}/${host}" + test -f "${STALE_FILE}" && rm -f "${STALE_FILE}" + done +} + +cron() { + stale=$(get_stale_once) + if [ -n "$stale" ]; then + sendmail -t <<EOM +To: bcfg2-admins@spline.inf.fu-berlin.de +From: cron@bcfg2.spline.inf.fu-berlin.de +Subject: New stale hosts + +Hallo, + +die folgenden Hosts haben sich seit 24h nicht mehr bei bcfg2 gemeldet: + +$(echo "$stale" | sed 's/^/ /') + +Gruß, +bcfg2 +EOM + fi +} + +show_usage() { + cat <<EOU +Usage: + $0 [mode] + +mode should be one of the following: + -a|--all display all hosts + -b|--bad display bad hosts + -s|--stale display hosts, that haven't run in the last 24 hours + -e|--extra display hosts with extra entries + + -c|--cron run in cron mode (report new stale hosts) + -n|--nagios run in nagios mode (report count hosts in groups) +EOU +} + +optarr=( $(getopt -o 'absecn' --long 'all,bad,stale,extra,cron,nagios' -- "$@") ) + +i=0 +while true; do + case ${optarr[$i]} in + -a|--all) + get_all + exit 0 + ;; + -b|--bad) + get_bad + exit 0 + ;; + -s|--stale) + get_stale + exit 0 + ;; + -e|--extra) + get_extra + exit 0 + ;; + + -c|--cron) + cron + exit 0 + ;; + -n|--nagios) + get_count + exit 0 + ;; + *) + show_usage + exit 1 + ;; + esac + ((i++)) +done + +exit 0 |