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
API GET
Re: API GET
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:
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):
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"}';
}
?>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/
https://www.mcapra.com/
Re: API GET
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.