List Hosts Memberships

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Post Reply
AngeloMileto
Posts: 39
Joined: Mon Mar 21, 2022 7:53 am

List Hosts Memberships

Post by AngeloMileto »

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.
sgardil
Posts: 271
Joined: Wed Aug 09, 2023 9:58 am

Re: List Hosts Memberships

Post by sgardil »

AngeloMileto 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.
Hey @AngeloMileto

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?
AngeloMileto
Posts: 39
Joined: Mon Mar 21, 2022 7:53 am

Re: List Hosts Memberships

Post by AngeloMileto »

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?
AngeloMileto
Posts: 39
Joined: Mon Mar 21, 2022 7:53 am

Re: List Hosts Memberships

Post by AngeloMileto »

So noone knows the sql query to run to get the memberships of a given host?
gwesterman
Posts: 222
Joined: Wed Aug 23, 2023 11:29 am

Re: List Hosts Memberships

Post by gwesterman »

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).

Code: Select all

mysql -u <username> -p<password> (no space between -p and password)
2) Select database:

Code: Select all

use nagiosql;
3) View table:

Code: Select all

SELECT * FROM tbl_lnkHostToHostgroup;

Code: Select all

SELECT * FROM tbl_lnkHostToHostgroup WHERE idMaster=<host_id>;
Otherwise, you may have to write a script that parses the hostgroup cfg files and matches hostgroups to hosts.
snapier3
Posts: 144
Joined: Tue Apr 23, 2019 7:12 pm

Re: List Hosts Memberships

Post by snapier3 »

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/

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]
snapier3
Posts: 144
Joined: Tue Apr 23, 2019 7:12 pm

Re: List Hosts Memberships

Post by snapier3 »

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.

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]
Happy Monitoring,
--SN
snapier3
Posts: 144
Joined: Tue Apr 23, 2019 7:12 pm

Re: List Hosts Memberships

Post by snapier3 »

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.

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]
Post Reply