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,
Extracting server names tagged to a host template
Re: Extracting server names tagged to a host template
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:
In action, looking through all my config files on a lab machine that use the "linux-server" template:
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
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
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
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/
https://www.mcapra.com/
-
- 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
Thanks , @mcapra .
This thread can be closed.
This thread can be closed.
-
- 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
Great!sajid4u2c wrote:Thanks , @mcapra .
This thread can be closed.
Closing thread