]> arthur.barton.de Git - nagcollect.git/blob - client/bin/nagcollect
NagCollect is licensed under the terms of the GNU General Public License
[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 --exclude "._*" --exclude "." ; r=$?
39         fi
40         rm -f "$tmp"
41         return $r
42 }
43
44 function submitService()
45 {
46         curl --insecure --fail --output /dev/null --silent --max-time $MAXTIME \
47                 --data "key=$CLIENT_KEY" \
48                 --data "host=$CLIENT_ID" \
49                 --data "service=$1" \
50                 --data "status=$2" \
51                 --data "text=$3" \
52                 "${SERVER_URL}/nagcollect.php" ; r=$?
53         return $r
54 }
55
56 [ -r "/usr/local/etc/nagcollect.conf" ] && . /usr/local/etc/nagcollect.conf
57 [ -r "/etc/nagcollect.conf" ] && . /etc/nagcollect.conf
58
59 [ -n "$SERVER_URL" -a -n "$CLIENT_KEY" -a -n "$CLIENT_ID" ] || \
60         Error "Configuration invalid, check SERVER_URL, CLIENT_KEY and CLIENT_ID variables!"
61
62 if [ "$AUTOUPDATE" = 1 ]; then
63         Msg "Updating local test scripts:"
64         refreshTestScripts || \
65                 Error "Failed to refresh test scripts from \"$SERVER_URL\" ($?)!"
66 fi
67
68 Msg "Running test scripts:"
69
70 ls -1 \
71         /usr/local/lib/nagcollect/*.tst \
72         /usr/local/lib/nagcollect/`uname`/*.tst \
73  2>/dev/null | while read tst; do
74         [ -r "$tst" ] || continue
75         SERVICE=""; STATUS=""; TEXT=""
76         Msg "Checking \"$tst\" ..."
77         . "$tst"
78         [ -n "$SERVICE" -a -n "$STATUS" ] || continue
79         submitService "$SERVICE" "$STATUS" "$TEXT"
80         Msg "RESULT: $SERVICE=$STATUS \"$TEXT\" ($?)"
81 done
82
83 # -eof-