1 #! /bin/bash |
|
2 # © 2011 Heiko Schlittermann <hs@schlittermann.de> |
|
3 # source: https://ssl.schlittermann.de/hg/check-by-nsca |
|
4 |
|
5 set -e |
|
6 |
|
7 unset ${!LC_*} LANG |
|
8 export LC_ALL=C |
|
9 ME=$(basename $0) |
|
10 TAB=$'\x09' |
|
11 |
|
12 function send() { send_nsca "$@"; } |
|
13 function debug_send() { echo "CMDLINE: $@"; sed 's/\t/<<TAB>>/g'; } |
|
14 send=send # changed by option |
|
15 |
|
16 |
|
17 # read defaults, if exist |
|
18 test -f /etc/defaults/$ME \ |
|
19 && source /etc/defaults/$ME |
|
20 |
|
21 hostname=$(hostname -f) |
|
22 svcname=- |
|
23 nsca_host= |
|
24 nsca_port= |
|
25 |
|
26 tmp=$(getopt -n $0 -o hH:S:p:d \ |
|
27 -l help,hostname:,servicename:,nsca-host:,nsca-port:,debug \ |
|
28 -- "$@") || exit 2 |
|
29 eval set -- "$tmp" |
|
30 |
|
31 while true; do |
|
32 opt="$1"; shift |
|
33 case "$opt" in |
|
34 -H|--hostname) hostname="$1"; shift;; |
|
35 -S|--svcname) svcname="$1"; shift;; |
|
36 --nsca-host) nsca_host="$1"; shift;; |
|
37 -p|--nsca-port) nsca_port="$1"; shift;; |
|
38 -h|--help) cat <<_USAGE |
|
39 Usage: $ME [options] -- {test} [test-options] |
|
40 -H|--hostname Hostname to be sent along the result ($hostname). |
|
41 -S|--servicename Servicename to be sent along the result. |
|
42 Use "" to mark a host check. |
|
43 Use "-" (the default) to have the servicename guessed |
|
44 from the test output (the first word). |
|
45 --nsca-host Destination host ($nsca_host) |
|
46 --nsca-port Destination port ($nsca_port) |
|
47 _USAGE |
|
48 exit 0;; |
|
49 -d|--debug) send="debug_$send";; |
|
50 --) break;; |
|
51 esac |
|
52 |
|
53 done |
|
54 |
|
55 rc=0 |
|
56 output=$("$@") || rc=$? |
|
57 |
|
58 test "$svcname" = "-" && svcname="${output%% *}" |
|
59 svcname="${svcname:+$TAB$svcname}" |
|
60 |
|
61 echo "$hostname$svcname$TAB$rc$TAB$output" \ |
|
62 | $send -H "$nsca_host" ${nsca_port:+-p $nsca_port} |
|
63 |
|
64 # vim:sts=4 sw=4 aw ai sm et: |
|