Page 1 of 1

API Question

Posted: Mon Mar 08, 2021 1:21 pm
by J.A.K
I've been digging through the API documentation, but I can seem to find a call\filter for seeing what hosts have a host template applied. I was thinking maybe:

https://nagiosxi.windstream.com/nagiosx ... onfig/host?

with use=templatename as a filter, but that pulls back all hosts. Is there any good API method to see what hosts have a host template applied?

Re: API Question

Posted: Mon Mar 08, 2021 4:37 pm
by benjaminsmith
Hi @J.A.K,

That would be the best endpoint to pull this from. You can filter this down using a script to pull just the host name and corresponding template files.

While we do not provide customer programming in support, here's a sample script (using python 3):

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
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')
Let us know if you have any other questions.
--Benjamin

Re: API Question

Posted: Tue Mar 09, 2021 9:51 pm
by J.A.K
Sounds good thank you! I just wanted to make sure there wasn't a filter already before I reinvented the wheel.

Re: API Question

Posted: Wed Mar 10, 2021 12:10 pm
by benjaminsmith
Hi,

Your welcome.

Let us know if you have any other questions or if it's okay to close this topic.

Regards,
Benjamin

Re: API Question

Posted: Thu Mar 11, 2021 2:06 am
by jweijters
I apologize for breaking in into this thread,
Is there a place where we can share custom API endpoints?

I've written an custom API endpoint for retrieving free variables from hosttemplates.
I would like to share my custom API endpoint, and would like to see what others have written

Kind regards,

Joris Weijters

Re: API Question

Posted: Thu Mar 11, 2021 11:03 am
by benjaminsmith
Hi Joris,
I apologize for breaking in into this thread,
Is there a place where we can share custom API endpoints?
Not a problem. That would be great. Right now, the best place would be on the Nagios Exchange in the Utilities category. You can upload a file or post a link to your GitHub or other code sharing site.

Best Regards,
Benjamin