Sending Data Between Nagios Servers

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Sending Data Between Nagios Servers

Post by optionstechnology »

no both of mine are set to nrdp.sh

I'm literally cut&pasting the line from the nagios command to the command line

it is identical
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Sending Data Between Nagios Servers

Post by scottwilkerson »

Can you PM me a copy of your /usr/local/nrdp/clients/send_nrdp.sh

I just tested on my system with this and the <br> was included in the output

Code: Select all

/usr/local/nrdp/clients/send_nrdp.sh -u http://NAGIOSXI/nrdp/ -t TOKEN -H HOSTNAME -s "Port - WAN Test" -S ok -o "This is a <br>new test"
You don't also have outbound transfers set on this XI server do you?
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Sending Data Between Nagios Servers

Post by optionstechnology »

Did you test from the command line or from the front end?

Outbound transfers are turned on on this box - but sending the info to a different server - I've turned off the NRDP settings in outbound settings now

This is my send_nrdp.sh-

Code: Select all

#!/bin/bash
#
# check_nrdp.sh
#
# Copyright (c) 2010-2017 - Nagios Enterprises, LLC.
# Written by: Scott Wilkerson ([email protected])
#
# 2017-09-25 Troy Lea aka BOX293
#  - Fixed script not working with arguments when run as a cron job
#    or if being used as a nagios command like obsessive compulsive.
#     ... "if [ ! -t 0 ]" was the reason why.
# 2017-12-08 Jørgen van der Meulen (Conclusion Xforce)
#  - Fixed typo in NRDP abbreviation


PROGNAME=$(basename $0)
RELEASE="Revision 0.6.1"

print_release() {
    echo "$RELEASE"
}

print_usage() {
    echo ""
    echo "$PROGNAME $RELEASE - Send NRDP script for Nagios"
    echo ""
    echo "Usage: send_nrdp.sh -u URL -t token [options]"
    echo ""
    echo "Usage: $PROGNAME -h display help"
    echo ""
}

print_help() {
        print_usage
        echo ""
        echo "This script is used to send NRDP data to a Nagios server"
        echo ""
        echo "Required:"
        echo "    -u","    URL of NRDP server.  Usually http://<IP_ADDRESS>/nrdp/"
        echo "    -t","    Shared token.  Must be the same token set in NRDP Server"
        echo ""
        echo "Options:"
        echo "    Single Check:"
        echo "        -H    host name"
        echo "        -s    service name"
        echo "        -S    State"
        echo "        -o     output"
        echo ""
        echo "    STDIN:"
        echo "        [-d    delimiter] (default -d \"\\t\")"
        echo "        With only the required parameters $PROGNAME is capable of"
        echo "        processing data piped to it either from a file or other"
        echo "        process.  By default, we use \t as the delimiter however this"
        echo "        may be specified with the -d option data should be in the"
        echo "        following formats one entry per line."
        echo "        For Host checks:"
        echo "        hostname    State    output"
        echo "        For Service checks"
        echo "        hostname    servicename    State    output"
        echo ""
        echo "    File:"
        echo "        -f /full/path/to/file"
        echo "        This file will be sent to the NRDP server specified in -u"
        echo "        The file should be an XML file in the following format"
        echo "        ##################################################"
        echo ""
        echo "        <?xml version='1.0'?>"
        echo "        <checkresults>"
        echo "          <checkresult type=\"host\" checktype=\"1\">"
        echo "            <hostname>YOUR_HOSTNAME</hostname>"
        echo "            <state>0</state>"
        echo "            <output>OK|perfdata=1.00;5;10;0</output>"
        echo "          </checkresult>"
        echo "          <checkresult type=\"service\" checktype=\"1\">"
        echo "            <hostname>YOUR_HOSTNAME</hostname>"
        echo "            <servicename>YOUR_SERVICENAME</servicename>"
        echo "            <state>0</state>"
        echo "            <output>OK|perfdata=1.00;5;10;0</output>"
        echo "          </checkresult>"
        echo "        </checkresults>"
        echo "        ##################################################"
        echo ""
        echo "    Directory:"
        echo "        -D /path/to/temp/dir"
        echo "        This is a directory that contains XML files in the format"
        echo "        above.  Additionally, if the -d flag is specified, $PROGNAME"
        echo "        will create temp files here if the server could not be reached."
        echo "        On additional calls with the same -D path, if a connection to"
        echo "        the server is successful, all temp files will be sent."
        exit 0
}

send_data() {
    pdata="token=$token&cmd=submitcheck"
    if [ ! "x$curl" == "x" ];then

        if [ $file ]; then
            fdata="--data-urlencode XMLDATA@$file"
            rslt=`curl -f --silent --insecure -d "$pdata" $fdata "$url/"`
        else
            pdata="$pdata&XMLDATA=$1"
            rslt=`curl -f --silent --insecure -d "$pdata" "$url/"`
        fi
        
        ret=$?
    else
        pdata="$pdata&XMLDATA=$1"
        rslt=`wget -q -O - --post-data="$pdata" "$url/"`
        ret=$?
    fi

    status=`echo $rslt | sed -n 's|.*<status>\(.*\)</status>.*|\1|p'`
    message=`echo $rslt | sed -n 's|.*<message>\(.*\)</message>.*|\1|p'`
    if [ $ret != 0 ];then
        echo "ERROR: could not connect to NRDP server at $url"
        # verify we are not processing the directory already and then write to the directory
        if [ ! "$2" ] && [ $directory ];then
            if [ ! -d "$directory" ];then
                mkdir -p "$directory"
            fi
            # This is where we write to the tmp directory
            echo $xml > `mktemp $directory/nrdp.XXXXXX`
        fi
        exit 1
    fi
    
    if [ "$status" != "0" ];then
        # This means we couldn't connect to NRPD server
        echo "ERROR: The NRDP Server said $message"
        # verify we are not processing the directory already and then write to the directory
        if [ ! "$2" ] && [ $directory ];then
            if [ ! -d "$directory" ];then
                mkdir -p "$directory"
            fi
            # This is where we write to the tmp directory
            echo $xml > `mktemp $directory/nrdp.XXXXXX`
        fi
        
        exit 2
    fi
    
    # If this was a directory call and was successful, remove the file
    if [ $2 ] && [ "$status" == "0" ];then
        rm -f "$2"
    fi

    # If we weren't successful error
    if [ $ret != 0 ];then
        echo "exited with error "$ret
        exit $ret
    fi
}

# Parse parameters

while getopts "u:t:H:s:S:o:f:d:c:D:hv" option
do
  case $option in
    u) url=$OPTARG ;;
    t) token=$OPTARG ;;
    H) host=$OPTARG ;;
    s) service=$OPTARG ;;
    S) State=$OPTARG ;;
    o) output=$OPTARG ;;
    f) file=$OPTARG ;;
    d) delim=$OPTARG ;;
    c) checktype=$OPTARG ;;
    D) directory=$OPTARG ;;
    h) print_help 0;;
    v) print_release
        exit 0 ;;
  esac
done

if [ ! $checktype ]; then
 checktype=1
fi
if [ ! $delim ]; then
 delim=`echo -e "\t"`
fi

if [ "x$url" == "x" -o "x$token" == "x" ]
then
  echo "Usage: send_nrdp -u url -t token"
  exit 1
fi
# detecting curl 
if [[ `which curl` =~ "/curl" ]]
 then curl=1; 
fi
# detecting wget if we don't have curl
if [[ `which wget` =~ "/wget" ]]
then
    wget=1;
fi

if [[ ! $curl && ! $wget ]];
then
  echo "Either curl or wget are required to run $PROGNAME"
  exit 1
fi

checkcount=0

if [ $host ]; then
    xml=""
    # we are not getting piped results
    if [ "$host" == "" ] || [ "$State" == "" ]; then
        echo "You must provide a host -H and State -S"
        exit 2
    fi
    if [ "$service" != "" ]; then
        xml="$xml<checkresult type='service' checktype='$checktype'><servicename>$service</servicename>"
    else
        xml="$xml<checkresult type='host' checktype='$checktype'>"
    fi
    
    # urlencode XML special chars
    output=${output//&/%26}
    output=${output//</%3C}
    output=${output//>/%3E}
    
    xml="$xml<hostname>$host</hostname><state>$State</state><output><![CDATA["$output"]]></output></checkresult>"
    checkcount=1
fi

 # If only url and token have been provided then it is assumed that data is being piped
########################
if [[ ! $host && ! $State && ! $file && ! $directory ]]; then
    xml=""
    # we know we are being piped results
    IFS=$delim
    
    while read -r line ; do
        arr=($line)
        if [ ${#arr[@]} != 0 ];then
            if [[ ${#arr[@]} < 3 ]] || [[ ${#arr[@]} > 4 ]];then
                echo "ERROR: STDIN must be either 3 or 4 fields long, I found "${#arr[@]}
            else
                if [ ${#arr[@]} == 4 ]; then
                    xml="$xml<checkresult type='service' checktype='$checktype'>
                    <servicename>${arr[1]}</servicename>
                    <hostname>${arr[0]}</hostname>
                    <state>${arr[2]}</state>
                    <output>${arr[3]}</output>"
                else
                    xml="$xml<checkresult type='host' checktype='$checktype'>
                    <hostname>${arr[0]}</hostname>
                    <state>${arr[1]}</state>
                    <output>${arr[2]}</output>"
                fi
                
                xml="$xml</checkresult>"
                checkcount=$[checkcount+1]
            fi
        fi
    done
    IFS=" "
fi

if [ $file ]; then
    xml=`cat $file`
    send_data "$xml"
fi

if [ $directory ]; then
    #echo "Processing directory..."
    for f in `ls $directory`
    do
      #echo "Processing $f file..."
      # take action on each file. $f store current file name
      xml=`cat $directory/$f`
      send_data "$xml" "$directory/$f"
    done
fi

if [ "x$file" == "x" ] && [ "x$directory" == "x" ]; then
    xml="<?xml version='1.0'?><checkresults>$xml</checkresults>"
    send_data "$xml"
    echo "Sent $checkcount checks to $url"
fi
Last edited by tmcdonald on Tue Jun 12, 2018 11:23 am, edited 1 time in total.
Reason: Please use [code][/code] tags around code
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Sending Data Between Nagios Servers

Post by scottwilkerson »

I tested from the command line, exact command as follows

Code: Select all

/usr/local/nrdp/clients/send_nrdp.sh -u http://NAGIOSXI/nrdp/ -t TOKEN -H HOSTNAME -s "Port - WAN Test" -S ok -o "This is a <br>new test"
worked perfectly with this command, even using the plugin you posted
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Sending Data Between Nagios Servers

Post by optionstechnology »

Yeah it works fine from command line - it just doesnt work when Nagios does it though the front end
optionstechnology wrote:Just one more thing (Always something!)

I use HTML <BR>'s in my status output

When i run this from command line it sends the characters over fine

When I run it from Nagios it strips out the '<' '>' characters....


How do I tell it not to do that?
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Sending Data Between Nagios Servers

Post by scottwilkerson »

I don't know what you mean here
optionstechnology wrote:it just doesnt work when Nagios does it though the front end
?

Do you mean if you do it through the web page /nrdp/ ? If so, that 's is because < & > are special chars and not valid XML and need to be encoded.
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Sending Data Between Nagios Servers

Post by optionstechnology »

I mean when Nagios runs it as a command it strips out the < and >
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Sending Data Between Nagios Servers

Post by scottwilkerson »

How do you have it configured to run as a command?
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Sending Data Between Nagios Servers

Post by optionstechnology »

I have set the following in nagios.cfg -

obsess_over_hosts=1
obsess_over_services=1
ochp_command=send_nrdp_host
ocsp_command=send_nrdp_service

This is the host command -
send_nrdp_host

/usr/local/nrdp/clients/send_nrdp.sh -u https://$_HOSTCLIENT_NAGIOS_SERVER$/nrdp -t $_HOSTTOKEN$ -H $HOSTNAME$ -S $HOSTSTATEID$ -o "$HOSTOUTPUT$"
This is the service command -
send_nrdp_service

/usr/local/nrdp/clients/send_nrdp.sh -u https://$_HOSTCLIENT_NAGIOS_SERVER$/nrdp -t $_HOSTTOKEN$ -H $HOSTNAME$ -s "$SERVICEDESC$" -S $SERVICESTATEID$ -o "$SERVICEOUTPUT$"
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Sending Data Between Nagios Servers

Post by scottwilkerson »

Oh, I see what's going on. These are marked as illegal_macro_output_chars in the nagios.cfg, primarily for safety because it could easily cause redirection on the command line if they weren't quoted or escaped properly.

You can remove them from that variable but I would like to stress that I highly recommend doing this in a testing environment first to make sure there are not other items affected.

To test, in nagios.cfg change this

Code: Select all

illegal_macro_output_chars=`~$&|'"<>
to this

Code: Select all

illegal_macro_output_chars=`~$&|'"
Then restart nagios

Code: Select all

service nagios restart
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
Locked