API GET

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
hales8181
Posts: 33
Joined: Thu Jan 19, 2017 11:15 am

API GET

Post by hales8181 »

Is it possible when pulling from the API the host group (/nagiosxi/api/v1/config/hostgroup) to pull back the hostgroup members? I'm trying to find a way of enumerating this but it's only pulling back:

@attributes : @{id=469}
instance_id : 1
hostgroup_name : <host group name>
is_active : 1
config_type : 1
alias : <host group name>


Thanks
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: API GET

Post by mcapra »

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/
hales8181
Posts: 33
Joined: Thu Jan 19, 2017 11:15 am

Re: API GET

Post by hales8181 »

Thanks! I'll give this a go!
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: API GET

Post by cdienger »

Hope it helps! Let us know if you have any updates!
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Locked