diff options
-rw-r--r-- | modules/private/monitoring/default.nix | 2 | ||||
-rwxr-xr-x | modules/private/monitoring/plugins/send_nrdp.sh | 288 |
2 files changed, 40 insertions, 250 deletions
diff --git a/modules/private/monitoring/default.nix b/modules/private/monitoring/default.nix index d9805ef..902b2b1 100644 --- a/modules/private/monitoring/default.nix +++ b/modules/private/monitoring/default.nix | |||
@@ -8,7 +8,7 @@ let | |||
8 | patchShebangs $out | 8 | patchShebangs $out |
9 | wrapProgram $out/check_command --prefix PATH : ${config.security.wrapperDir} | 9 | wrapProgram $out/check_command --prefix PATH : ${config.security.wrapperDir} |
10 | wrapProgram $out/send_nrdp.sh --prefix PATH : ${lib.makeBinPath [ | 10 | wrapProgram $out/send_nrdp.sh --prefix PATH : ${lib.makeBinPath [ |
11 | pkgs.curl pkgs.which pkgs.coreutils | 11 | pkgs.curl pkgs.jq |
12 | ]} | 12 | ]} |
13 | wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [ | 13 | wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [ |
14 | pkgs.gnugrep pkgs.gawk pkgs.procps-ng | 14 | pkgs.gnugrep pkgs.gawk pkgs.procps-ng |
diff --git a/modules/private/monitoring/plugins/send_nrdp.sh b/modules/private/monitoring/plugins/send_nrdp.sh index 27e47b4..c83c8cb 100755 --- a/modules/private/monitoring/plugins/send_nrdp.sh +++ b/modules/private/monitoring/plugins/send_nrdp.sh | |||
@@ -1,267 +1,57 @@ | |||
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | # | ||
3 | # check_nrdp.sh | ||
4 | # | ||
5 | # Copyright (c) 2010-2017 - Nagios Enterprises, LLC. | ||
6 | # Written by: Scott Wilkerson (nagios@nagios.org) | ||
7 | # | ||
8 | # 2017-09-25 Troy Lea aka BOX293 | ||
9 | # - Fixed script not working with arguments when run as a cron job | ||
10 | # or if being used as a nagios command like obsessive compulsive. | ||
11 | # ... "if [ ! -t 0 ]" was the reason why. | ||
12 | # 2017-12-08 Jørgen van der Meulen (Conclusion Xforce) | ||
13 | # - Fixed typo in NRDP abbreviation | ||
14 | 2 | ||
15 | 3 | TEMPLATE='{ | |
16 | PROGNAME=$(basename $0) | 4 | "cmd": "submitcheck", |
17 | RELEASE="Revision 0.6.1" | 5 | "token": $token, |
18 | 6 | "checkresult": [{ | |
19 | print_release() { | 7 | "hostname": $hostname, |
20 | echo "$RELEASE" | 8 | "state": $state, |
21 | } | 9 | "output": $output, |
22 | 10 | "type": $type, | |
23 | print_usage() { | 11 | "servicename": $servicename |
24 | echo "" | 12 | }] |
25 | echo "$PROGNAME $RELEASE - Send NRDP script for Nagios" | 13 | }' |
26 | echo "" | 14 | |
27 | echo "Usage: send_nrdp.sh -u URL -t token [options]" | 15 | while getopts "u:t:H:s:S:o:" option |
28 | echo "" | ||
29 | echo "Usage: $PROGNAME -h display help" | ||
30 | echo "" | ||
31 | } | ||
32 | |||
33 | print_help() { | ||
34 | print_usage | ||
35 | echo "" | ||
36 | echo "This script is used to send NRDP data to a Nagios server" | ||
37 | echo "" | ||
38 | echo "Required:" | ||
39 | echo " -u"," URL of NRDP server. Usually http://<IP_ADDRESS>/nrdp/" | ||
40 | echo " -t"," Shared token. Must be the same token set in NRDP Server" | ||
41 | echo "" | ||
42 | echo "Options:" | ||
43 | echo " Single Check:" | ||
44 | echo " -H host name" | ||
45 | echo " -s service name" | ||
46 | echo " -S State" | ||
47 | echo " -o output" | ||
48 | echo "" | ||
49 | echo " STDIN:" | ||
50 | echo " [-d delimiter] (default -d \"\\t\")" | ||
51 | echo " With only the required parameters $PROGNAME is capable of" | ||
52 | echo " processing data piped to it either from a file or other" | ||
53 | echo " process. By default, we use \t as the delimiter however this" | ||
54 | echo " may be specified with the -d option data should be in the" | ||
55 | echo " following formats one entry per line." | ||
56 | echo " For Host checks:" | ||
57 | echo " hostname State output" | ||
58 | echo " For Service checks" | ||
59 | echo " hostname servicename State output" | ||
60 | echo "" | ||
61 | echo " File:" | ||
62 | echo " -f /full/path/to/file" | ||
63 | echo " This file will be sent to the NRDP server specified in -u" | ||
64 | echo " The file should be an XML file in the following format" | ||
65 | echo " ##################################################" | ||
66 | echo "" | ||
67 | echo " <?xml version='1.0'?>" | ||
68 | echo " <checkresults>" | ||
69 | echo " <checkresult type=\"host\" checktype=\"1\">" | ||
70 | echo " <hostname>YOUR_HOSTNAME</hostname>" | ||
71 | echo " <state>0</state>" | ||
72 | echo " <output>OK|perfdata=1.00;5;10;0</output>" | ||
73 | echo " </checkresult>" | ||
74 | echo " <checkresult type=\"service\" checktype=\"1\">" | ||
75 | echo " <hostname>YOUR_HOSTNAME</hostname>" | ||
76 | echo " <servicename>YOUR_SERVICENAME</servicename>" | ||
77 | echo " <state>0</state>" | ||
78 | echo " <output>OK|perfdata=1.00;5;10;0</output>" | ||
79 | echo " </checkresult>" | ||
80 | echo " </checkresults>" | ||
81 | echo " ##################################################" | ||
82 | echo "" | ||
83 | echo " Directory:" | ||
84 | echo " -D /path/to/temp/dir" | ||
85 | echo " This is a directory that contains XML files in the format" | ||
86 | echo " above. Additionally, if the -d flag is specified, $PROGNAME" | ||
87 | echo " will create temp files here if the server could not be reached." | ||
88 | echo " On additional calls with the same -D path, if a connection to" | ||
89 | echo " the server is successful, all temp files will be sent." | ||
90 | exit 0 | ||
91 | } | ||
92 | |||
93 | send_data() { | ||
94 | pdata="token=$token&cmd=submitcheck" | ||
95 | if [ $file ]; then | ||
96 | fdata="--data-urlencode XMLDATA@$file" | ||
97 | rslt=`curl -f --silent --insecure -d "$pdata" $fdata "$url/"` | ||
98 | else | ||
99 | pdata="$pdata&XMLDATA=$1" | ||
100 | rslt=`curl -f --silent --insecure -d "$pdata" "$url/"` | ||
101 | fi | ||
102 | |||
103 | ret=$? | ||
104 | |||
105 | status=`echo $rslt | sed -n 's|.*<status>\(.*\)</status>.*|\1|p'` | ||
106 | message=`echo $rslt | sed -n 's|.*<message>\(.*\)</message>.*|\1|p'` | ||
107 | if [ $ret != 0 ];then | ||
108 | echo "ERROR: could not connect to NRDP server at $url" | ||
109 | # verify we are not processing the directory already and then write to the directory | ||
110 | if [ ! "$2" ] && [ $directory ];then | ||
111 | if [ ! -d "$directory" ];then | ||
112 | mkdir -p "$directory" | ||
113 | fi | ||
114 | # This is where we write to the tmp directory | ||
115 | echo $xml > `mktemp $directory/nrdp.XXXXXX` | ||
116 | fi | ||
117 | exit 1 | ||
118 | fi | ||
119 | |||
120 | if [ "$status" != "0" ];then | ||
121 | # This means we couldn't connect to NRPD server | ||
122 | echo "ERROR: The NRDP Server said $message" | ||
123 | # verify we are not processing the directory already and then write to the directory | ||
124 | if [ ! "$2" ] && [ $directory ];then | ||
125 | if [ ! -d "$directory" ];then | ||
126 | mkdir -p "$directory" | ||
127 | fi | ||
128 | # This is where we write to the tmp directory | ||
129 | echo $xml > `mktemp $directory/nrdp.XXXXXX` | ||
130 | fi | ||
131 | |||
132 | exit 2 | ||
133 | fi | ||
134 | |||
135 | # If this was a directory call and was successful, remove the file | ||
136 | if [ $2 ] && [ "$status" == "0" ];then | ||
137 | rm -f "$2" | ||
138 | fi | ||
139 | |||
140 | # If we weren't successful error | ||
141 | if [ $ret != 0 ];then | ||
142 | echo "exited with error "$ret | ||
143 | exit $ret | ||
144 | fi | ||
145 | } | ||
146 | |||
147 | while getopts "u:t:H:s:S:o:f:d:c:D:hv" option | ||
148 | do | 16 | do |
149 | case $option in | 17 | case $option in |
150 | u) url=$OPTARG ;; | 18 | u) url=$OPTARG ;; |
151 | t) token=$OPTARG ;; | 19 | t) token=$OPTARG ;; |
152 | H) host=$OPTARG ;; | 20 | H) hostname=$OPTARG ;; |
153 | s) service=$OPTARG ;; | 21 | s) servicename=$OPTARG ;; |
154 | S) State=$OPTARG ;; | 22 | S) state=$OPTARG ;; |
155 | o) output=$OPTARG ;; | 23 | o) output=$OPTARG ;; |
156 | f) file=$OPTARG ;; | ||
157 | d) delim=$OPTARG ;; | ||
158 | c) checktype=$OPTARG ;; | ||
159 | D) directory=$OPTARG ;; | ||
160 | h) print_help 0;; | ||
161 | v) print_release | ||
162 | exit 0 ;; | ||
163 | esac | 24 | esac |
164 | done | 25 | done |
165 | 26 | ||
166 | if [ ! $checktype ]; then | 27 | if [ -n "$servicename" ]; then |
167 | checktype=1 | 28 | checktype="service" |
168 | fi | 29 | else |
169 | if [ ! $delim ]; then | 30 | checktype="host" |
170 | delim=`echo -e "\t"` | ||
171 | fi | 31 | fi |
172 | 32 | ||
173 | if [ "x$url" == "x" -o "x$token" == "x" ] | 33 | payload=$(jq -n \ |
174 | then | 34 | --arg type "$checktype" \ |
175 | echo "Usage: send_nrdp -u url -t token" | 35 | --arg hostname "$hostname" \ |
176 | exit 1 | 36 | --arg servicename "$servicename" \ |
177 | fi | 37 | --arg output "$output" \ |
178 | # detecting curl | 38 | --arg token "$token" \ |
179 | if [[ `which curl` =~ "/curl" ]] | 39 | --arg state "$state" \ |
180 | then curl=1; | 40 | "$TEMPLATE") |
181 | fi | ||
182 | |||
183 | if [[ ! $curl ]]; | ||
184 | then | ||
185 | echo "Either curl or wget are required to run $PROGNAME" | ||
186 | exit 1 | ||
187 | fi | ||
188 | |||
189 | checkcount=0 | ||
190 | 41 | ||
191 | if [ $host ]; then | 42 | rslt=$(curl -f --silent --insecure -d "$payload" -H "Content-Type: application/json" "$url") |
192 | xml="" | 43 | ret=$? |
193 | # we are not getting piped results | ||
194 | if [ "$host" == "" ] || [ "$State" == "" ]; then | ||
195 | echo "You must provide a host -H and State -S" | ||
196 | exit 2 | ||
197 | fi | ||
198 | if [ "$service" != "" ]; then | ||
199 | xml="$xml<checkresult type='service' checktype='$checktype'><servicename>$service</servicename>" | ||
200 | else | ||
201 | xml="$xml<checkresult type='host' checktype='$checktype'>" | ||
202 | fi | ||
203 | |||
204 | # urlencode XML special chars | ||
205 | output=${output//&/%26} | ||
206 | output=${output//</%3C} | ||
207 | output=${output//>/%3E} | ||
208 | |||
209 | xml="$xml<hostname>$host</hostname><state>$State</state><output><![CDATA["$output"]]></output></checkresult>" | ||
210 | checkcount=1 | ||
211 | fi | ||
212 | |||
213 | # If only url and token have been provided then it is assumed that data is being piped | ||
214 | ######################## | ||
215 | if [[ ! $host && ! $State && ! $file && ! $directory ]]; then | ||
216 | xml="" | ||
217 | # we know we are being piped results | ||
218 | IFS=$delim | ||
219 | |||
220 | while read -r line ; do | ||
221 | arr=($line) | ||
222 | if [ ${#arr[@]} != 0 ];then | ||
223 | if [[ ${#arr[@]} < 3 ]] || [[ ${#arr[@]} > 4 ]];then | ||
224 | echo "ERROR: STDIN must be either 3 or 4 fields long, I found "${#arr[@]} | ||
225 | else | ||
226 | if [ ${#arr[@]} == 4 ]; then | ||
227 | xml="$xml<checkresult type='service' checktype='$checktype'> | ||
228 | <servicename>${arr[1]}</servicename> | ||
229 | <hostname>${arr[0]}</hostname> | ||
230 | <state>${arr[2]}</state> | ||
231 | <output>${arr[3]}</output>" | ||
232 | else | ||
233 | xml="$xml<checkresult type='host' checktype='$checktype'> | ||
234 | <hostname>${arr[0]}</hostname> | ||
235 | <state>${arr[1]}</state> | ||
236 | <output>${arr[2]}</output>" | ||
237 | fi | ||
238 | |||
239 | xml="$xml</checkresult>" | ||
240 | checkcount=$[checkcount+1] | ||
241 | fi | ||
242 | fi | ||
243 | done | ||
244 | IFS=" " | ||
245 | fi | ||
246 | 44 | ||
247 | if [ $file ]; then | 45 | if [ $ret != 0 ];then |
248 | xml=`cat $file` | 46 | echo "ERROR: could not connect to NRDP server at $url" |
249 | send_data "$xml" | 47 | exit 1 |
250 | fi | 48 | fi |
251 | 49 | ||
252 | if [ $directory ]; then | 50 | status=$(echo "$rslt" | jq -r .status) |
253 | #echo "Processing directory..." | 51 | message=$(echo "$rslt" | jq -r .message) |
254 | for f in `ls $directory` | ||
255 | do | ||
256 | #echo "Processing $f file..." | ||
257 | # take action on each file. $f store current file name | ||
258 | xml=`cat $directory/$f` | ||
259 | send_data "$xml" "$directory/$f" | ||
260 | done | ||
261 | fi | ||
262 | 52 | ||
263 | if [ "x$file" == "x" ] && [ "x$directory" == "x" ]; then | 53 | if [ "$status" != "ok" ];then |
264 | xml="<?xml version='1.0'?><checkresults>$xml</checkresults>" | 54 | echo "ERROR: The NRDP Server said $message" |
265 | send_data "$xml" | 55 | exit 2 |
266 | echo "Sent $checkcount checks to $url" | ||
267 | fi | 56 | fi |
57 | echo "Sent 1 checks to $url" | ||