#!/bin/bash
#
# NagCollect -- Nagios Data Collector for Passive Checks
-# Copyright (c)2009 Alexander Barton, alex@barton.de
+# Copyright (c)2009-2011 Alexander Barton, alex@barton.de
#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+# Please read the file COPYING, README and AUTHORS for more information.
+#
+
+NAME=`basename "$0"`
+MAXTIME=300
+
+function Msg()
+{
+ echo $*
+}
-if [ "$1" = "--help" -o "$1" = "-h" -o $# -lt 5 -o $# -gt 6 ]; then
- echo "Usage: $0 <server-url> <key> <host> <status> <service> [<text>]"
+function Error()
+{
+ logger -t "$NAME" "$*"
+ echo $*
exit 1
+}
+
+function refreshTestScripts()
+{
+ tmp=`mktemp /tmp/nagcollect.XXXXXX`
+ Msg "Downloading scripts from ${SERVER_URL} ..."
+ curl --insecure --fail --output "$tmp" --silent --max-time $MAXTIME \
+ "${SERVER_URL}/nagcollecttestscripts.tgz" ; r=$?
+ if [ $r -eq 0 ]; then
+ # Update local test scripts
+ Msg "Extracting scripts ..."
+ mkdir -p /usr/local/lib/nagcollect
+ tar xzf "$tmp" -C /usr/local/lib/nagcollect -po \
+ --exclude "._*" --exclude ".DS_Store" ; r=$?
+ fi
+ chmod -R a+rX /usr/local/lib/nagcollect
+ rm -f "$tmp"
+ return $r
+}
+
+function submitService()
+{
+ curl --insecure --fail --output /dev/null --silent --max-time $MAXTIME \
+ --data "key=$CLIENT_KEY" \
+ --data "host=$CLIENT_ID" \
+ --data "service=$1" \
+ --data "status=$2" \
+ --data "text=$3" \
+ "${SERVER_URL}/nagcollect.php" ; r=$?
+ return $r
+}
+
+[ -r "/usr/local/etc/nagcollect.conf" ] && . /usr/local/etc/nagcollect.conf
+[ -r "/etc/nagcollect.conf" ] && . /etc/nagcollect.conf
+
+[ -n "$SERVER_URL" -a -n "$CLIENT_KEY" -a -n "$CLIENT_ID" ] || \
+ Error "Configuration invalid, check SERVER_URL, CLIENT_KEY and CLIENT_ID variables!"
+
+if [ "$AUTOUPDATE" = 1 ]; then
+ Msg "Updating local test scripts:"
+ refreshTestScripts || \
+ Error "Failed to refresh test scripts from \"$SERVER_URL\" ($?)!"
fi
-curl --insecure --include \
- --data-urlencode "key=$2" \
- --data-urlencode "host=$3" \
- --data-urlencode "status=$4" \
- --data-urlencode "service=$5" \
- --data-urlencode "text=$6" \
- "$1"
+Msg "Running test scripts:"
+
+ls -1 \
+ /usr/local/lib/nagcollect/*.tst \
+ /usr/local/lib/nagcollect/`uname`/*.tst \
+ 2>/dev/null | while read tst; do
+ [ -r "$tst" ] || continue
+ SERVICE=""; STATUS=""; TEXT=""
+ Msg "Checking \"$tst\" ..."
+ . "$tst"
+ [ -n "$SERVICE" -a -n "$STATUS" ] || continue
+ submitService "$SERVICE" "$STATUS" "$TEXT"
+ Msg "RESULT: $SERVICE=$STATUS \"$TEXT\" ($?)"
+done
# -eof-