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
Finding all hosts associated with a Template?
-
Matthew.Cary
- Posts: 32
- Joined: Fri Nov 10, 2017 11:43 am
-
benjaminsmith
- Posts: 5324
- Joined: Wed Aug 22, 2018 4:39 pm
- Location: saint paul
Re: Finding all hosts associated with a Template?
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
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')
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Be sure to check out our Knowledgebase for helpful articles and solutions!
-
Matthew.Cary
- Posts: 32
- Joined: Fri Nov 10, 2017 11:43 am
Re: Finding all hosts associated with a Template?
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.
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.
-
benjaminsmith
- Posts: 5324
- Joined: Wed Aug 22, 2018 4:39 pm
- Location: saint paul
Re: Finding all hosts associated with a Template?
Hi Matthew,
Glad that was helpful. And thanks for the PowerShell sample (keeping that in my handy collection of notes and snippets)!
Benjamin
Glad that was helpful. And thanks for the PowerShell sample (keeping that in my handy collection of notes and snippets)!
Benjamin
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Be sure to check out our Knowledgebase for helpful articles and solutions!