Is there a way to list the groups that a given host is a member of? Kinda rhetorical as if you look at a host in the XI web UI, it shows the groups but looking at the host file itself they aren't there.
We're trying to run a report basically showing every host's membership so we can audit every so often to ensure the hosts are accurately categorized.
List Hosts Memberships
Re: List Hosts Memberships
Hey @AngeloMiletoAngeloMileto wrote: ↑Wed Oct 30, 2024 8:33 am Is there a way to list the groups that a given host is a member of? Kinda rhetorical as if you look at a host in the XI web UI, it shows the groups but looking at the host file itself they aren't there.
We're trying to run a report basically showing every host's membership so we can audit every so often to ensure the hosts are accurately categorized.
You can see all of the hostgroups in /usr/local/nagios/etc/hostgroups.cfg and in there it will have each host that is part of that group in the members section. Like you mentioned looking at the hosts themselves dont have what group they are apart of in /etc/hosts/x.cfg so it may take a little work to get exactly what you want. It shouldnt be too bad to make a script to put everything in a nice format but I understand not everyone likes to do that. Is this what your asking for?
-
- Posts: 39
- Joined: Mon Mar 21, 2022 7:53 am
Re: List Hosts Memberships
Not exactly as I did already know that I could look in the hostsgroups file. That's the issue, that doesn't tell you if say host x should be a part of that group but if you could list every host along with the groups they were associated with, it would be easy to see that host x is NOT a member of the particular group.
I was figuring since it was visible on the web, there would be an API call or even a SQL query?
I was figuring since it was visible on the web, there would be an API call or even a SQL query?
-
- Posts: 39
- Joined: Mon Mar 21, 2022 7:53 am
Re: List Hosts Memberships
So noone knows the sql query to run to get the memberships of a given host?
-
- Posts: 222
- Joined: Wed Aug 23, 2023 11:29 am
Re: List Hosts Memberships
Hi @AngeloMileto,
You may have some luck with the tbl_lnkHostToHostgroup sql table. This is the table used to generate the hostgroups on the host pages in the CCM. I say may because it looks like you inadvertently might have come across a bug as this functionality appears to be broken on at least the latest version of XI.
But, if it is working for you, you can see relationships from hosts to hostgroups with this table.
1) Log into mysql (I believe root or nagiosql users will both work).
2) Select database:
3) View table:
Otherwise, you may have to write a script that parses the hostgroup cfg files and matches hostgroups to hosts.
You may have some luck with the tbl_lnkHostToHostgroup sql table. This is the table used to generate the hostgroups on the host pages in the CCM. I say may because it looks like you inadvertently might have come across a bug as this functionality appears to be broken on at least the latest version of XI.
But, if it is working for you, you can see relationships from hosts to hostgroups with this table.
1) Log into mysql (I believe root or nagiosql users will both work).
Code: Select all
mysql -u <username> -p<password> (no space between -p and password)
Code: Select all
use nagiosql;
Code: Select all
SELECT * FROM tbl_lnkHostToHostgroup;
Code: Select all
SELECT * FROM tbl_lnkHostToHostgroup WHERE idMaster=<host_id>;
Re: List Hosts Memberships
Doesn't give you the relationship origin i.e. Membership set via Host config or Added to Hostgroup direct but, it does give you the membership.
https://github.com/SNapier/hostgroup_click/
https://github.com/SNapier/hostgroup_click/
Code: Select all
python3 hostgroup_click.py -n drs -H "localhost,u2204ncpa"
localhost was found in 1 of 6 total hostgroups. [linux-servers]
u2204ncpa was found in 3 of 6 total hostgroups. [linux-servers,linux-columbia-mo,linux-servers]
Re: List Hosts Memberships
I fixed a bug that would compound the list membership and skew the counts.
I added -o/--origin var to determine if the host has any membership set via the object config.
Happy Monitoring,
--SN
I added -o/--origin var to determine if the host has any membership set via the object config.
Code: Select all
python.exe hostgroup_click.py -n drs -H "u2204ncpa,localhost" -o
u2204ncpa was found in 2 of 6 total hostgroups. [linux-columbia-mo,linux-servers] Membership for 1 groups is assigned via the u2204ncpa host object config. [dev-linux-web]
localhost was found in 1 of 6 total hostgroups. [linux-servers]
--SN
Re: List Hosts Memberships
Upon testing of the origin option I noticed that any hostgroup assigned at the host object level was being omitted from the output of "config/hostgroups" which is no bueno.
I refactored the script to collect both hostgroups output and host object membership and a cumulative total.
I refactored the script to collect both hostgroups output and host object membership and a cumulative total.
Code: Select all
localhost: total_groups=1; Membership for 1 group/s is assigned via hostgroup object config/s. [linux-servers]
u2204ncpa: total_groups=3; Membership for 2 group/s is assigned via hostgroup object config/s. [linux-columbia-mo,linux-servers] Membership for 1 group/s is assigned via host object config/s. [dev-linux-web]