Page 1 of 2

Hostgroups & wildcards

Posted: Fri Jul 13, 2018 10:12 am
by hales8181
I have a requirement to populate a hostgroup automatically using wildcards or regex's - but I'm not sure if it's possible.

I have the following:
Hostgroup 1
Host a
Host b
Host c
Host d

Hostgroup 2
Host e
Host f
Host g
Host h

Hostgroup 3
Host a
Host b
Host e
Host f

And what I'm looking to do is to have hostgroup who's members are all hosts that are in Hostgroup 1 AND Hostgroup 3 - so the members would expand to only Host a & Host b.

Is this possible?

Thanks

Re: Hostgroups & wildcards

Posted: Fri Jul 13, 2018 11:13 am
by bolson
Hello,

You may need to clarify your question... Given what you have said, why not just create another hostgroup which includes host a and host b?

Re: Hostgroups & wildcards

Posted: Fri Jul 13, 2018 1:29 pm
by gormank
Why not add hg1 and hg3 to the hg that should contain their members? Otherwise, you need a way to identify the hosts and can then create a script that defines the hostgroups and either import, or create via the API. I define HGs using a substring of hostnames via a script and import, since I'm too dense at this point to use the API...

Re: Hostgroups & wildcards

Posted: Fri Jul 13, 2018 1:48 pm
by lmiltchev
@hales8181, did bolson and gormank answer your question?

Re: Hostgroups & wildcards

Posted: Fri Jul 13, 2018 1:50 pm
by bolson
It sounds like you're asking if hostgroups can be nested, ie: can a hostgroup have members which are other hostgroups rather than hosts. If you're running a current version of XI, the answer is yes. The bigger question to me is, what is it about your environment that makes scripting this out a preferable solution to simply defining your hostgroups in the CCM? It seems as though you're adding a good deal of complexity to something that's pretty trivial in the Web interface.

Re: Hostgroups & wildcards

Posted: Fri Jul 13, 2018 2:38 pm
by gormank
To be clear, my creation of HGs via script is for the initial setup. After that they're pretty much static. I also create hosts from a list of IP/hostname pairs via script. I certainly don't want to be manually creating ~100 hosts in CCM.
There needs to be some way to determine what hosts belong where, so the OP needs to decide what that is.

Re: Hostgroups & wildcards

Posted: Fri Jul 13, 2018 3:01 pm
by bolson
Hello Gormank,

My response and question regarding environment and methods was directed at tho OP. As he has not responded since his initial post. it may be safe to assume his issue has been resolved. Thank you for your input.

Re: Hostgroups & wildcards

Posted: Fri Jul 13, 2018 3:52 pm
by hales8181
I’m in a different time zone - apologies- I have simplified my set up - I’ll explain fully tomorrow as I’m off to bed now, but basically I need to new host group to only contain members that are in not a and c host groups. Not all hosts from a are in c and the other way around - not sure if hat makes sense?

Re: Hostgroups & wildcards

Posted: Fri Jul 13, 2018 4:04 pm
by lmiltchev
...I have simplified my set up - I’ll explain fully tomorrow...
No problem - whenever you have time. Please give us an example of your configs. We will try to test them in-house, and suggest a possible solution.

Re: Hostgroups & wildcards

Posted: Fri Jul 13, 2018 7:18 pm
by Maxwellb99
Hi Hales8181,

I really like this question, but for a different reason. I don't want to hijack your thread so I'll post below & in a different thread.

My understanding of your questions is that you have an existing Nagios implementation that contains already populated hostgroups. You'd like to be able to take the (set) intersection of the hosts in the N Hostgroups to form a new N+1 set. This is how I'd go about solving this.

NOTE: I come from a programming background rather than a sysadmin background. I'm sure there's a way to do this in the shell but I love python.

1. in Python - use the urllib & requests libraries to query the "GET objects/hostgroupmembers" API endpoint
1.1. It'll be a little tricky - The JSON object is a dictionary. but, you'll have to be careful as you want the "host_name" value(s) nested
within the "attributes" key which is itself a list value to the "host" key which belongs to "members".
1.2. As long as you loop through and create a new dictionary with the "hostgroup_name" as the key & the "members" as the value or
even just the "host" list as the value it shouldn't be too bad.
1.3. There are other (easier) ways to do this:
1.3.1 Grab the hostgroups config from /usr/local/nagios/etc/hostgroups.cfg. parse it out as a CSV then read that into python.
1.3.2 Use the GUI & write the hosts in the hostgroups to a CSV then read that into python.

2. Now that you have the data it's almost trivially easy, as sets are a native structure & will output the intersection.
2.1 Depending on how you built your objects it would be something like
2.1.1 hostgroup_new = hostgroup_A.intersection(hostgroup_C)
2.2 Write the data to a CSV.

3. Use the API to create the N + 1 hostgroup: "POST config/hostgroup"
3.1 write a short script that reads in a CSV then executes the POST command.

Now Nagios is going to have to help me out...
I can create hosts, I can create hostgroups, I know when I create a host I can add it to a hostgroup.
These hosts already exist. Would posting the hosts again through the API with only the new hostgroup add them to the hostgroup? Is there a way I'm missing or is the only option to add each host by hand to the new hostgroup?

Either way, you've now created a new hostgroup that contains the intersection of n of the N hostgroups.

The reason I love this question is that we would also love to be able to do something similar. Imagine that we support a hadoop cluster. I know each of the boxes are RHEL but I don't know which services are running on each, nor whether they're management nodes or their function within the cluster, finally, they all belong to the Hadoop Hostgroup. We are vehemently opposed to adding hosts to services as the dependent relationship keeps us from killing them off via the API. So right now I'm creating hostgroups: "Hadoop_Service1", "Hadoop_Service2", ... It would be great to be able to take the intersection of a parent hostgroup from within a service. We can take this in another thread or PM me.

OP hopefully that answers your question. That's how I'd implement it. If you have any questions, comments, or concerns please let me know. Nagios if I'm missing some built in functionality that's much less convoluted please let us know.

Thanks,
Maxwell Ramirez