Adding existing services to host via REST API
-
johncwelch
- Posts: 22
- Joined: Mon Jun 15, 2015 8:04 am
Adding existing services to host via REST API
So to start off, I did read this post: https://support.nagios.com/forum/viewto ... =6&t=36401
But it seems to be for creating new services and adding them to a host. I have a number of existing services (Windows C: Drive utilization for example), and I want to automate adding hosts to that service. All the parameters are already set, I just need to append a new host to that service.
Is this doable via the API?
But it seems to be for creating new services and adding them to a host. I have a number of existing services (Windows C: Drive utilization for example), and I want to automate adding hosts to that service. All the parameters are already set, I just need to append a new host to that service.
Is this doable via the API?
- tacolover101
- Posts: 432
- Joined: Mon Apr 10, 2017 11:55 am
Re: Adding existing services to host via REST API
i can't recall if it's possible to append, but you could add a new service.
something that may help you out is looking at host groups + templates - this can be very powerful for templating different types of devices out.
something that may help you out is looking at host groups + templates - this can be very powerful for templating different types of devices out.
-
johncwelch
- Posts: 22
- Joined: Mon Jun 15, 2015 8:04 am
Re: Adding existing services to host via REST API
given how odd our setups can be, templating ends up being more trouble than it's worth. (we'd have to have around 20 templates or so) And adding new services every time we add a host is not a great way to go either, especially since having 300 versions of "Drive C: utilization" just makes life harder.
if the API isn't up to adding services to a host or vice-versa, that's not a major problem, we can still make good use of the API for adding new hosts.
if the API isn't up to adding services to a host or vice-versa, that's not a major problem, we can still make good use of the API for adding new hosts.
Re: Adding existing services to host via REST API
You do not add a host to a service... You add a service to a host. You can also add a service to a hostgroup. This way, the service will be added to all hosts, that are members of this hostgroup. As you add more members to the hostgroup, the service will be added automatically to the new hosts.I have a number of existing services (Windows C: Drive utilization for example), and I want to automate adding hosts to that service.
I will give you a simple example of how to add an existing service to hosts and a hostgroup via the REST API.
I started off with the existing service - see the config below:
Code: Select all
define service {
host_name Windows7
service_description Drive C: Disk Usage
use xiwizard_windowsserver_nsclient_service
check_command check_xi_service_nsclient!welcome!USEDDISKSPACE!-l C -w 85 -c 95
max_check_attempts 5
check_interval 5
retry_interval 1
check_period xi_timeperiod_24x7
notification_interval 60
notification_period xi_timeperiod_24x7
notifications_enabled 1
contacts nagiosadmin
_xiwizard windowsserver
register 1
}Example:
Code: Select all
define host {
host_name MyHost1
use xiwizard_generic_host
address 127.0.0.1
register 1
}Example:
Code: Select all
define hostgroup {
hostgroup_name windows-hosts
alias My Windows Hosts
members MyHost3,MyHost4
}Code: Select all
curl -XPOST "http://192.168.18.151/nagiosxi/api/v1/config/service?apikey=LTltbjobR0X3V5ViDIitYaI8hjsjoFBaOcWYukamF7oAsD8lhJRvSPWq8I3PjTf7&pretty=1" -d "host_name=Windows7,MyHost1,MyHost2&hostgroup_name=windows-hosts&service_description=Drive C: Disk Usage&use=xiwizard_windowsserver_nsclient_service&check_command=check_xi_service_nsclient\!welcome\!USEDDISKSPACE\!-l C -w 85 -c 95&max_check_attempts=5&check_interval=5&retry_interval=1&check_period=xi_timeperiod_24x7&contacts=nagiosadmin¬ification_interval=60¬ification_period=xi_timeperiod_24x7&force=1&applyconfig=1"
{
"success": "Successfully added Windows7,MyHost1,MyHost2 :: Drive C: Disk Usage to the system. Config applied, Nagios Core was restarted."
}In the GUI: Hope this helps.
You do not have the required permissions to view the files attached to this post.
Be sure to check out our Knowledgebase for helpful articles and solutions!
-
johncwelch
- Posts: 22
- Joined: Mon Jun 15, 2015 8:04 am
Re: Adding existing services to host via REST API
the problem I get with that is I add a duplicate service with the host name as the service name. For example, before, I have:
Then I run:
it completes successfully, but I now have:
which is the thing I don't want. I want the existing service, Linux / Usage test, to show a new host, namely welch macbook, not a new service named welch macbook.
Then I run:
Code: Select all
curl -XPOST "https://nagios-internal.company.com/nagiosxi/api/v1/config/service?apikey=Gf8Ck0RlYdkhK0Nr3E6pBkYQJLMl4FuZTWBtWI8bULsFLf649rtJOLQtsSRQtlPG&pretty=1" -d "host_name=welch macbook&service_description=Linux / Usage test&use=xiwizard_linuxsnmp_storage&check_xi_service_snmp_linux_storage\! -C NwRdCr0 --v2c -G -S 1,0,0 -m "^/$" -w 90 -c 95 -f\!\!\!\!\!\!\!&max_check_attempts=5&check_interval=5&retry_interval=1&check_period=xi_timeperiod_24x7&contacts=joh
n_welch,serveralerts¬ification_interval=5¬ification_period=xi_timeperiod_24x7&applyconfig=1"which is the thing I don't want. I want the existing service, Linux / Usage test, to show a new host, namely welch macbook, not a new service named welch macbook.
You do not have the required permissions to view the files attached to this post.
Re: Adding existing services to host via REST API
Actually, you are correct. Right after I posted, I noticed that a new (duplicate) service was added. I didn't reply right away as I was testing the command. I remembered that there was a new directive in the API ("config_name"), which should be available in the latest stable version of XI. I tried appending "&config_name=Windows7" to my command, but this didn't solve the issue. A duplicate service was still created. 
Just wanted to give you heads up that I am waiting on a feedback from our developers on the issue. Thank you!
Just wanted to give you heads up that I am waiting on a feedback from our developers on the issue. Thank you!
Be sure to check out our Knowledgebase for helpful articles and solutions!
-
johncwelch
- Posts: 22
- Joined: Mon Jun 15, 2015 8:04 am
Re: Adding existing services to host via REST API
No worries, and I appreciate the help!
Re: Adding existing services to host via REST API
It seems that (at least as of now), you will have to delete the service before re-adding it. If you didn't, you would end up with duplicates.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Re: Adding existing services to host via REST API
Here's what we do. I assign services to hostgroups and then update the host to assign a hostgroup to the host, effectively assigning the service to the host. This allows for singular services to be applied across multiple hosts without having to redefine the services over and over. Further, I define OS hostgroups so that standard checks for say windows or linux can be assigned at the host level and then add hostgroups for the custom checks only where necessary.johncwelch wrote:So to start off, I did read this post: https://support.nagios.com/forum/viewto ... =6&t=36401
But it seems to be for creating new services and adding them to a host. I have a number of existing services (Windows C: Drive utilization for example), and I want to automate adding hosts to that service. All the parameters are already set, I just need to append a new host to that service.
Is this doable via the API?
Re: Adding existing services to host via REST API
@johncwelch let us know if you have any more questions. Thank you!
Be sure to check out our Knowledgebase for helpful articles and solutions!