]>
git.immae.eu Git - perso/Immae/Projets/Puppet.git/blob - modules/profile/templates/monitoring/send_nrdp.sh.erb
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
148 url
="<%= @naemon_url %>"
149 token
="<%= @naemon_token %>"
151 while getopts "u:t:H:s:S:o:f:d:c:D:hv" option
157 s
) service
=$OPTARG ;;
162 c
) checktype
=$OPTARG ;;
163 D
) directory
=$OPTARG ;;
170 if [ ! $checktype ]; then
173 if [ ! $delim ]; then
177 if [ "x$url" == "x" -o "x$token" == "x" ]
179 echo "Usage: send_nrdp -u url -t token"
183 if [[ `which curl` =~
"/curl" ]]
189 echo "Either curl or wget are required to run $PROGNAME"
197 # we are not getting piped results
198 if [ "$host" == "" ] || [ "$State" == "" ]; then
199 echo "You must provide a host -H and State -S"
202 if [ "$service" != "" ]; then
203 xml
="$xml<checkresult type='service' checktype='$checktype'><servicename>$service</servicename>"
205 xml
="$xml<checkresult type='host' checktype='$checktype'>"
208 # urlencode XML special chars
209 output
=${output//&/%26}
210 output
=${output//</%3C}
211 output
=${output//>/%3E}
213 xml
="$xml<hostname>$host</hostname><state>$State</state><output><![CDATA["$output"]]></output></checkresult>"
217 # If only url and token have been provided then it is assumed that data is being piped
218 ########################
219 if [[ ! $host && ! $State && ! $file && ! $directory ]]; then
221 # we know we are being piped results
224 while read -r line
; do
226 if [ ${#arr[@]} != 0 ];then
227 if [[ ${#arr[@]} < 3 ]] || [[ ${#arr[@]} > 4 ]];then
228 echo "ERROR: STDIN must be either 3 or 4 fields long, I found "${#arr[@]}
230 if [ ${#arr[@]} == 4 ]; then
231 xml
="$xml<checkresult type='service' checktype='$checktype'>
232 <servicename>${arr[1]}</servicename>
233 <hostname>${arr[0]}</hostname>
234 <state>${arr[2]}</state>
235 <output>${arr[3]}</output>"
237 xml
="$xml<checkresult type='host' checktype='$checktype'>
238 <hostname>${arr[0]}</hostname>
239 <state>${arr[1]}</state>
240 <output>${arr[2]}</output>"
243 xml
="$xml</checkresult>"
244 checkcount
=$
[checkcount
+1]
256 if [ $directory ]; then
257 #echo "Processing directory..."
258 for f
in `ls $directory`
260 #echo "Processing $f file..."
261 # take action on each file. $f store current file name
262 xml
=`cat $directory/$f`
263 send_data
"$xml" "$directory/$f"
267 if [ "x$file" == "x" ] && [ "x$directory" == "x" ]; then
268 xml
="<?xml version='1.0'?><checkresults>$xml</checkresults>"
270 echo "Sent $checkcount checks to $url"