Rest API to update host in hostgroup not working.

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
amane
Posts: 77
Joined: Thu Jan 18, 2018 9:53 am

Rest API to update host in hostgroup not working.

Post 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.
Last edited by amane on Wed Aug 14, 2019 1:55 pm, edited 1 time in total.
User avatar
mbellerue
Posts: 1403
Joined: Fri Jul 12, 2019 11:10 am

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

Post 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.
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!
amane
Posts: 77
Joined: Thu Jan 18, 2018 9:53 am

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

Post 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?
amane
Posts: 77
Joined: Thu Jan 18, 2018 9:53 am

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

Post by amane »

Also, i am looking to associate a template with host....
User avatar
mbellerue
Posts: 1403
Joined: Fri Jul 12, 2019 11:10 am

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

Post 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.
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!
User avatar
mbellerue
Posts: 1403
Joined: Fri Jul 12, 2019 11:10 am

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

Post 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.
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!
amane
Posts: 77
Joined: Thu Jan 18, 2018 9:53 am

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

Post 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.
User avatar
mbellerue
Posts: 1403
Joined: Fri Jul 12, 2019 11:10 am

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

Post 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
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!
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

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

Post 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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked