Page 1 of 1
Finding all hosts associated with a Template?
Posted: Tue Jun 08, 2021 2:38 pm
by Matthew.Cary
Is there a way to see all the hosts that are associated with a given template? I can't seem to find this in the GUI or the API reference.
I know I can go into each host & see what host template it has assigned, but I'm really hoping there is somewhere I can look to see all the host associations in one place.
I'm running 5.8.2
Re: Finding all hosts associated with a Template?
Posted: Tue Jun 08, 2021 5:09 pm
by benjaminsmith
Hi Matthew,
Right now, the API is probably the easiest place to retrieve that information, but it will take a little parsing to reduce the output (use the config endpoint). While we do not provide custom scripting in support, here's a sample script (python 3) you can try running, it will print the hostname and any templates. You can adjust the output to check for a certain host template. Let me know if that works for you, Benjamin
Code: Select all
import requests
import json
# Update IP Address / API key with your server information
api = {'baseurl':'http://192.168.23.113/nagiosxi',
'key':'?apikey=sCWXTQ3rHtm483AgRUUtLi04v5ECCVmktCCGoU8mINpPPflWafJbRKeGO8fGjUh6',
'config_hosts': '/api/v1/config/host'
}
def get_api_data(url,endpoint,key):
data = requests.get(url + endpoint + key, verify=False )
return data
r = get_api_data(api['baseurl'],api['config_hosts'],api['key'])
hosts = r.json()
# Print out host name and templates used
i = 0
while i < len(hosts):
print( hosts[i]['host_name'], end=",")
try:
for template in hosts[i]['use']:
print(template, end=',')
except:
print("No Template Assigned", end=",")
i = i + 1
print('\n')
Re: Finding all hosts associated with a Template?
Posted: Thu Jun 10, 2021 2:59 pm
by Matthew.Cary
Thanks! I got lost looking for the template info via a get objects/host instead of a get config/host. (the fact that the data I'm looking for is labed "use" instead of "template" meant I never found it searching through the API Docs.
But this got me where I needed!
I ended up modifying this into powershell:
Invoke-RestMethod "
http://192.168.23.113/nagiosxi/api/v1/c ... 6&pretty=1" | Select-Object -ExpandProperty SyncRoot | select host_name,use
It isn't as well structured a script as yours, but it got me what I wanted.

Re: Finding all hosts associated with a Template?
Posted: Thu Jun 10, 2021 4:46 pm
by benjaminsmith
Hi Matthew,
Glad that was helpful. And thanks for the PowerShell sample (keeping that in my handy collection of notes and snippets)!
Benjamin