Page 1 of 1

Rest API to update host in hostgroup not working.

Posted: Tue Aug 13, 2019 10:27 am
by amane
We are using the below script to call rest api to add host, however it merged host and services together.

def restToSplunkHost(hostname, IP):
print("Method restToSplunkHost")
print(hostname)
print(IP)
data1 = "host_name=%s" %hostname
data2 = "&address=%s&check_command=check-host-alive\&max_check_attempts=3&check_period=xi_timeperiod_24x7&contacts=nagiosad&notification_interval=5&notification_period=xi_timeperiod_24x7&applyconfig=1" %IP
data = data1 + data2
print(data)
url = "http://lxanagasdprd1/nagiosxi/api/v1/co ... c&pretty=1"
req = urllib2.Request(url, data)
f = urllib2.urlopen(req)
for x in f:
print(x)
f.close()

def restToSplunkService(hostname, IP):
print("Method restToSplunkService")
print(hostname)
data1 = "host_name=%s" %hostname
data2 = "&address=%s&service_description=Process: Process Agent Status: splunkd&check_command=check_xi_service_nsclient_NoPassword\!PROCSTATE\!-l splunkd.exe -d SHOWALL&check_interval=5&retry_interval=5&max_check_attempts=2&check_period=xi_timeperiod_24x7&contacts=nagiosadmin&notification_interval=5&notification_period=xi_timeperiod_24x7&applyconfig=1" %IP
data = data1 + data2
print(data)
url = "http://lxnagasprd01.lowes.com/nagiosxi/ ... c&pretty=1"
req = urllib2.Request(url, data)
f = urllib2.urlopen(req)
for x in f:
print(x)
f.close()


Also we are not able to add host in a hostgroup with using rest api.

Re: Rest API to update host in hostgroup not working.

Posted: Tue Aug 13, 2019 1:56 pm
by mbellerue
Both your host and your service functions use this url variable,

Code: Select all

url = "http://lxappnagprd031.lowes.com/nagiosxi/api/v1/config/host...
That API end point is just for working with hosts. For your service function, you'll want

Code: Select all

url = "http://lxappnagprd031.lowes.com/nagiosxi/api/v1/config/service...
I made this change, and while it did add the service, there is still something wrong with the variables you're specifying. I wasn't able to dig too deeply in to it, but maybe with this little push you'll be able to make more progress. A good resource to look at for building your API calls is in Nagios XI. Just go to Help -> Config Reference. That will show you how to build each call.

Re: Rest API to update host in hostgroup not working.

Posted: Wed Aug 14, 2019 4:43 am
by amane
Thank you for looking into it.
I am looking into the variable i am passing.

Also, can i add a host to an existing hostgroup using Rest?

Re: Rest API to update host in hostgroup not working.

Posted: Wed Aug 14, 2019 5:16 am
by amane
Also, i am looking to associate a template with host....

Re: Rest API to update host in hostgroup not working.

Posted: Wed Aug 14, 2019 9:15 am
by mbellerue
I'm not seeing a way to change the template associated with a host in the Config Reference. I'm also not seeing a way to add a host to a hostgroup. For hostgroup, I imagine that functionality is on the way, as contactgroup has methods for adding members. I will put in a request for these features to be added to the API.

Re: Rest API to update host in hostgroup not working.

Posted: Wed Aug 14, 2019 10:21 am
by mbellerue
Cancel that last post of mine. As it turns out there is a way to add hosts to an existing host group. Here's an example command.

Code: Select all

curl -XPOST "http://192.168.23.100/nagiosxi/api/v1/config/hostgroup?apikey=<YourAPIKey>&pretty=1" -d "hostgroup_name=testapihostgroup&alias=HostGroup&members=localhost,Host%20With%20Spaces&applyconfig=1"
In the above command I added two hosts named "localhost" and "Host With Spaces" to the hostgroup "testapihostgroup"

There are three important things demonstrated here.

1. members is the directive we're looking for in order to add hosts
2. You cannot append new hosts to the end of a host group. If you are adding a host to the hostgroup, you must specify all existing hosts as well as the host you are adding.
3. If the name of the host you are adding to the hostgroup includes special characters, like a space, you will need to use HTML characters, like %20.

Given that the members directive exists in the hostgroup end point, I would then also imagine that template would be a directive that exists in the host end point.

Re: Rest API to update host in hostgroup not working.

Posted: Fri Aug 16, 2019 4:12 am
by amane
Thank you !!! i am able to add host to the hostgroup now.

But, i had no luck finding solution to add template to the host.

Re: Rest API to update host in hostgroup not working.

Posted: Fri Aug 16, 2019 9:28 am
by mbellerue
In Nagios XI, if you go to Help -> Config Reference, you've got some good documentation on how to use the API. But there are some pieces of information that are omitted for the sake of brevity. You will see this blurb under many of the required parameter blocks.
** All normal host directives are able to be used like normal core config definition files.
This tells us that we can take any of the definitions (e.g. host_name, alias, address, etc.) for any object (e.g. host, service, contact, etc.) and put them in our API call. Using templates as an example.

Code: Select all

curl -XPOST "http://192.168.145.200/nagiosxi/api/v1/config/host?apikey=<MyAPIKey>&pretty=1" -d "host_name=testapihost&address=192.168.145.91&use=windows-server&check_command=check_ping\!3000,80%\!5000,100%&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7&applyconfig=1"
I tried to put the template in towards the front. So it goes host_name, address, and then use, which defines the template for the host.

This is easily the best document to use for helping to define your objects.
https://assets.nagios.com/downloads/nag ... tions.html

There are three directive common to all object definitions. They are name, use, and register. You can use this document for more information on those.
https://assets.nagios.com/downloads/nag ... tance.html

Re: Rest API to update host in hostgroup not working.

Posted: Fri Aug 16, 2019 9:31 am
by lmiltchev
You can easily add a template to a host via the REST API. You will need to use "&use=<your host template name>" and "&force=1".

Example:

Code: Select all

[root@main-nagios-xi ~]# curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/host?apikey=xxx&pretty=1" -d "host_name=testapihost&address=127.0.0.1&check_command=check_ping\!3000,80%\!5000,100%&use=xiwizard_generic_host&force=1&applyconfig=1"
{
    "success": "Added testapihost to the system. Config applied, Nagios Core was restarted."
}
Let us know if this helped.