Hello,
I am trying to figure out how to use the API to get the relationship between our hosts and what their associated contacts/contactgroups are. The API reference within the web interface doesn't seem to have anything that indicates this either way. What am I missing?
I was hoping to put together something that would use the API to export all hosts within a hostgroup, and any contacts or contactgroups they are associated with.
Missing API documentation
Re: Missing API documentation
Currently, this is not possible thing to do via the REST API. I can submit a feature request on your behalf if you'd like. Please keep in mind that the decision to implement the enhancement is at the discretion of our development team.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Re: Missing API documentation
Thank you for the reply. So, I dug into it a bit more and thought I may have found a way, but then hit a wall and am wondering if it's the same wall that you are already aware of.
If I use the API (/nagiosxi/api/v1/config/host), I can get a list of all hosts which also seems to contain host group, contact, and contact group info. For example, here is some PowerShell code I used:
This does result in a CSV that looks like it has what I need:
However, I am finding that the results from the API do not match the GUI / the system at large. For example, if I check on the same host as in that example of the CSV output, in the GUI it shows that it does have a relationship, and is in fact a member of the "Network" host group whereas the API output shows it is not a member of any host groups:
Is this mismatch what leads you to say that for now this isn't supported via the API, or am I just doing something incorrectly?
If I use the API (/nagiosxi/api/v1/config/host), I can get a list of all hosts which also seems to contain host group, contact, and contact group info. For example, here is some PowerShell code I used:
Code: Select all
$Nagios_Hosts = Invoke-RestMethod -Method GET -Uri "http://$NagiosXI_Server/nagiosxi/api/v1/config/host?apikey=$NagiosXI_APIKey"
$Nagios_Hosts | Select-Object Host_Name,Address,@{N='host_groups';E={$_.hostgroups -join ","}},@{N='contacts';E={$_.contacts -join ","}},@{N='contact_groups';E={$_.contact_groups -join ","}} | Export-Csv -NoTypeInformation .\Documents\NagiosHostExport.csv
However, I am finding that the results from the API do not match the GUI / the system at large. For example, if I check on the same host as in that example of the CSV output, in the GUI it shows that it does have a relationship, and is in fact a member of the "Network" host group whereas the API output shows it is not a member of any host groups:
Is this mismatch what leads you to say that for now this isn't supported via the API, or am I just doing something incorrectly?
You do not have the required permissions to view the files attached to this post.
Re: Missing API documentation
Worth noting that the PowerShell code and subsequent CSV export does work for some hosts, and shows their host groups etc. I don't know what the difference is between the hosts that work properly and the ones that it shows incorrectly "empty" data.
Re: Missing API documentation
That won't work because you can define the hostgroup in multiple locations in the configs and each is technically unique to that config that you're modifying:
Using /nagiosxi/api/v1/config/host will only show you the hostgroups defined directly on that host config.
Using /nagiosxi/api/v1/config/hostgroups would show the hostgroup members defined directly on that hostgroup config.
If you pass the host into the objects/hostgroupmembers API endpoint it should show them all:
Another place you can look to get that type of info is in the backend Core interface here:
Using /nagiosxi/api/v1/config/host will only show you the hostgroups defined directly on that host config.
Using /nagiosxi/api/v1/config/hostgroups would show the hostgroup members defined directly on that hostgroup config.
If you pass the host into the objects/hostgroupmembers API endpoint it should show them all:
Code: Select all
https://YOURXISERVER/nagiosxi/api/v1/objects/hostgroupmembers?apikey=XXXXXXXX&pretty=1&host_name=THEHOSTNAMECode: Select all
https://YOURXISERVER/nagios/cgi-bin/config.cgi