运用shell脚本批量添加nagios客户端信息的简单实现

[root@v73 shelltest]# cat editnagios.sh 
#!/bin/bash
cat >host.list <<EOF
v76 192.168.156.76
m3 192.168.156.83
EOF

cp /usr/local/nagios/etc/hosts.cfg /usr/local/nagios/etc/hosts.cfg.bak.`date '+%Y%m%d%H%M%S'` && cp /usr/local/nagios/etc/objects/services.cfg /usr/local/nagios/etc/objects/services.cfg.bak.`date '+%Y%m%d%H%M%S'`

exec <host.list
while read line
do
egrep `echo $line|awk '{print $2}'` /usr/local/nagios/etc/hosts.cfg
if [ $? -ne 0 ]; then
echo 'define host {' >>/usr/local/nagios/etc/hosts.cfg
echo '        use                     linux-server' >>/usr/local/nagios/etc/hosts.cfg
echo "        host_name               `echo $line|awk '{print $1}'`" >>/usr/local/nagios/etc/hosts.cfg
echo "        alias                   `echo $line|awk '{print $1}'` " >>/usr/local/nagios/etc/hosts.cfg
echo "        address                 `echo $line|awk '{print $2}'` " >>/usr/local/nagios/etc/hosts.cfg
echo "}" >>/usr/local/nagios/etc/hosts.cfg
echo "" >> /usr/local/nagios/etc/hosts.cfg
fi

egrep `echo $line|awk '{print $1}'` /usr/local/nagios/etc/objects/services.cfg
if
[ $? -ne 0 ]; then
echo 'define service {' >> /usr/local/nagios/etc/objects/services.cfg
echo '        use                     local-service' >> /usr/local/nagios/etc/objects/services.cfg
echo "        host_name               `echo $line|awk '{print $1}'`" >> /usr/local/nagios/etc/objects/services.cfg
echo "        service_description     check-host-alive" >> /usr/local/nagios/etc/objects/services.cfg
echo "        check_command           check-host-alive" >> /usr/local/nagios/etc/objects/services.cfg
echo "}" >> /usr/local/nagios/etc/objects/services.cfg
echo "" >> /usr/local/nagios/etc/objects/services.cfg

echo 'define service {' >> /usr/local/nagios/etc/objects/services.cfg
echo '        use                     local-service' >> /usr/local/nagios/etc/objects/services.cfg
echo "        host_name               `echo $line|awk '{print $1}'`" >> /usr/local/nagios/etc/objects/services.cfg
echo "        service_description     Current Load  " >> /usr/local/nagios/etc/objects/services.cfg
echo "        check_command           check_nrpe!check_load" >> /usr/local/nagios/etc/objects/services.cfg
echo "}" >> /usr/local/nagios/etc/objects/services.cfg
echo "" >> /usr/local/nagios/etc/objects/services.cfg

echo 'define service {' >> /usr/local/nagios/etc/objects/services.cfg
echo '        use                     local-service' >> /usr/local/nagios/etc/objects/services.cfg
echo "        host_name               `echo $line|awk '{print $1}'`" >> /usr/local/nagios/etc/objects/services.cfg
echo "        service_description     Check Disk sda1  " >> /usr/local/nagios/etc/objects/services.cfg
echo "        check_command           check_nrpe!check_sda1" >> /usr/local/nagios/etc/objects/services.cfg
echo "}" >> /usr/local/nagios/etc/objects/services.cfg
echo "" >> /usr/local/nagios/etc/objects/services.cfg

echo 'define service {' >> /usr/local/nagios/etc/objects/services.cfg
echo '        use                     local-service' >> /usr/local/nagios/etc/objects/services.cfg
echo "        host_name               `echo $line|awk '{print $1}'`" >> /usr/local/nagios/etc/objects/services.cfg
echo "        service_description     Check Disk sda2  " >> /usr/local/nagios/etc/objects/services.cfg
echo "        check_command           check_nrpe!check_sda2" >> /usr/local/nagios/etc/objects/services.cfg
echo "}" >> /usr/local/nagios/etc/objects/services.cfg
echo "" >> /usr/local/nagios/etc/objects/services.cfg

fi

done

 

按照前面一篇博文的方式安装好nagios客户端,再到服务器执行这个脚本,这样就能监控到服务器的信息

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页