More RESTapi Fun

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
highness
Posts: 192
Joined: Thu May 01, 2014 4:25 pm

More RESTapi Fun

Post by highness »

We use VCAC and AWS to spin up (and burn down) hosts on a daily basis. Trying to keep the current hosts in monitoring is a huge challenge.

I've been trying to automate the addition of hosts to Nagios, but have have hit a stumbling block with the RESTapi stuff.

Here is what I'm trying to add a host to a predefined host group:

Code: Select all

curl -XPOST "http://nagios.example.com/nagiosxi/api/v1/system/applyconfig?apikey=<supersecretkey>&pretty=1" -d "host_name=us-hq1-tst-i-nagios-0002.example.com&address=10.10.10.10&hostgroups=monitoring_server&max_check_attempts=5&check_interval=5&retry_interval=1&check_period=xi_timeperiod_24x7&contacts=indeat&notification_interval=60&notification_period=xi_timeperiod_24x7&force=1&applyconfig=1"
When I run that curl command, I get this:

Code: Select all

{
    "success": "Apply config command has been sent to the backend."
}
Yet, the host never shows up in Nagios.

I can manually add that host to the host group by copying the us-hq1-tst-i-nagios-0002.example.com.cfg file into the /usr/local/nagios/etc/import directory and applying the config.

Is anyone able to add a host this way?
User avatar
lmiltchev
Former Nagios Staff
Posts: 13587
Joined: Mon May 23, 2011 12:15 pm

Re: More RESTapi Fun

Post by lmiltchev »

What is the Nagios XI version that you are currently running? I was able to add a host via the REST API and add it to a "predefined" hostgroup without any issues in Nagios XI 5.2.9. My "success" message is different than yours though:

Code: Select all

{
    "success": "Successfully added APIhost to the system. Config applied, Nagios Core was restarted."
}
I wonder if you are using an older version of XI.
Be sure to check out our Knowledgebase for helpful articles and solutions!
highness
Posts: 192
Joined: Thu May 01, 2014 4:25 pm

Re: More RESTapi Fun

Post by highness »

lmiltchev wrote:What is the Nagios XI version that you are currently running? I was able to add a host via the REST API and add it to a "predefined" hostgroup without any issues in Nagios XI 5.2.9. My "success" message is different than yours though:

Code: Select all

{
    "success": "Successfully added APIhost to the system. Config applied, Nagios Core was restarted."
}
I wonder if you are using an older version of XI.
I'm currently on 5.2.5. I'll look at upgrading and trying this issue again.
User avatar
lmiltchev
Former Nagios Staff
Posts: 13587
Joined: Mon May 23, 2011 12:15 pm

Re: More RESTapi Fun

Post by lmiltchev »

I'm currently on 5.2.5. I'll look at upgrading and trying this issue again.
Let us know if you run into the same issue after the upgrade. We will keep this thread open for the time being.
Be sure to check out our Knowledgebase for helpful articles and solutions!
highness
Posts: 192
Joined: Thu May 01, 2014 4:25 pm

Re: More RESTapi Fun

Post by highness »

This is still horribly broken...

I installed 5.2.9 on a new system and tried adding a new system to a nonexistent host group:

Code: Select all

curl -XPOST "http://10.10.10.5/nagiosxi/api/v1/config/hostgroup?apikey=<SECRET_KEY>&pretty=1" -d "host_name=webtraffic.example.com&address=10.10.10.78&alias=webtraffic.example.com&hostgroup_name=rixx_test1&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7&applyconfig=1"
and it returned:

Code: Select all

{
    "success": "Successfully added rixx_test1 to the system. Config applied, Nagios Core was restarted."
}
It created the host group, but didn't create the new host nor add the host to the newly created host group (it only created the host group). Unless I'm missing something, I thought that it would create the host, the host group and place the host into the host group.

I then tried to add another new host to that newly created host group:

Code: Select all

curl -XPOST "http://10.10.10.5/nagiosxi/api/v1/config/hostgroup?apikey=<SECRET_KEY>&pretty=1" -d "host_name=vipview.example.com&address=10.10.10.79&alias=vipview.example.com&hostgroup_name=rixx_test1&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7&applyconfig=1"
and it returned:

Code: Select all

{
    "success": "Successfully added rixx_test1 to the system. Config applied, Nagios Core was restarted."
}
So, it looks like it created the same host group... <sigh>
User avatar
lmiltchev
Former Nagios Staff
Posts: 13587
Joined: Mon May 23, 2011 12:15 pm

Re: More RESTapi Fun

Post by lmiltchev »

You are going about this wrong... There are two ways to do this, provided neither host nor hostgroup exist. Examples below:

Method 1:

// Create a hostgroup

Code: Select all

curl -XPOST "http://192.168.4.41/nagiosxi/api/v1/config/hostgroup?apikey=sb6ohbuv&pretty=1" -d "hostgroup_name=rixx_test1&alias=rixx_test1"
// Create a host and add it to the hostgroup

Code: Select all

curl -XPOST "http://192.168.4.41/nagiosxi/api/v1/config/host?apikey=sb6ohbuv&pretty=1" -d "host_name=webtraffic.example.com&address=10.10.10.78&alias=webtraffic.example.com&hostgroups=rixx_test1&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"
From the CLI:

Code: Select all

[root@localhost ~]# curl -XPOST "http://192.168.4.41/nagiosxi/api/v1/config/hostgroup?apikey=sb6ohbuv&pretty=1" -d "hostgroup_name=rixx_test1&alias=rixx_test1"
{
    "success": "Successfully added rixx_test1 to the system. Config imported but not yet applied."
}
[root@localhost ~]# curl -XPOST "http://192.168.4.41/nagiosxi/api/v1/config/host?apikey=sb6ohbuv&pretty=1" -d "host_name=webtraffic.example.com&address=10.10.10.78&alias=webtraffic.example.com&hostgroups=rixx_test1&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"
{
    "success": "Successfully added webtraffic.example.com to the system. Config applied, Nagios Core was restarted."
}
Configs that were generated:

Code: Select all

define hostgroup {
	hostgroup_name                		rixx_test1
	alias                         		rixx_test1
	}

define host {
	host_name			webtraffic.example.com
	alias				webtraffic.example.com
	address				10.10.10.78
	hostgroups			rixx_test1
	check_command			check_ping!3000,80%!5000,100%
	max_check_attempts		2
	check_period			24x7
	contacts			nagiosadmin
	notification_interval		5
	notification_period		24x7
	register			1
	}
Method 2:

// Create a host

Code: Select all

curl -XPOST "http://192.168.4.41/nagiosxi/api/v1/config/host?apikey=sb6ohbuv&pretty=1" -d "host_name=webtraffic.example.com&address=10.10.10.78&alias=webtraffic.example.com&check_command=check_ping\!3000,80%\!5000,100%&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7"
// Create a hostgroup and add the host as a member

Code: Select all

curl -XPOST "http://192.168.4.41/nagiosxi/api/v1/config/hostgroup?apikey=sb6ohbuv&pretty=1" -d "hostgroup_name=rixx_test1&alias=rixx_test1&members=webtraffic.example.com&applyconfig=1"
From the CLI:

Code: Select all

[root@localhost ~]# curl -XPOST "http://192.168.4.41/nagiosxi/api/v1/config/host?apikey=sb6ohbuv&pretty=1" -d "host_name=webtraffic.example.com&address=10.10.10.78&alias=webtraffic.example.com&check_command=check_ping\!3000,80%\!5000,100%&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7"
{
    "success": "Successfully added webtraffic.example.com to the system. Config imported but not yet applied."
}
[root@localhost ~]# curl -XPOST "http://192.168.4.41/nagiosxi/api/v1/config/hostgroup?apikey=sb6ohbuv&pretty=1" -d "hostgroup_name=rixx_test1&alias=rixx_test1&members=webtraffic.example.com&applyconfig=1"
{
    "success": "Successfully added rixx_test1 to the system. Config applied, Nagios Core was restarted."
}
Configs that were generated:

Code: Select all

define hostgroup {
	hostgroup_name                		rixx_test1
	alias                         		rixx_test1
	members                       		webtraffic.example.com
	}

define host {
	host_name			webtraffic.example.com
	alias				webtraffic.example.com
	address				10.10.10.78
	check_command			check_ping!3000,80%!5000,100%
	max_check_attempts		2
	check_period			24x7
	contacts			nagiosadmin
	notification_interval		5
	notification_period		24x7
	register			1
	}
Be sure to check out our Knowledgebase for helpful articles and solutions!
highness
Posts: 192
Joined: Thu May 01, 2014 4:25 pm

Re: More RESTapi Fun

Post by highness »

Thanks for that solution; I'll give that a shot, but was trying to avoid adding any services manually (or via the CLI) - just want to add the hosts because I want them to inherit the services assigned to the predefined host group.

Let me try to do a better job of explaining of what happens here so you can see what I'm up against and maybe give a little more clarity to what I'm trying to accomplish (you might want to first get a sandwich or a cup of coffee, as this will probably be a bit lengthy):


BACKGROUND
We have developers that don't push any new code to an existing production server (that's another story for another day, but I digress). During an implementation, the developers will spin up new replacement servers and put those new servers into production; requesting us to remove the old servers once the new servers are actively in Nagios.

Once we receive the request to add new hosts, we pick a single "old" server and do a bulk clone using the host names of the new servers that were just spun up (the "old" server is already in a previously defined host group which has inherited the service checks).

Once the new servers are active in Nagios, we now need to remove the "old" hosts and services from Nagios. We typically go in and bulk delete the services associated with each host (sometimes as many as 20 services per host) and then delete the host(s). For one or two servers, this doesn't totally suck, but it is rather inconvenient (now if Nagios would just let me delete a host and didn't give a rip about the services tied to the host, this part would certainly suck less). Doing it this way, typically, will cause us to do 2 - 3 configuration applies. This adding / deleting of services and hosts happens several times a day.

Occasionally, the developers will want some additional service or drive monitored on their hosts. We then have to go in and add that service check to each host (either manually, or occasionally we can clone the service, depending on the number of services or servers to add that service check to). This happens several times a month.

As you can see, this is a total pain in the a$$.

HOW I'M TRYING TO FIX IT
To make this work much more logically (and suck less), I grouped a set of hosts into host groups. That way, I can add a new host to an existing host group and have it inherit the checks associated with that host group. To do that, I did this:

Create a host group called tomcat.
Create a host called tomcat and assigned services to that host.
Assign tomcat the host to the host group called tomcat.

Create a host group called apache_lb.
Create a host called apache_lb and assigned services to that host.
Assign apache_lb the host to the host group called apache_lb.

create a host group called product_api.
Assign the tomcat host group to product_api.
Assign the apache_lb host group to product_api.

So now, when I want to report on the product_api hosts, it includes all the tomcat/apache_lb hosts in one host group.


WHAT I'M TRYING TO ACCOMPLISH
The developers have a script that generates a configuration file for each host when it is spun up in VCAC. That script uses it's Chef data bag name to create the host group. The configuration file looks like this:

Code: Select all

define host {
host_name myprodhost-1455.example.com
address 10.10.10.1
use xiwizard_linuxserver_host
hostgroups production_api-apache_ws
max_check_attempts 5
check_interval 5
retry_interval 1
check_period xi_timeperiod_24x7
contacts noc_folks
notification_interval 60
notification_period xi_timeperiod_24x7
icon_image redhat.png
statusmap_image redhat.png
_xiwizardlinux-server register 1
 }

That configuation file gets copied to the Nagios host. The directory gets checked every 30 minutes. If a new configuration file is found in that directory, the script checks the host group database to verify that host group actually exists. If it does, the config file is copied to /usr/local/nagios/etc/import and an email notification is sent to the NOC who can apply the configuration.

If that host group does not exist (which happens occasionally, as the developers may create a new host group on the fly), we move the configuration file to a holding area and notify the NOC that the host group does not exist in Nagios. The NOC creates that new host group and the config file is then moved back into the holding directory for processing at the next half hour.

MY QUESTION
Is a way to add the host to the host group without having to add additional checks (since they already exist). I thought that if I was just trying to add the host to the (existing) host group, it would just add the host and wouldn't require me to add an additional check.

Does that make this any clearer or make more sense?
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: More RESTapi Fun

Post by eloyd »

The answer was in the section on creating a host, then adding it to an existing host group (which would have all your services defined on it):

Code: Select all

curl -XPOST "http://192.168.4.41/nagiosxi/api/v1/config/host?apikey=sb6ohbuv&pretty=1" -d "host_name=webtraffic.example.com&address=10.10.10.78&alias=webtraffic.example.com&check_command=check_ping\!3000,80%\!5000,100%&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7"

curl -XPOST "http://192.168.4.41/nagiosxi/api/v1/config/hostgroup?apikey=sb6ohbuv&pretty=1" -d "hostgroup_name=rixx_test1&alias=rixx_test1&members=webtraffic.example.com&applyconfig=1"
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
User avatar
lmiltchev
Former Nagios Staff
Posts: 13587
Joined: Mon May 23, 2011 12:15 pm

Re: More RESTapi Fun

Post by lmiltchev »

Thanks, @eloyd!

@highness, eloyd is right - since you already have hostgroups & services, you could use the REST API to add new hosts, then add them as members to your hostgroups.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked