]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/monitoring/plugins/send_nrdp.sh
Add monitoring modules via naemon
[perso/Immae/Config/Nix.git] / modules / private / monitoring / plugins / send_nrdp.sh
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
15
16 PROGNAME=$(basename $0)
17 RELEASE="Revision 0.6.1"
18
19 print_release() {
20 echo "$RELEASE"
21 }
22
23 print_usage() {
24 echo ""
25 echo "$PROGNAME $RELEASE - Send NRDP script for Nagios"
26 echo ""
27 echo "Usage: send_nrdp.sh -u URL -t token [options]"
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
149 case $option in
150 u) url=$OPTARG ;;
151 t) token=$OPTARG ;;
152 H) host=$OPTARG ;;
153 s) service=$OPTARG ;;
154 S) State=$OPTARG ;;
155 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
164 done
165
166 if [ ! $checktype ]; then
167 checktype=1
168 fi
169 if [ ! $delim ]; then
170 delim=`echo -e "\t"`
171 fi
172
173 if [ "x$url" == "x" -o "x$token" == "x" ]
174 then
175 echo "Usage: send_nrdp -u url -t token"
176 exit 1
177 fi
178 # detecting curl
179 if [[ `which curl` =~ "/curl" ]]
180 then curl=1;
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
191 if [ $host ]; then
192 xml=""
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
247 if [ $file ]; then
248 xml=`cat $file`
249 send_data "$xml"
250 fi
251
252 if [ $directory ]; then
253 #echo "Processing directory..."
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
263 if [ "x$file" == "x" ] && [ "x$directory" == "x" ]; then
264 xml="<?xml version='1.0'?><checkresults>$xml</checkresults>"
265 send_data "$xml"
266 echo "Sent $checkcount checks to $url"
267 fi