]> arthur.barton.de Git - nagcollect.git/blob - client/bin/nagcollect
8f07f3827fc7dbb02599ae555e352c9d4ffe58b8
[nagcollect.git] / client / bin / nagcollect
1 #!/bin/bash
2 #
3 # NagCollect -- Nagios Data Collector for Passive Checks
4 # Copyright (c)2009-2010 Alexander Barton, alex@barton.de
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 # Please read the file COPYING, README and AUTHORS for more information.
11 #
12
13 NAME=`basename "$0"`
14 MAXTIME=300
15
16 function Msg()
17 {
18         echo $*
19 }
20
21 function Error()
22 {
23         logger -t "$NAME" "$*"
24         echo $*
25         exit 1
26 }
27
28 function refreshTestScripts()
29 {
30         tmp=`mktemp /tmp/nagcollect.XXXXXX`
31         Msg "Downloading scripts from ${SERVER_URL} ..."
32         curl --insecure --fail --output "$tmp" --silent --max-time $MAXTIME \
33                 "${SERVER_URL}/nagcollecttestscripts.tgz" ; r=$?
34         if [ $r -eq 0 ]; then
35                 # Update local test scripts
36                 Msg "Extracting scripts ..."
37                 mkdir -p /usr/local/lib/nagcollect
38                 tar xzf "$tmp" -C /usr/local/lib/nagcollect -po \
39                  --exclude "._*" --exclude "." ; r=$?
40         fi
41         chmod -R a+rX /usr/local/lib/nagcollect
42         rm -f "$tmp"
43         return $r
44 }
45
46 function submitService()
47 {
48         curl --insecure --fail --output /dev/null --silent --max-time $MAXTIME \
49                 --data "key=$CLIENT_KEY" \
50                 --data "host=$CLIENT_ID" \
51                 --data "service=$1" \
52                 --data "status=$2" \
53                 --data "text=$3" \
54                 "${SERVER_URL}/nagcollect.php" ; r=$?
55         return $r
56 }
57
58 [ -r "/usr/local/etc/nagcollect.conf" ] && . /usr/local/etc/nagcollect.conf
59 [ -r "/etc/nagcollect.conf" ] && . /etc/nagcollect.conf
60
61 [ -n "$SERVER_URL" -a -n "$CLIENT_KEY" -a -n "$CLIENT_ID" ] || \
62         Error "Configuration invalid, check SERVER_URL, CLIENT_KEY and CLIENT_ID variables!"
63
64 if [ "$AUTOUPDATE" = 1 ]; then
65         Msg "Updating local test scripts:"
66         refreshTestScripts || \
67                 Error "Failed to refresh test scripts from \"$SERVER_URL\" ($?)!"
68 fi
69
70 Msg "Running test scripts:"
71
72 ls -1 \
73         /usr/local/lib/nagcollect/*.tst \
74         /usr/local/lib/nagcollect/`uname`/*.tst \
75  2>/dev/null | while read tst; do
76         [ -r "$tst" ] || continue
77         SERVICE=""; STATUS=""; TEXT=""
78         Msg "Checking \"$tst\" ..."
79         . "$tst"
80         [ -n "$SERVICE" -a -n "$STATUS" ] || continue
81         submitService "$SERVICE" "$STATUS" "$TEXT"
82         Msg "RESULT: $SERVICE=$STATUS \"$TEXT\" ($?)"
83 done
84
85 # -eof-