Page 2 of 2
Re: AP: Is there a way to delete a host from the list of hosts that the service runs on without deleting the entire serv
Posted: Thu Oct 03, 2024 1:22 pm
by snapier3
Ah, I get you.
Without removing the association that provides the inheritance of the 1:XX service object I don't think you would be able to accomplish what you're after.
Wonder if throwing something like this on the service object would work, even if group membership says otherwise?
Hostgroup windows includes a box named angryserver.
Windows inherits CPU checks with warn at 75 and crit at 85, this box is angry and I don't want it on that server.
So in the post to the service in the API it would be something like this.
hostgroups=windows
hosts=!angryserver
It's kind of cumbersome and I haven't tested if this would prevent the config from being written for that object.
Re: AP: Is there a way to delete a host from the list of hosts that the service runs on without deleting the entire serv
Posted: Thu Oct 03, 2024 1:41 pm
by eloyd
What you've posted technically works because it creates a correct Nagios Core configuration file. However, it's....cumbersome.

Re: AP: Is there a way to delete a host from the list of hosts that the service runs on without deleting the entire serv
Posted: Thu Oct 03, 2024 2:02 pm
by snapier3
I just test the above scenario and it does work preventing the config from being written when done via the UI so in theory you could do same via the API, I think.
Hostgroup with two members
hg-linux.PNG
hg-memers.PNG
Check applied via hostgroup
hostgroup-apply.PNG
Localhost excluded
excludedhost.PNG
Only deployed to the one host
deployed-service.PNG
Re: AP: Is there a way to delete a host from the list of hosts that the service runs on without deleting the entire serv
Posted: Thu Oct 03, 2024 2:09 pm
by snapier3
eloyd wrote: ↑Thu Oct 03, 2024 1:41 pm
What you've posted technically works because it creates a correct Nagios Core configuration file. However, it's....cumbersome.
Most Definitely True!
Have you tried getting a csv list of hostgroup members though?
--SN
Re: AP: Is there a way to delete a host from the list of hosts that the service runs on without deleting the entire serv
Posted: Thu Oct 03, 2024 2:31 pm
by eloyd
Yes. For us, that's quite easy, because we've written an API pre-processor script. So it would be:
Code: Select all
# napi --api --config -t hostgroupmembers -hg <hostgroup> -o h -o C
host1,host2,host3,host4

Re: AP: Is there a way to delete a host from the list of hosts that the service runs on without deleting the entire serv
Posted: Fri Oct 04, 2024 10:44 am
by danderson
I think I get what you're saying, you want an API call that will basically add the host=!SOMEHOST line to the service config file and/or you want to preexisting delete command for a service to be able to handle that.
Re: AP: Is there a way to delete a host from the list of hosts that the service runs on without deleting the entire serv
Posted: Fri Oct 04, 2024 10:46 am
by snapier3
eloyd wrote: ↑Thu Oct 03, 2024 2:31 pm
Yes. For us, that's quite easy, because we've written an API pre-processor script. So it would be:
Code: Select all
# napi --api --config -t hostgroupmembers -hg <hostgroup> -o h -o C
host1,host2,host3,host4
Yeah, I just did it in Python
I have a function named nagiosxiGenericAPI that does the http interaction with the API and returns data.
Then I just take what I need from there
Code: Select all
##GET THE HOSTGROUPMEMBERS FOR THE TARGET GROUP
modhg = "&hostgroup_name={}".format(meta.hostgroup)
hostgm = nagiosxiGenericAPI("objects","hostgroupmembers",modhg,"get",auth["url"],auth["apikey"])
hd = hostgm.json()
##BUILD THE LIST
memlst = list()
hostlistcnt = 0
members = hd["hostgroup"][0]["members"]['host']
for i in members:
memlst.append(i["host_name"])
hostlistcnt += 1
print(",".join(memlist))
Re: AP: Is there a way to delete a host from the list of hosts that the service runs on without deleting the entire serv
Posted: Fri Oct 04, 2024 10:49 am
by eloyd
danderson wrote: ↑Fri Oct 04, 2024 10:44 am
I think I get what you're saying, you want an API call that will basically add the
host=!SOMEHOST line to the service config file and/or you want to preexisting delete command for a service to be able to handle that.
Yes!