Extracting server names tagged to a host template

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
sajid4u2c
Posts: 46
Joined: Thu Mar 15, 2018 10:55 am

Extracting server names tagged to a host template

Post by sajid4u2c »

Hi,

I have a requirement wherein I need to get the list of hosts tagged to host template.

Suppose I have added "host_template_test_servers" as the host template for around 100 servers and keep on adding few more servers with the same template.

Then after some days, we may need the total list of servers which are using this template "host_template_test_servers"

I have gone through all the NagiosXI GUI options but did not find any help.

Solution through command line in also welcome. Please suggest.

NagiosXI version: 5.2.7

Regards,
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Extracting server names tagged to a host template

Post by mcapra »

You could probably parse the config files. Or query the CCM database (nagiosql) directly. Or daisy-chain some REST API calls. I can think of several solutions to this problem.

If you were parsing the config files, looking for all objects (not just host objects) with the use host_template_test_servers directive, here's a totally imperfect one-liner I threw together for that:

Code: Select all

template='host_template_test_servers'; cat /usr/local/nagios/etc/nagios.cfg | grep -Pzo '((cfg_file)|(cfg_dir))=.*' | while read -r line ; do grep -Pzor "(use.*$template)" $(echo $line | sed -e "s/cfg_.*=//g"); done
In action, looking through all my config files on a lab machine that use the "linux-server" template:

Code: Select all

[root@capra_nag etc]# template='linux-server'; cat /usr/local/nagios/etc/nagios.cfg | grep -Pzo '((cfg_file)|(cfg_dir))=.*' | while read -r line ; do grep -Pzor "(use.*$template)" $(echo $line | sed -e "s/cfg_.*=//g"); done
/usr/local/nagios/etc/hosts/CENESPROD01.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/CENESPROD02.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/CENESPROD03.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/localhost.dep:use                     linux-server
/usr/local/nagios/etc/hosts/KIBPROD00.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/ESMARVELPROD00.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/PRODHERMES1.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/PRODHERMES2.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/PRODHERMES3.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/COLPROD02.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/COLPROD03.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/PRODDELPHISCRIPTS.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/COLPROD00.bak:use                     linux-server
/usr/local/nagios/etc/hosts/COLPROD01.bak:use                     linux-server
/usr/local/nagios/etc/hosts/PRODDATAMART.cfg:use                     linux-server
/usr/local/nagios/etc/hosts/CENESPROD00.cfg:use                     linux-server
As the Nagios XI CCM generated config files are one-to-one for each host object, you should get a single line with hostnames in the filename. Run that through another sed for a clean list of hosts, or your favorite text editor with some find/replace.

grep, sed, and awk are great tools to learn if you spend a good chunk of time in Linux terminals :)
Former Nagios employee
https://www.mcapra.com/
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Extracting server names tagged to a host template

Post by scottwilkerson »

Thanks @mcapra!

this is probably the easiest way
Former Nagios employee
Creator:
ahumandesign.com
enneagrams.com
sajid4u2c
Posts: 46
Joined: Thu Mar 15, 2018 10:55 am

Re: Extracting server names tagged to a host template

Post by sajid4u2c »

Thanks :) , @mcapra .
This thread can be closed.
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Extracting server names tagged to a host template

Post by scottwilkerson »

sajid4u2c wrote:Thanks :) , @mcapra .
This thread can be closed.
Great!

Closing thread
Former Nagios employee
Creator:
ahumandesign.com
enneagrams.com
Locked