]>
git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/monitoring/plugins/send_nrdp.sh
5 # Copyright (c) 2010-2017 - Nagios Enterprises, LLC.
6 # Written by: Scott Wilkerson (nagios@nagios.org)
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
16 PROGNAME
=$(basename $0)
17 RELEASE
="Revision 0.6.1"
25 echo "$PROGNAME $RELEASE - Send NRDP script for Nagios"
27 echo "Usage: send_nrdp.sh -u URL -t token [options]"
29 echo "Usage: $PROGNAME -h display help"
36 echo "This script is used to send NRDP data to a Nagios server"
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"
45 echo " -s service name"
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"
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 " ##################################################"
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 " ##################################################"
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."
94 pdata
="token=$token&cmd=submitcheck"
96 fdata
="--data-urlencode XMLDATA@$file"
97 rslt
=`curl -f --silent --insecure -d "$pdata" $fdata "$url/"`
99 pdata
="$pdata&XMLDATA=$1"
100 rslt
=`curl -f --silent --insecure -d "$pdata" "$url/"`
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"
114 # This is where we write to the tmp directory
115 echo $xml > `mktemp $directory/nrdp.XXXXXX`
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"
128 # This is where we write to the tmp directory
129 echo $xml > `mktemp $directory/nrdp.XXXXXX`
135 # If this was a directory call and was successful, remove the file
136 if [ $2 ] && [ "$status" == "0" ];then
140 # If we weren't successful error
141 if [ $ret != 0 ];then
142 echo "exited with error "$ret
147 while getopts "u:t:H:s:S:o:f:d:c:D:hv" option
153 s
) service
=$OPTARG ;;
158 c
) checktype
=$OPTARG ;;
159 D
) directory
=$OPTARG ;;
166 if [ ! $checktype ]; then
169 if [ ! $delim ]; then
173 if [ "x$url" == "x" -o "x$token" == "x" ]
175 echo "Usage: send_nrdp -u url -t token"
179 if [[ `which curl` =~
"/curl" ]]
185 echo "Either curl or wget are required to run $PROGNAME"
193 # we are not getting piped results
194 if [ "$host" == "" ] || [ "$State" == "" ]; then
195 echo "You must provide a host -H and State -S"
198 if [ "$service" != "" ]; then
199 xml
="$xml<checkresult type='service' checktype='$checktype'><servicename>$service</servicename>"
201 xml
="$xml<checkresult type='host' checktype='$checktype'>"
204 # urlencode XML special chars
205 output
=${output//&/%26}
206 output
=${output//</%3C}
207 output
=${output//>/%3E}
209 xml
="$xml<hostname>$host</hostname><state>$State</state><output><![CDATA["$output"]]></output></checkresult>"
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
217 # we know we are being piped results
220 while read -r line
; do
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[@]}
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>"
233 xml
="$xml<checkresult type='host' checktype='$checktype'>
234 <hostname>${arr[0]}</hostname>
235 <state>${arr[1]}</state>
236 <output>${arr[2]}</output>"
239 xml
="$xml</checkresult>"
240 checkcount
=$
[checkcount
+1]
252 if [ $directory ]; then
253 #echo "Processing directory..."
254 for f
in `ls $directory`
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"
263 if [ "x$file" == "x" ] && [ "x$directory" == "x" ]; then
264 xml
="<?xml version='1.0'?><checkresults>$xml</checkresults>"
266 echo "Sent $checkcount checks to $url"