Page 1 of 2
Does Nagios XI 5.4.8 REST API GET retrieve parents?
Posted: Wed Jan 31, 2018 6:42 pm
by toneill2000
I'm using Nagios XI 5.4.8 and have read all my available REST API system documentation. I can successfully use all the GET, PUT and POST commands and documented. However, one item seems to be missing that I want to accomplish. That is, I want to retreive with GET operation the parent information for a given host. But, when I use the GET objects/host operation, it only gives me information about the host and none of the relationships, again in my case I want the parents of the host. Is this possible with REST API?
I see the the *.cfg files contain the parent information and if I could just get at that data through the REST API, I would be all set. Ideally, there would be some way to use the REST API and get connecting relationship information about objects. It seems this might be limited with my version though. Any help is appreciated.
Todd
p.s., these are the only fields that are returned from REST API call for host info:
hostList.jpg
Re: Does Nagios XI 5.4.8 REST API GET retrieve parents?
Posted: Thu Feb 01, 2018 2:10 pm
by dwhitfield
The developers are making big API improvements for XI 5.5 and 6.0. Keep your eye on
https://www.nagios.com/roadmaps/
Re: Does Nagios XI 5.4.8 REST API GET retrieve parents?
Posted: Thu Feb 01, 2018 6:18 pm
by toneill2000
@dwhitfield, thanks for the link. Do you know if there is any capability to accomplish what I'm looking for?
One idea that might work is to use a custom api endpoint. I just have not yet tinkered with this - other than the small tutorial in the help system. I wish there were more examples or documentation on how to leverage this. Other ideas? Thanks.
Todd
Re: Does Nagios XI 5.4.8 REST API GET retrieve parents?
Posted: Thu Feb 01, 2018 6:21 pm
by dwhitfield
We should be able to get you a database query tomorrow that will help you, but it's just not in the API right now. I don't have the d query, another tech does. If you want it, just respond to this to get the request back on our board.
Re: Does Nagios XI 5.4.8 REST API GET retrieve parents?
Posted: Thu Feb 01, 2018 7:31 pm
by toneill2000
I did a raw SQL statement on the db (see below) which shows what I want. The part I need help with is how to have this type of a SQL statement run in a custom api end point and then send the results back to caller. I'm not finding examples that show how to do this.
If you have any or at least a link to one, that would be great! In general, I can write PHP code and query against MySQL so I just need a jump start here. Again, I've created the sample custom api endpoint from the help system but that was very basic and didn't show how to connect to db or retrieve any Nagios data. Thanks in advance.
Todd
Code: Select all
SELECT
h.display_name AS child,
h.host_object_id child_object_id,
p.parent_host_object_id,
o.name1 parent_name
FROM
nagios.nagios_hosts h
JOIN
nagios.nagios_host_parenthosts p ON p.host_id = h.host_id
JOIN
nagios.nagios_objects o ON o.object_id = p.parent_host_object_id
WHERE
h.display_name = 'testapihost';
Results showing my one child with its two parents:
child, child_object_id, parent_host_object_id, parent_name
testapihost, 426, 161, server1.parent1.us
testapihost, 426, 162, server2.parent2.us
Re: Does Nagios XI 5.4.8 REST API GET retrieve parents?
Posted: Fri Feb 02, 2018 3:55 pm
by dwhitfield
Unfortunately, we are unable to write custom endpoints. It may be that someone on the forum is willing to help out. API improvements are coming. We'd love to have them, but they just don't exist.
Re: Does Nagios XI 5.4.8 REST API GET retrieve parents?
Posted: Sun Feb 04, 2018 10:05 am
by toneill2000
Hi
@dwhitfield, I'm actually not wanting anyone to write my script for me. I'm merely looking to see if it's a viable approach for me to have my custom endpoint simply select data from the db and then return that to the caller. If that's not recommended, or not best practice, or there's a better way to accomplish my goal, I would like to know before I go further.
If my suggested approach (simply running a SQL statement in my custom endpoint and returning results) is fine, then I'm hoping someone on the forum can copy / paste a few lines of code to indicate this is how you would do this within the Nagios framework. I'm guessing it's pretty simple - it's just a matter of seeing a little snippet to get me off and running. Thanks!
Todd
Re: Does Nagios XI 5.4.8 REST API GET retrieve parents?
Posted: Sun Feb 04, 2018 2:04 pm
by toneill2000
I figured it out. I'm surprised there's no snippets in the forums (at least that I found) for custom api endpoints which shows doing basic DB access from a custom end point. I'm posting it here in case this helps anyone. I created two endpoints; one that returns a host along with comma separate parents and another that returns a hostgroup along with comma separated members. I'm all set for now. Thanks.
Todd
Code: Select all
<?php
// include the xi component helper
require_once(dirname(__FILE__) . '/../componenthelper.inc.php');
// Testing the inclusion of the common include
require_once(dirname(__FILE__) . '../../../common.inc.php');
// define/call our component initialize function
toddcustomendpoints_component_init();
function toddcustomendpoints_component_init()
{
// information to pass to xi about our component
$args = array(
COMPONENT_NAME => "toddcustomendpoints",
COMPONENT_VERSION => "1.0",
COMPONENT_AUTHOR => "todd, LLC",
COMPONENT_DESCRIPTION => "todd Nagios XI Custom API Endpoints",
COMPONENT_TITLE => "todd retrieve data operations"
);
// register with xi
register_component("toddcustomendpoints", $args);
// register todd custom api handlers
register_custom_api_callback('todd', 'host', 'toddcustomendpoints_todd_host');
register_custom_api_callback('todd', 'hostgroup', 'toddcustomendpoints_todd_hostgroup');
}
// Returns host and host's parents separated with comma. First element in $args is host name
function toddcustomendpoints_todd_host($endpoint, $verb, $args) {
// Validate first element in $args
if (sizeof($args) == 0) {
return '{ "error":"host argument not found and is required" }';
}
$sql = "
SELECT
h2.host_name parent_host
FROM
tbl_host h
JOIN
tbl_lnkHostToHost hh ON hh.idMaster = h.id
JOIN
tbl_host h2 ON h2.id = hh.idSlave
WHERE
h.host_name = '" . $args[0] . "';
";
$host = $args[0];
$parents = '';
$rs = exec_sql_query(DB_NAGIOSQL, $sql);
// Concatenate parent names
while($row = $rs->FetchRow()) {
$parents = $parents . ',' . $row['parent_host'];
}
// Strip leading comma
$parents = substr($parents, 1);
return '{ "host_name":"' . $host . '", "parents":"' . $parents . '" }';
}
// Returns hostgroup and groups members separated with comma. First element in $args is hostgroup_name
function toddcustomendpoints_todd_hostgroup($endpoint, $verb, $args) {
// Validate first element in $args
if (sizeof($args) == 0) {
return '{ "error":"host argument not found and is required" }';
}
$sql = "
SELECT
h.host_name
FROM
tbl_hostgroup hg
JOIN
tbl_lnkHostgroupToHost hgh ON hgh.idMaster = hg.id
JOIN
tbl_host h ON h.id = hgh.idSlave
WHERE
hg.hostgroup_name = '" . $args[0] . "';
";
$hostGroup = $args[0];
$hosts = '';
$rs = exec_sql_query(DB_NAGIOSQL, $sql);
// Concatenate parent names
while($row = $rs->FetchRow()) {
$hosts = $hosts . ',' . $row['host_name'];
}
// Strip leading comma
$hosts = substr($hosts, 1);
return '{ "hostgroup_name":"' . $hostGroup . '", "members":"' . $hosts . '" }';
}
?>
Sample responses from host and hostgroup custom api endpoints:
[img]host.jpg[/img]
[img]hostGroup.jpg[/img]
Re: Does Nagios XI 5.4.8 REST API GET retrieve parents?
Posted: Mon Feb 05, 2018 12:02 pm
by kyang
Glad you found a solution that works for you.
Thanks for sharing it, it will help others as well.
Did you have any more questions, or are we okay to lock this up?
Re: Does Nagios XI 5.4.8 REST API GET retrieve parents?
Posted: Mon Feb 05, 2018 1:34 pm
by toneill2000
@kyang, yes you can lock this. Thank you.