Adding Hostgroups to Hosts via API

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Adding Hostgroups to Hosts via API

Post by optionstechnology »

Has anyone came up with a good way to add hostgroups to hosts?

At the minute I have to find every hostgroup the host is a member of using /nagiosxi/api/v1/objects/hostgroupmembers

And then import the host again with the list + the new hostgroup using /nagiosxi/api/v1/config/host

It is possibly the most complicated, least efficient way of doing anything... Has anyone came up with better solution?

Is there a command somewhere I am missing?
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: Adding Hostgroups to Hosts via API

Post by lmiltchev »

It doesn't really matter how you have hostgroups defined, e.g. adding a host to hostgroup or vice versa - adding a hostgroup to a host. Nagios will "understand" both configs. It is a lot easier to add members (hosts) to a hostgroup via the REST API with "one go", then the other way around.

Example of adding hosts (members) to a hostgroup:

Code: Select all

curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/hostgroup?xxx&pretty=1" -d "hostgroup_name=testapihostgroup&alias=HostGroup&members=host1,host2,host3&applyconfig=1"
Example of adding a hostgroup to hosts:

Code: Select all

curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/host?apikey=xxx&pretty=1" -d "host_name=host1&address=127.0.0.1&check_command=check_ping\!3000,80%\!5000,100%&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7&hostgroups=testapihostgroup&applyconfig=1"

curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/host?apikey=xxx&pretty=1" -d "host_name=host2&address=127.0.0.1&check_command=check_ping\!3000,80%\!5000,100%&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7&hostgroups=testapihostgroup&applyconfig=1"

curl -XPOST "http://x.x.x.x/nagiosxi/api/v1/config/host?apikey=xxx&pretty=1" -d "host_name=host3&address=127.0.0.1&check_command=check_ping\!3000,80%\!5000,100%&max_check_attempts=2&check_period=24x7&contacts=nagiosadmin&notification_interval=5&notification_period=24x7&hostgroups=testapihostgroup&applyconfig=1"
The examples above are for adding new hosts/hostgroups. If you would like to modify existing hosts/hostgroups, you would need to use the "PUT" command.

Example of modifying a hostgroup config:

Code: Select all

curl -XPUT "http://x.x.x.x/nagiosxi/api/v1/config/hostgroup/testapihostgroup?apikey=xxx&pretty=1&members=host1,host2,host3&applyconfig=1"
Example of modifying a host config:

Code: Select all

curl -XPUT "http://x.x.x.x/nagiosxi/api/v1/config/host/host1?apikey=xxx&pretty=1&address=127.0.0.1&notification_interval=30&hostgroups=testapihostgroup2&applyconfig=1"

curl -XPUT "http://x.x.x.x/nagiosxi/api/v1/config/host/host2?apikey=xxx&pretty=1&address=127.0.0.1&notification_interval=30&hostgroups=testapihostgroup2&applyconfig=1"

curl -XPUT "http://x.x.x.x/nagiosxi/api/v1/config/host/host3?apikey=xxx&pretty=1&address=127.0.0.1&notification_interval=30&hostgroups=testapihostgroup2&applyconfig=1"
Again, you can use either way of configuring host-hostgroup relationships - it wouldn't change the functionality.
Be sure to check out our Knowledgebase for helpful articles and solutions!
optionstechnology
Posts: 234
Joined: Thu Nov 17, 2016 11:26 am

Re: Adding Hostgroups to Hosts via API

Post by optionstechnology »

When I try this, I get a response saying "This section has not yet been implemented."

Am I missing something?

Thanks
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Adding Hostgroups to Hosts via API

Post by scottwilkerson »

What version of XI are you running? These were added in versions > 5.6.0
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
Locked