Page 1 of 1

Adding Hostgroups to Hosts via API

Posted: Tue May 07, 2019 8:37 am
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?

Re: Adding Hostgroups to Hosts via API

Posted: Tue May 07, 2019 11:04 am
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.

Re: Adding Hostgroups to Hosts via API

Posted: Wed May 15, 2019 12:11 pm
by optionstechnology
When I try this, I get a response saying "This section has not yet been implemented."

Am I missing something?

Thanks

Re: Adding Hostgroups to Hosts via API

Posted: Wed May 15, 2019 4:21 pm
by scottwilkerson
What version of XI are you running? These were added in versions > 5.6.0