Page 1 of 1
Custom Rest API Output
Posted: Thu Jan 25, 2018 10:25 pm
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"
}
]
}
}
Re: Custom Rest API Output
Posted: Thu Jan 25, 2018 11:54 pm
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.
Re: Custom Rest API Output
Posted: Fri Jan 26, 2018 12:43 am
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.
Re: Custom Rest API Output
Posted: Fri Jan 26, 2018 9:21 am
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"
}
]
}
}
Re: Custom Rest API Output
Posted: Fri Jan 26, 2018 10:20 am
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.
Re: Custom Rest API Output
Posted: Sun Jan 28, 2018 8:28 pm
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.
Re: Custom Rest API Output
Posted: Mon Jan 29, 2018 12:28 pm
by kyang
Sounds good!
Did you have any more questions or are we okay to close this thread?
Re: Custom Rest API Output
Posted: Mon Jan 29, 2018 8:02 pm
by rajasegar
kyang wrote:Sounds good!
Did you have any more questions or are we okay to close this thread?
Please close this thread.
Re: Custom Rest API Output
Posted: Tue Jan 30, 2018 10:03 am
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!