Custom Rest API Output

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
rajasegar
Posts: 1018
Joined: Sun Mar 30, 2014 10:49 pm

Custom Rest API Output

Post by rajasegar »

For REST API can we choose only a few fields to return

Like in the result below, just return for state entry the following line only.

"state_time": "2015-09-24 03:39:52",
"host_name": "127.0.0.1",
"service_description": "Load",
"state": "2",

Is is possible to change / add any new API config files to achieve this?

Thanks

Code: Select all

{
    "statehistory": {
        "recordcount": "2",
        "stateentry": [
            {
                "instance_id": "1",
                "state_time": "2015-09-24 03:39:52",
                "object_id": "175",
                "objecttype_id": "2",
                "host_name": "127.0.0.1",
                "service_description": "Load",
                "state_change": "1",
                "state": "2",
                "state_type": "0",
                "current_check_attempt": "1",
                "max_check_attempts": "5",
                "last_state": "0",
                "last_hard_state": "0",
                "output": "awfawfawf"
            },
            {
                "instance_id": "1",
                "state_time": "2015-09-24 03:39:48",
                "object_id": "175",
                "objecttype_id": "2",
                "host_name": "127.0.0.1",
                "service_description": "Load",
                "state_change": "1",
                "state": "0",
                "state_type": "1",
                "current_check_attempt": "5",
                "max_check_attempts": "5",
                "last_state": "3",
                "last_hard_state": "3",
                "output": "awfawfawf"
            }
        ]
    }
}
5 x Nagios 5.6.9 Enterprise Edition
RHEL 6 & 7
rrdcached & ramdisk optimisation
User avatar
tacolover101
Posts: 432
Joined: Mon Apr 10, 2017 11:55 am

Re: Custom Rest API Output

Post by tacolover101 »

i believe there was an update made which enabled custom API setup for endpoints, but i can't seem to find the link for it.

another option is using a tool such as jq, to parse out json and pass your info as variables.
rajasegar
Posts: 1018
Joined: Sun Mar 30, 2014 10:49 pm

Re: Custom Rest API Output

Post by rajasegar »

tacolover101 wrote:i believe there was an update made which enabled custom API setup for endpoints, but i can't seem to find the link for it.

another option is using a tool such as jq, to parse out json and pass your info as variables.
Thanks for the info. Hope someone has the link for it.
5 x Nagios 5.6.9 Enterprise Edition
RHEL 6 & 7
rrdcached & ramdisk optimisation
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Custom Rest API Output

Post by mcapra »

I believe under the "Help" section of the Nagios XI GUI, there is a "Custom API Endpoints" section.

Here's an older post I made that implements one for the purpose of finding all members of a given hostgroup (it's in the "customers only" section, so I quoted it below):
https://support.nagios.com/forum/viewto ... 8&p=214393
There isn't a way to do this directly via the API in it's current state, though I know dependency resolution is on the developer's to-do list.

I wrote this little custom API endpoint that should get you a list of hosts in a particular hostgroup:

Code: Select all

<?php

// include the xi component helper
require_once(dirname(__FILE__) . '/../componenthelper.inc.php');
require_once(dirname(__FILE__) . '/../../utils-xmlobjects.inc.php');

// define/call our component initialize function
nagiosxicustomendpoints_component_init();
function nagiosxicustomendpoints_component_init()
{

    // information to pass to xi about our component
    $args = array(
        COMPONENT_NAME =>           "nagiosxicustomendpoints",
        COMPONENT_VERSION =>        "1.0",
        COMPONENT_AUTHOR =>         "Nagios Enterprises, LLC",
        COMPONENT_DESCRIPTION =>    "Demonstrate Nagios XI Custom API Endpoints",
        COMPONENT_TITLE =>          "Nagios XI Custom API Endpoints Example"
        );

    // register with xi
    register_component("nagiosxicustomendpoints", $args);

    // register our custom api handler
    register_custom_api_callback('custom', 'hostsinhostgroup', 'nagiosxicustomendpoints_custom_hostsinhostgroup');

}

// the function to be called when we reach our custom endpoint via api
function nagiosxicustomendpoints_custom_hostsinhostgroup($endpoint, $verb, $args) {
   $hostgroups = json_decode(xml2json::transformXmlStringToJson(get_hostgroup_member_objects_xml_output()));

   foreach($hostgroups->hostgrouplist->hostgroup as $hg){
      if($hg->hostgroup_name == $_GET['hostgroup'])
         return $hg;
   }
   
   return '{"message":"No results found"}';
}

?>
Refer to the help section of Nagios XI for implementing it (specifically "Custom API Endpoints"). Note that this endpoint comes with no particular guarantees/merchantability; It's as-is and I did it for fun.

Usage example (with hostgroup=hostgroup_name being required):

Code: Select all

[root@xi-stable libexec]# curl -XGET "http://192.168.67.1/nagiosxi/api/v1/custom/hostsinhostgroup?apikey=KR2LLsBuhmmFnS4dbmeURW0culVlv39vbbBVW8pet69bXdH8CUiK8DcFX7gMpohD&pretty=1&hostgroup=linux-servers"
{
    "@attributes": {
        "id": "144"
    },
    "instance_id": "1",
    "hostgroup_name": "linux-servers",
    "members": {
        "host": [
            {
                "@attributes": {
                    "id": "222"
                },
                "host_name": "192.168.67.103"
            },
            {
                "@attributes": {
                    "id": "224"
                },
                "host_name": "192.168.67.106"
            },
            {
                "@attributes": {
                    "id": "225"
                },
                "host_name": "192.168.67.107"
            },
            {
                "@attributes": {
                    "id": "229"
                },
                "host_name": "192.168.67.200"
            },
            {
                "@attributes": {
                    "id": "231"
                },
                "host_name": "192.168.67.4"
            },
            {
                "@attributes": {
                    "id": "232"
                },
                "host_name": "192.168.67.5"
            },
            {
                "@attributes": {
                    "id": "235"
                },
                "host_name": "192.168.67.97"
            },
            {
                "@attributes": {
                    "id": "284"
                },
                "host_name": "testhostfile"
            },
            {
                "@attributes": {
                    "id": "221"
                },
                "host_name": "192.168.67.101"
            }
        ]
    }
}
Former Nagios employee
https://www.mcapra.com/
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Custom Rest API Output

Post by dwhitfield »

mcapra wrote:I believe under the "Help" section of the Nagios XI GUI, there is a "Custom API Endpoints" section.
That is correct.
rajasegar
Posts: 1018
Joined: Sun Mar 30, 2014 10:49 pm

Re: Custom Rest API Output

Post by rajasegar »

mcapra wrote:I believe under the "Help" section of the Nagios XI GUI, there is a "Custom API Endpoints" section.

Here's an older post I made that implements one for the purpose of finding all members of a given hostgroup (it's in the "customers only" section, so I quoted it below):
https://support.nagios.com/forum/viewto ... 8&p=214393
There isn't a way to do this directly via the API in it's current state, though I know dependency resolution is on the developer's to-do list.

I wrote this little custom API endpoint that should get you a list of hosts in a particular hostgroup:

Code: Select all

<?php

// include the xi component helper
require_once(dirname(__FILE__) . '/../componenthelper.inc.php');
require_once(dirname(__FILE__) . '/../../utils-xmlobjects.inc.php');

// define/call our component initialize function
nagiosxicustomendpoints_component_init();
function nagiosxicustomendpoints_component_init()
{

    // information to pass to xi about our component
    $args = array(
        COMPONENT_NAME =>           "nagiosxicustomendpoints",
        COMPONENT_VERSION =>        "1.0",
        COMPONENT_AUTHOR =>         "Nagios Enterprises, LLC",
        COMPONENT_DESCRIPTION =>    "Demonstrate Nagios XI Custom API Endpoints",
        COMPONENT_TITLE =>          "Nagios XI Custom API Endpoints Example"
        );

    // register with xi
    register_component("nagiosxicustomendpoints", $args);

    // register our custom api handler
    register_custom_api_callback('custom', 'hostsinhostgroup', 'nagiosxicustomendpoints_custom_hostsinhostgroup');

}

// the function to be called when we reach our custom endpoint via api
function nagiosxicustomendpoints_custom_hostsinhostgroup($endpoint, $verb, $args) {
   $hostgroups = json_decode(xml2json::transformXmlStringToJson(get_hostgroup_member_objects_xml_output()));

   foreach($hostgroups->hostgrouplist->hostgroup as $hg){
      if($hg->hostgroup_name == $_GET['hostgroup'])
         return $hg;
   }
   
   return '{"message":"No results found"}';
}

?>
Refer to the help section of Nagios XI for implementing it (specifically "Custom API Endpoints"). Note that this endpoint comes with no particular guarantees/merchantability; It's as-is and I did it for fun.

Usage example (with hostgroup=hostgroup_name being required):

Code: Select all

[root@xi-stable libexec]# curl -XGET "http://192.168.67.1/nagiosxi/api/v1/custom/hostsinhostgroup?apikey=KR2LLsBuhmmFnS4dbmeURW0culVlv39vbbBVW8pet69bXdH8CUiK8DcFX7gMpohD&pretty=1&hostgroup=linux-servers"
{
    "@attributes": {
        "id": "144"
    },
    "instance_id": "1",
    "hostgroup_name": "linux-servers",
    "members": {
        "host": [
            {
                "@attributes": {
                    "id": "222"
                },
                "host_name": "192.168.67.103"
            },
            {
                "@attributes": {
                    "id": "224"
                },
                "host_name": "192.168.67.106"
            },
            {
                "@attributes": {
                    "id": "225"
                },
                "host_name": "192.168.67.107"
            },
            {
                "@attributes": {
                    "id": "229"
                },
                "host_name": "192.168.67.200"
            },
            {
                "@attributes": {
                    "id": "231"
                },
                "host_name": "192.168.67.4"
            },
            {
                "@attributes": {
                    "id": "232"
                },
                "host_name": "192.168.67.5"
            },
            {
                "@attributes": {
                    "id": "235"
                },
                "host_name": "192.168.67.97"
            },
            {
                "@attributes": {
                    "id": "284"
                },
                "host_name": "testhostfile"
            },
            {
                "@attributes": {
                    "id": "221"
                },
                "host_name": "192.168.67.101"
            }
        ]
    }
}
Thanks, this has been a great help.
5 x Nagios 5.6.9 Enterprise Edition
RHEL 6 & 7
rrdcached & ramdisk optimisation
kyang

Re: Custom Rest API Output

Post by kyang »

Sounds good!

Did you have any more questions or are we okay to close this thread?
rajasegar
Posts: 1018
Joined: Sun Mar 30, 2014 10:49 pm

Re: Custom Rest API Output

Post by rajasegar »

kyang wrote:Sounds good!

Did you have any more questions or are we okay to close this thread?
Please close this thread.
5 x Nagios 5.6.9 Enterprise Edition
RHEL 6 & 7
rrdcached & ramdisk optimisation
kyang

Re: Custom Rest API Output

Post by kyang »

Sounds good! I'll be closing this thread!

If you have any more questions, feel free to create another thread.

Thanks for using the Nagios Support Forum!
Locked