交互式添加nagios主机和服务脚本
搭建好nagios服务器之后,剩下的工作其实就是将欲监控的主机和服务添加到配置文件中,这过程其实有很多重复性的工作,所以我写了个交互式脚本,可以一步到位。
脚本思路分析:
首先分析下主机和服务的配置:
主机:
definehost{
uselinux-server,hosts-pnp
host_nameclient
aliasclient
address192.168.56.105
}
服务:
defineservice{
usegeneric-service,services-pnp
host_nameclient
service_descriptioncheck_load
check_commandcheck_nrpe!check_load
max_check_attempts5
normal_check_interval1
}
注:上面红色部分为变化的部分,其余的都是固定的。也就是说需要将红色部分设置成变量。
接下来的问题就是配置服务的时候,如何一次性读入多个服务,实现一步到位。也就是说,我可以一次性添加多个服务,而无需重复性的运行脚本。下面我是通过数组来实现的。
脚本如下:
添加主机:
#!/bin/bashecho "Please input the infomations of the server you want to set."read -p "hostname:" HNAMEread -p "alias:" ALIASread -p "IP:" IPcat << EOF > /usr/local/nagios/etc/hosts/${HNAME}.cfgdefine host{use linux-server,hosts-pnphost_name ${HNAME}alias ${ALIAS}address ${IP}}EOFservice nagios reload
添加服务:
#!/bin/bashread -p "Please input your hostname :" HNAMEread -p "please input the number of the services you want to set :" necho "Please input the services' name :"read -a SHELLfor ((i=0;i<=${n}-1;i++))doecho "${SHELL[$i]}" >> /tmp/name.txtdonewhile read linedocat << EOF >> /usrl/local/nagios/etc/services/${HNAME}.cfgdefine service{use generic-service,services-pnphost_name ${HNAME}service_description check_${line}check_command check_nrpe!check_${line}max_check_attempts 5normal_check_interval 1}EOFecho "$line"done < /tmp/name.txtrm -rf /tmp/name.txtservice nagios reload
运行结果如下:(注意:下面蓝色部分为交互式输入部分)
添加主机:
[root@localhostnagios]#./autochost.sh
Pleaseinputtheinfomationsoftheserveryouwanttoset.
hostname:slave3
alias:slave3
IP:192.168.56.110
Runningconfigurationcheck...done.
Reloadingnagiosconfiguration...done
[root@localhostnagios]#catetc/hosts/slave3.cfg
definehost{
uselinux-server,hosts-pnp
host_nameslave3
aliasslave3
address192.168.56.110
}
添加服务:
[root@localhostnagios]#./autocservice.sh
Pleaseinputyourhostname:slave3
pleaseinputthenumberoftheservicesyouwanttoset:4
Pleaseinputtheservices'name:
httpsshmysqlftp
[root@localhostnagios]#catetc/services/slave3.cfg
defineservice{
usegeneric-service,services-pnp
host_nameslave3
service_descriptioncheck_http
check_commandcheck_nrpe!check_http
max_check_attempts5
normal_check_interval1
}
defineservice{
usegeneric-service,services-pnp
host_nameslave3
service_descriptioncheck_ssh
check_commandcheck_nrpe!check_ssh
max_check_attempts5
normal_check_interval1
}
defineservice{
usegeneric-service,services-pnp
host_nameslave3
service_descriptioncheck_mysql
check_commandcheck_nrpe!check_mysql
max_check_attempts5
normal_check_interval1
}
defineservice{
usegeneric-service,services-pnp
host_nameslave3
service_descriptioncheck_ftp
check_commandcheck_nrpe!check_ftp
max_check_attempts5
normal_check_interval1
}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。