#!/bin/sh

COLUMN=upd
COLOR=green

REPORTING_HELPERS="$(dirname $0)/reporting-helpers"
if [ -f "$REPORTING_HELPERS" ]; then
    . "$REPORTING_HELPERS"
else
    echo "E: $REPORTING_HELPERS helpers not present, bailing out." 1>&2
    exit 1;
fi

#
# check for remaining upgrades
#
REMAINING_UPGRADES="$(remaining_upgrades)"
if [ -n "$REMAINING_UPGRADES" ]; then
    COLOR=red
    MSG="${MSG}${REMAINING_UPGRADES}"
fi

#
# check for errors in log file
#
ERRORS_IN_LOGFILE=$(errors_in_logfile "$1")
if [ "$ERRORS_IN_LOGFILE" = "notfound" ]; then
    COLOR=red
    MSG="${MSG}

log file missing -- please investigate

"
else
    if [ "$ERRORS_IN_LOGFILE" = "warnings" ]; then
        COLOR=yellow
    elif [ "$ERRORS_IN_LOGFILE" = "errors" ]; then
        COLOR=red
    fi

    MSG="${MSG}
$(print_logfile_for_xymon "$1")

"
fi

BB=/bin/false
if [ -x /usr/lib/hobbit/client/bin/bb ]; then
    BB=/usr/lib/hobbit/client/bin/bb
fi
if [ -x /usr/lib/xymon/client/bin/xymon ]; then
    BB=/usr/lib/xymon/client/bin/xymon
fi

if [ -f /etc/default/hobbit-client ]; then
    . /etc/default/hobbit-client
fi
if [ -f /etc/default/xymon-client ]; then
    . /etc/default/xymon-client
fi

for server in $HOBBITSERVERS $XYMONSERVERS; do
    $BB $server "@" << EOF
status+36h $CLIENTHOSTNAME.$COLUMN $COLOR $(date)

${MSG}
EOF
done

exit 0
