Using api to return hosts and services for a contact

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
awilson
Posts: 224
Joined: Mon Mar 21, 2016 1:20 pm

Using api to return hosts and services for a contact

Post by awilson »

Is it possible to use the API to return the hosts and services associated (linked/configured for) a contact?

Thx
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

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

Post 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
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
awilson
Posts: 224
Joined: Mon Mar 21, 2016 1:20 pm

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

Post 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
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

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

Post 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
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
awilson
Posts: 224
Joined: Mon Mar 21, 2016 1:20 pm

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

Post 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.
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

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

Post 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
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
Locked