脚本如下:
添加主机:
#!/bin/bash
echo "Please input the infomations of the server you want to set."
read -p "hostname:" HNAME
read -p "alias:" ALIAS
read -p "IP:" IP
cat /usr/local/nagios/etc/hosts/${HNAME}.cfg
define host{
use linux-server,hosts-pnp
host_name ${HNAME}
alias ${ALIAS}
address ${IP}
}
EOF
service nagios reload
添加服务:
#!/bin/bash
read -p "Please input your hostname :" HNAME
read -p "please input the number of the services you want to set :" n
echo "Please input the services' name :"
read -a SHELL
for ((i=0;i> /tmp/name.txt
done
while read line
do
cat > /usrl/local/nagios/etc/services/${HNAME}.cfg
define service{
use generic-service,services-pnp
host_name ${HNAME}
service_description check_${line}
check_command check_nrpe!check_${line}
max_check_attempts 5
normal_check_interval 1
}
EOF
echo "$line"
done < /tmp/name.txt
rm -rf /tmp/name.txt
service nagios reload
运行结果如下:(注意:下面蓝色部分为交互式输入部分)
添加主机:
[root@localhost nagios]# ./autochost.sh
Please input the infomations of the server you want to set.
hostname:slave3
alias:slave3
IP:192.168.56.110
Running configuration check...done.
Reloading nagios configuration...done
[root@localhost nagios]# cat etc/hosts/slave3.cfg
define host{
use linux-server,hosts-pnp
host_name slave3
alias slave3
address 192.168.56.110
}
添加服务:
[root@localhost nagios]# ./autocservice.sh
Please input your hostname :slave3
please input the number of the services you want to set :4
Please input the services' name :
http ssh mysql ftp