Getting Current State from JSON Query

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
grenley
Posts: 96
Joined: Tue May 13, 2014 6:06 pm

Getting Current State from JSON Query

Post 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
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Getting Current State from JSON Query

Post 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>
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
grenley
Posts: 96
Joined: Tue May 13, 2014 6:06 pm

Re: Getting Current State from JSON Query

Post 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?
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Getting Current State from JSON Query

Post 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/expl ... -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.
Former Nagios employee
https://www.mcapra.com/
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Getting Current State from JSON Query

Post by cdienger »

Thanks for the assist, @mcapra!
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
grenley
Posts: 96
Joined: Tue May 13, 2014 6:06 pm

Re: Getting Current State from JSON Query

Post by grenley »

Perfect! Thanks very much!
npolovenko
Support Tech
Posts: 3457
Joined: Mon May 15, 2017 5:00 pm

Re: Getting Current State from JSON Query

Post by npolovenko »

@grenley, Sounds like we are ready to lock this thread as resolved?
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Locked