Page 1 of 1
List Hosts Memberships
Posted: Wed Oct 30, 2024 8:33 am
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.
Re: List Hosts Memberships
Posted: Wed Oct 30, 2024 9:25 am
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?
Re: List Hosts Memberships
Posted: Wed Oct 30, 2024 10:40 am
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?
Re: List Hosts Memberships
Posted: Fri Nov 01, 2024 9:52 am
by AngeloMileto
So noone knows the sql query to run to get the memberships of a given host?
Re: List Hosts Memberships
Posted: Fri Nov 01, 2024 11:54 am
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:
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.
Re: List Hosts Memberships
Posted: Sat Nov 02, 2024 10:52 am
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]
Re: List Hosts Memberships
Posted: Tue Nov 05, 2024 2:42 pm
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
Re: List Hosts Memberships
Posted: Thu Nov 07, 2024 2:26 pm
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]
Re: List Hosts Memberships
Posted: Tue Aug 26, 2025 7:38 am
by AngeloMileto
I resorted to PowerShell - since we were doing other work/checks there anyway - and copied all of the .cfg files local to do the work/lookups.
# NOTE: Just in case there are any typos in here - I can't copy directly from our system so I had to retype this code snippet here.
Code: Select all
# First get a list of all the hosts contained in the .cfg files
$NagiosHosts=@()
$FileCnt=(Get-ChildItem .\CNCAAD\Configs\nagios\etc\hosts\).Count
$cnt=1
ForEach ($tmpFile in (Get-ChildItem .\CNCAAD\Configs\nagios\etc\hosts\))
{
Write-Progress -Activity "Processing Nagios Hosts Files..." -CurrentOperation "$($tmpFile)" -PercentComplete (($cnt / $FileCnt*100))
$tmpHost=New-Object PSObject -Property @{HostName=$(Get-Content $tmpFile.FullName | FindStr host_name | Where-Object {$_ -notlike "#*"} | Where-Object {($_.SubString($_.IndexOf("host_name"))).TrimStart("host_name").Trim().Split(" ")[0]});
HostGroups=""}
$NagiosHosts = $NagiosHosts + $tmpHost
$cnt = $cnt +1
}
Write-Progress -Activity "Complete" -Completed # This clears the progress bar
# Now search for every host in the hostgroups.cfg to be able to report any hosts not in a group.
$HostCnt=$($NagiosHosts.Count)
$cnt=1
ForEach ($tmpHost in $NagiosHosts)
{
Write-Progress -Activity "Processing Nagios Hosts..." -CurrentOperation "$($tmpHost.HostName)" -PercentComplete (($cnt / $HostCnt*100))
$tmpHost.HostGroups=$(Get-Content ".\CNCAAD\Configs\nagios\etc\hostgroups.cfg" | Select-String -Pattern $tmpHost.HostName -Context 2 | FindStr.exe hostgroup_name | ForEach-Object {($_ -split '\s+')[2]}) -join ", "
$cnt = $cnt +1
}
Write-Progress -Activity "Completed" -Completed #This clears the progress bar
# Now compare/list any hosts not a member of the "*site*" group.
$MissingSiteGroup = @($NagiosHosts | Where-Object {$_.HostGroups -notlike "*site*"})
[code]
From there you can compare whatever you need based on your group memberships/requirements. I also do a check for OS group membership but your organization is probably different.