Page 1 of 1

Getting Current State from JSON Query

PostPosted: Thu Mar 08, 2018 7:02 pm
by grenley
Hi.
This should be an easy and quick one.
I'm using the Core JSON API to get object info.
One of the values that's in status.dat is "current_state" but I don't see that come back in the JSON.
There's a value called "status" but it has a value that does not match the current state.
In the json below, the status = 2 but the current state is "UP" (0).
How do I handle this?

Code: Select all
{
    "format_version": 0,
    "result": {
        "query_time": 1520553414000,
        "cgi": "statusjson.cgi",
        "user": "coreapiuser",
        "query": "host",
        "query_status": "released",
        "program_start": 1520355947000,
        "last_data_update": 1520553407000,
        "type_code": 0,
        "type_text": "Success",
        "message": ""
    },
    "data": {
        "host": {
            "name": "patinfra.sldc.sbc.com",
            "plugin_output": "OK - Agent is alive",
            "long_plugin_output": "",
            "perf_data": "",
            "status": 2,
            "last_update": 1520553407000,
            "has_been_checked": true,
            "should_be_scheduled": true,
            "current_attempt": 1,
            "max_attempts": 1,
            "last_check": 1520553406000,
            "next_check": 1520553577000,
            "check_options": 8,
            "check_type": 1,
            "last_state_change": 1519545569000,
            "last_hard_state_change": 1519545569000,
            "last_hard_state": 0,
            "last_time_up": 1520553407000,
            "last_time_down": 1519545569000,
            "last_time_unreachable": 0,
            "state_type": 1,
            "last_notification": 0,
            "next_notification": 0,
            "no_more_notifications": false,
            "notifications_enabled": true,
            "problem_has_been_acknowledged": false,
            "acknowledgement_type": 0,
            "current_notification_number": 0,
            "accept_passive_checks": true,
            "event_handler_enabled": true,
            "checks_enabled": true,
            "flap_detection_enabled": true,
            "is_flapping": false,
            "percent_state_change": 0,
            "latency": 0,
            "execution_time": 0,
            "scheduled_downtime_depth": 0,
            "process_performance_data": true,
            "obsess": true
        }
    }
}


Thanks, Rick

Re: Getting Current State from JSON Query

PostPosted: Fri Mar 09, 2018 4:08 pm
by cdienger
The 2 actually means up in this cases. You can use the enumerate option to see this:

http://nagios_ip/nagios/cgi-bin/statusjson.cgi?query=host&formatoptions=enumerate&hostname=<hostname>

Re: Getting Current State from JSON Query

PostPosted: Fri Mar 09, 2018 7:22 pm
by grenley
Awesome. Thanks!
Is there full doc on all the API fields and options?
Also, I can't find any doc on how the numeric status values map (like, 2 = "ok")
Is that documented somewhere?

Re: Getting Current State from JSON Query

PostPosted: Mon Mar 12, 2018 11:57 am
by mcapra
grenley wrote:Is there full doc on all the API fields and options?

I don't think so, but the JSON Query Generator (http://<nagios server>/nagios/jsonquery.html) offers all available CGIs and various variables you can set:
https://labs.nagios.com/2014/06/19/exploring-the-new-json-cgis-in-nagios-core-4-0-7-part-1/

grenley wrote:Also, I can't find any doc on how the numeric status values map (like, 2 = "ok"). Is that documented somewhere?

Kinda sorta? Things are handled a bit differently when it comes to filtering within the CGIs. Here's your "host is up" state.

Re: Getting Current State from JSON Query

PostPosted: Mon Mar 12, 2018 1:03 pm
by cdienger
Thanks for the assist, @mcapra!

Re: Getting Current State from JSON Query

PostPosted: Tue Mar 13, 2018 1:03 pm
by grenley
Perfect! Thanks very much!

Re: Getting Current State from JSON Query

PostPosted: Tue Mar 13, 2018 4:23 pm
by npolovenko
@grenley, Sounds like we are ready to lock this thread as resolved?