Adding existing services to host via REST API

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
johncwelch
Posts: 22
Joined: Mon Jun 15, 2015 8:04 am

Adding existing services to host via REST API

Post by johncwelch »

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?
User avatar
tacolover101
Posts: 432
Joined: Mon Apr 10, 2017 11:55 am

Re: Adding existing services to host via REST API

Post by tacolover101 »

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.
johncwelch
Posts: 22
Joined: Mon Jun 15, 2015 8:04 am

Re: Adding existing services to host via REST API

Post by johncwelch »

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

Re: Adding existing services to host via REST API

Post by lmiltchev »

I have a number of existing services (Windows C: Drive utilization for example), and I want to automate adding hosts to that service.
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 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
}
I created 4 "fake" hosts - MyHost1, MyHost2, MyHost3, and MyHost4.

Example:

Code: Select all

define host {
    host_name    MyHost1
    use          xiwizard_generic_host
    address      127.0.0.1
    register     1
}
I also created a "fake" hostgroup - windows-hosts, and added MyHost3, and MyHost4 hosts to it.

Example:

Code: Select all

define hostgroup {
    hostgroup_name    windows-hosts
    alias             My Windows Hosts
    members           MyHost3,MyHost4
}
Next, I ran the following command from the CLI on my test Nagios XI server:

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&notification_interval=60&notification_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."
}
The existing "Drive C: Disk Usage" service on "Windows7" host was added successfully to all 4 "fake" hosts. The service was added directly to "MyHost1" and "MyHost2", and indirectly to "MyHost3" and "MyHost4" (via the "windows-hosts" hostgroup).

In the GUI:
example01.PNG
example03.PNG
example02.PNG
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

Post by johncwelch »

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:
Screen Shot 2018-04-25 at 12.58.18.png
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&notification_interval=5&notification_period=xi_timeperiod_24x7&applyconfig=1"
it completes successfully, but I now have:
Screen Shot 2018-04-25 at 13.01.15.png
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.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Adding existing services to host via REST API

Post by lmiltchev »

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!
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

Post by johncwelch »

No worries, and I appreciate the help!
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Adding existing services to host via REST API

Post by lmiltchev »

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!
krutaw
Posts: 60
Joined: Wed Jul 31, 2013 6:30 pm

Re: Adding existing services to host via REST API

Post by krutaw »

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

Re: Adding existing services to host via REST API

Post by lmiltchev »

@johncwelch let us know if you have any more questions. Thank you!
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked