Page 1 of 1

API GET

Posted: Mon Mar 06, 2017 11:51 am
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

Re: API GET

Posted: Mon Mar 06, 2017 1:33 pm
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"
            }
        ]
    }
}

Re: API GET

Posted: Tue Mar 07, 2017 6:48 am
by hales8181
Thanks! I'll give this a go!

Re: API GET

Posted: Tue Mar 07, 2017 10:56 am
by cdienger
Hope it helps! Let us know if you have any updates!