Forum Discussion

gof5go_289559's avatar
gof5go_289559
Icon for Nimbostratus rankNimbostratus
Apr 27, 2017

Health monitor to parse Nagios XI REST API call

I am needing to check Linux service status (crond) to determine if a pool member is healthy.

Initially, I started down the path to use an external monitor to run a perl script to do this, but have a bit to set up to allow the f5 to SSH with keys to all of our hosts.

So then I found that Nagios XI (for which there is already and agent set up on all of our hosts) has a REST API. The results returned with a curl request contain many attributes all in one line. For example:

> curl -k --silent "https://nagiosserver.local/nagiosxi/api/v1/objects/servicestatus?host_name=host5&service_description=crond&apikey=[api-key-here]

{"servicestatuslist":{"recordcount":"1","servicestatus":{"@attributes":{"id":"390234"},"instance_id":"1","service_id":"2214","host_id":"2208","host_name":"host5","host_alias":"host5","name":"crond","host_display_name":"","host_address":"10.10.10.5","display_name":"crond","status_update_time":"2017-04-27 12:27:18","status_text":"crond (pid  3398) is running...","status_text_long":"","current_state":"0","performance_data":"","should_be_scheduled":"1","check_type":"0","last_state_change":"2017-04-27 09:04:08","last_hard_state_change":"2017-04-27 09:04:08","last_hard_state":"0","last_time_ok":"2017-04-27 12:22:48","last_time_warning":"1969-12-31 19:00:00","last_time_critical":"1969-12-31 19:00:00","last_time_unknown":"1969-12-31 19:00:00","last_notification":"1969-12-31 19:00:00","next_notification":"1969-12-31 19:00:00","no_more_notifications":"0","acknowledgement_type":"0","current_notification_number":"0","process_performance_data":"1","obsess_over_service":"1","event_handler_enabled":"1","modified_service_attributes":"0","event_handler":"","check_command":"check_nrpe!check_init_service!-a 'crond'!!!!!!","normal_check_interval":"5","retry_check_interval":"1","check_timeperiod_id":"115","icon_image":"","icon_image_alt":"","has_been_checked":"1","current_check_attempt":"1","max_check_attempts":"5","last_check":"2017-04-27 12:22:48","next_check":"2017-04-27 12:27:46","state_type":"1","notifications_enabled":"0","problem_acknowledged":"0","flap_detection_enabled":"1","is_flapping":"0","percent_state_change":"0","latency":"0","execution_time":"0.08153","scheduled_downtime_depth":"0","passive_checks_enabled":"1","active_checks_enabled":"1"}}}

I really only need to check that "current_state" = 0 or that "status_text" contains "is running". The following greps out the current_state of "0" which would mean my pool member should be marked UP.

curl -k --silent "https://nagiosserver.local/nagiosxi/api/v1/objects/servicestatus?host_name=host5&service_description=crond&apikey=[api-key-here]" | grep -Po 'current_state":"\K.*?(?=",)'
O

So, how to make this a health monitor? Is it possible to do this using the built-in http/https monitor types or is using an external monitor the only way since the URL used is always my nagios server's and not the pool member's?

Many thanks!

1 Reply

  • I ended up creating an external file monitor with the following. Still need to fix the "-k" requirement on the curl command...

    In the LTM health monitor, add a variable with the Name of HOST_NAME and the Value of the host's name as seen by Nagios.

    This could easily be re-purposed to check any service's status in Nagios...

    !/bin/sh
    
     This script expects the following Name/Value in the LTM monitor and no Arguments are needed
      HOST_NAME  = the monitored host name in Nagios
    
     remove IPv6/IPv4 compatibility prefix (LTM passes addresses in IPv6 format)
    IP=`echo ${1} | sed 's/::ffff://'`
    PORT=${2}
    
    PIDFILE="/var/run/`basename ${0}`.${IP}_${PORT}.pid"
     kill of the last instance of this monitor if hung and log current pid
    if [ -f $PIDFILE ]
    then
       echo "EAV exceeded runtime needed to kill ${IP}:${PORT}" | logger -p local0.error
       kill -9 `cat $PIDFILE` > /dev/null 2>&1
    fi
    echo "$$" > $PIDFILE
    
      send request & check for "current_state:0"
        curl -kfNs "https://nagiosserver.local/nagiosxi/api/v1/objects/servicestatus?host_name=${HOST_NAME}&service_description=crond&apikey=[api-key-here]" | grep -i "\"current_state\":\"0\"" 2>&1 > /dev/null
    
         mark node UP if true
        if [ $? -eq 0 ]
        then
            rm -f $PIDFILE
            echo "UP"
        else
            rm -f $PIDFILE
        fi