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?
Adding Hostgroups to Hosts via API
-
optionstechnology
- Posts: 234
- Joined: Thu Nov 17, 2016 11:26 am
Re: Adding Hostgroups to Hosts via API
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:
Example of adding a hostgroup to hosts:
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:
Example of modifying a host config:
Again, you can use either way of configuring host-hostgroup relationships - it wouldn't change the functionality.
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"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¬ification_interval=5¬ification_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¬ification_interval=5¬ification_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¬ification_interval=5¬ification_period=24x7&hostgroups=testapihostgroup&applyconfig=1"
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"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¬ification_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¬ification_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¬ification_interval=30&hostgroups=testapihostgroup2&applyconfig=1"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
When I try this, I get a response saying "This section has not yet been implemented."
Am I missing something?
Thanks
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
What version of XI are you running? These were added in versions > 5.6.0