前言
随着业务的增加,我们所要监控的remote_linux数越来越多。这样我们可以一次性所要添加监控的主机数可能为10台,20台,甚至是百台上千台主机。
如果通过我们手动添加显然效率低下,甚至还会出错。这里就给出一个简单脚本,通过给定的hosts.list文件,来自动生成hosts.cfg.
文件
hosts_list
[root@nagios_scripts nagios_scripts]# for i in `seq 2 9`;do echo "lnmp_$i 10.0.0.$i">>hosts_list;done
[root@nagios_scripts nagios_scripts]# cat hosts.list
lnmp_2 10.0.0.2
lnmp_3 10.0.0.3
lnmp_4 10.0.0.4
lnmp_5 10.0.0.5
lnmp_6 10.0.0.6
lnmp_7 10.0.0.7
lnmp_8 10.0.0.8
lnmp_9 10.0.0.9
nagios_create_hosts.sh
[root@nagios_scripts nagios_scripts]# cat nagios_create_hosts.sh
#!/bin/sh
HOSTFILE="/usr/local/nagios/etc/services"
exec <hosts.list
while read line
do
cat >>$HOSTFILE/hosts.cfg<<EOF
#host:`echo $line|awk '{print $1}'` ip:`echo $line|awk '{print $2}'`
define host{
use linux-server
host_name `echo $line|awk '{print $1}'`
alias `echo $line|awk '{print $1}'`
address `echo $line|awk '{print $2}'`
}
EOF
done
执行结果
[root@nagios_scripts nagios_scripts]# sh nagios_create_hosts.sh
[root@nagios_scripts nagios_scripts]# cat /usr/local/nagios/etc/services/hosts.cfg
#host:lnmp_2 ip:10.0.0.2
define host{
use linux-server
host_name lnmp_2
alias lnmp_2
address 10.0.0.2
}
#host:lnmp_3 ip:10.0.0.3
define host{
use linux-server
host_name lnmp_3
alias lnmp_3
address 10.0.0.3
}
#host:lnmp_4 ip:10.0.0.4
define host{
use linux-server
host_name lnmp_4
alias lnmp_4
address 10.0.0.4
}
#host:lnmp_5 ip:10.0.0.5
define host{
use linux-server
host_name lnmp_5
alias lnmp_5
address 10.0.0.5
}
#host:lnmp_6 ip:10.0.0.6
define host{
use linux-server
host_name lnmp_6
alias lnmp_6
address 10.0.0.6
}
#host:lnmp_7 ip:10.0.0.7
define host{
use linux-server
host_name lnmp_7
alias lnmp_7
address 10.0.0.7
}
#host:lnmp_8 ip:10.0.0.8
define host{
use linux-server
host_name lnmp_8
alias lnmp_8
address 10.0.0.8
}
#host:lnmp_9 ip:10.0.0.9
define host{
use linux-server
host_name lnmp_9
alias lnmp_9
address 10.0.0.9
}
转载于:https://blog.51cto.com/chboy/1719170