Page 1 of 1
Extracting server names tagged to a host template
Posted: Mon Feb 18, 2019 11:46 pm
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,
Re: Extracting server names tagged to a host template
Posted: Tue Feb 19, 2019 11:43 am
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

Re: Extracting server names tagged to a host template
Posted: Tue Feb 19, 2019 4:27 pm
by scottwilkerson
Thanks
@mcapra!
this is probably the easiest way
Re: Extracting server names tagged to a host template
Posted: Tue Feb 19, 2019 10:59 pm
by sajid4u2c
Thanks

, @mcapra .
This thread can be closed.
Re: Extracting server names tagged to a host template
Posted: Wed Feb 20, 2019 7:48 am
by scottwilkerson
sajid4u2c wrote:Thanks

, @mcapra .
This thread can be closed.
Great!
Closing thread