Page 1 of 1

Using api to return hosts and services for a contact

Posted: Wed May 15, 2019 12:32 pm
by awilson
Is it possible to use the API to return the hosts and services associated (linked/configured for) a contact?

Thx

Re: Using api to return hosts and services for a contact

Posted: Wed May 15, 2019 4:09 pm
by scottwilkerson
The short answer is no, however there is an undocumented feature that allows for this using the following endpoint:

for services

Code: Select all

http://YOUR_SERVER/nagiosxi/includes/components/nagioscore/ui/objectjson.php?query=servicelist&contactname=nagiosadmin&apikey=APIKEY
for hosts

Code: Select all

http://YOUR_SERVER/nagiosxi/includes/components/nagioscore/ui/objectjson.php?query=hostlist&contactname=nagiosadmin&apikey=APIKEY
change nagiosadmin to the user you want to query and replace YOUR_SERVER and APIKEY

Re: Using api to return hosts and services for a contact

Posted: Thu May 16, 2019 8:25 pm
by awilson
Thank you for your suggestion.
I had success executing the query using a chrome add-on (YARC!) that is able to provide the header components. It says that the request it used looked like this:
  • {
    "method": "POST",
    "transformRequest": [
    null
    ],
    "transformResponse": [
    null
    ],
    "url": "http://xx.xx.xx.xx/nagiosxi/includes/co ... ervicelist",
    "headers": {
    "Accept": "application/json, text/plain, */*",
    "Authorization": "apikey=keystring",
    "Content-Type": "application/json;charset=utf-8"
    },
    "data": "",
    "timeout": {}
    }
Will the API support a username and password pair instead of an apikey? I'm trying to use a Google API script (https://github.com/bradjasper/ImportJSON) that is able to import JSON into a Google Sheet. I'm not certain how to map the Nagios-required parameters into its fields. We've had success with function using its basic authentication approach for username and password with another application.

function ImportJSONViaPost(url, payload, fetchOptions, query, parseOptions)
or
function ImportJSON(url, query, parseOptions)

Is there a way to format the apikey using some url keywords (authorization?)

If I use "http://YOUR_SERVER/nagiosxi/includes/components/nagioscore/ui/objectjson.php?query=servicelist&contactname=nagiosadmin&apikey=APIKEY" as the url, I get a bad request error (after replacing the placeholders.)

Thanks

Re: Using api to return hosts and services for a contact

Posted: Fri May 17, 2019 8:18 am
by scottwilkerson
What version of XI are you using? this was added in 5.6.0+

If yuo are using a newer version you should be able to simply put

Code: Select all

http://YOUR_SERVER/nagiosxi/includes/components/nagioscore/ui/objectjson.php?query=servicelist&contactname=nagiosadmin&apikey=APIKEY
in your browser whole, that's how I tested it

Re: Using api to return hosts and services for a contact

Posted: Fri May 17, 2019 1:04 pm
by awilson
We are still on 5.4.4. The url works. I'm trying to try different approaches to get the JSON-formatted information into a spreadsheet. I was just wondering if there was an alternative authentication method.

I don't know enough about host rest urls work to figure out how to use this other function. I'll keep searching. Thanks for the info.

You can close this one.

Re: Using api to return hosts and services for a contact

Posted: Fri May 17, 2019 1:14 pm
by scottwilkerson
I will close the thread but I want to give you another option, you can get this from core to and it may work for you

Code: Select all

http://USERNAME:PASSWORD@YOUR_SERVER/nagios/cgi-bin/objectjson.cgi?query=servicelist&contactname=nagiosadmin