上篇“自动化安装nagios”完成了nagios的服务器端的安装,现需要对nagios进行配置,实现基本的监控功能。
参考文章:老男孩的博客
南非蚂蚁的博客
实验环境:
- CentOS 6.3 X86_64, VirtualBox 4.1.14
- 关闭防火墙和SELinux
- nagios版本nagios-3.4.1
- 插件版本nagios-plugins-1.4.16
- nrpe版本nrpe-2.13
实现步骤:
- 添加被监控服务器的机器名到配置文件hosts.cfg
- 添加被监控服务器的机器名到所属组的配置文件hostgrps.cfg
- 添加被监控服务器的服务类型到配置文件services.cfg
- 将nrpe添加到命令配置文件commands.cfg
- 将上述配置文件的路径放在nagios的主配置文件nagios.cfg里声明
开始配置:
声明一个变量为nagios安装路径
- nagios_dir="/usr/local/nagios"
一,添加被监控的主机到hosts.cfg
- cd $nagios_dir/etc/objects #进入nagios配置文件所在目录
- [ -f new.host ] && rm -f new.host #判断new.host文件是否存在,若在则删除旧主机文件
- exec < hosts.list #hosts.list为手动创建的文件,用于指定主机名和IP
- while read line #从hosts.list提取主机名和IP并添加到new.host文件
- do
- echo "define host {" >> new.host
- echo " use linux-server" >> new.host
- echo " host_name `echo $line|awk '{print $1}'`" >> new.host
- echo " alias `echo $line|awk '{print $1}'`" >> new.host
- echo " address `echo $line|awk '{print $2}'`" >> new.host
- echo "}" >> new.host
- echo >> new.host
- done
- cat new.host >> hosts.cfg #将做好的主机模板添加到hosts.cfg文件
hosts.list文件需手动创建,格式为“主机名 IP地址”,可以一次添加多个,
若以后有新主机需要增加,则直接在hosts.list里清空旧内容,将新主机信息添加进去
- test-machine1 192.168.0.100
- test-machine2 192.168.0.101
- test-machine3 192.168.0.102
new.host为生成的主机模板
- define host {
- use linux-server
- host_name test-machine1
- alias test-machine1
- address 192.168.0.100
- }
最后将生成好的主机模板添加到nagios所需要的主机文件hosts.cfg,里面都是生成好的所定义的被监控主机模板。
二,添加主机所属组
将被监控主机归到一个监控组里,这里仅生成一个组,其他以此类推
- cd $nagios_dir/etc/objects
- newhosts=`cat hosts.list|awk '{print $1}'|tr '\n' ','` #提取host.list文件里的主机名
- if [ ! -f hostgrps.cfg ]; then #判断hostgrps.cfg文件是否存在
- echo "define hostgroup {" >> hostgrps.cfg #添加hostgrps.cfg文件正文
- echo " hostgroup_name lnx-srvs" >> hostgrps.cfg
- echo " alias lnx-srvs" >> hostgrps.cfg
- echo " members $newhosts" >> hostgrps.cfg
- echo "}" >> hostgrps.cfg
- sed -i.bak '/members/ s/,$//' hostgrps.cfg #去掉主机名结尾的逗号
- else
- sed -i.bak '/members/ s/^.*$/&,'$newhosts'/' hostgrps.cfg #若hostgrps文件已存在,则仅将新主机名追加到members那一列最后
- sed -i.bak '/members/ s/,$//' hostgrps.cfg
- fi
最终所生成的hostgrps.cfg文件名格式为
- define hostgroup{
- hostgroup_name lnx_srvs
- alias lnx_srvs
- members test-machine1,test-machine2
- }
三,将nrpe添加到命令配置文件
- echo"
- define command{
- command_name nrpe
- command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
- }" >> $natios_dir/etc/objects/commands.cfg
四,添加服务文件
接下来是比较重要的nagios监控的服务文件services.cfg
- cd $nagios_dir/etc/objects
- newhosts=`cat hosts.list|awk '{print $1}'|tr '\n' ','` #定义变量newhosts为提取的主机名
- if [ ! -f services.cfg ]; then #判断services.cfg文件是否存在
- echo " #若不存在则新增模板正文
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Current Users On Remote System
- check_command nrpe!check_users #监控主机当前登录的用户数量
- }
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Current System Loads
- check_command nrpe!check_load #监控系统的负载
- }
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Zombie Processes On Remote System
- check_command nrpe!check_zombie_procs #监控主机僵尸进程数量
- }
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Total Processes On Remote System
- check_command nrpe!check_total_procs #监控主机总进程数量
- }
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Disk Using Of Remote System
- check_command nrpe!check_disk #监控磁盘空间
- }
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Swap Using Of Remote System
- check_command nrpe!check_swap #监控交换分区空间
- }" > serverces.cfg
- sed -i.bak '/host_name/ s/,$//' services.cfg #去掉host_name最后的逗号
- else
- sed -i.bak '/host_name/ s/^.*$/&,'$newhosts'/' services.cfg #若services.cfg已存在,则直接追加新主机名即可
- sed -i.bak '/host_name/ s/,$//' services.cfg
- fi
五,重启nagios服务
以上配置文件都需在nagios主配置文件nagios.cfg里声明
- cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
- cfg_file=/usr/local/nagios/etc/objects/hostgrps.cfg
- cfg_file=/usr/local/nagios/etc/objects/services.cfg
重启nagios服务
- /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
- service nagios restart
至此,nagios服务器端配置基本完成,剩下的则需要在被监控主机做相关配置。
完整的配置脚本如下:
- #!/bin/bash
- #auto configure nagios settings
- #2012-11-28
- nagios_dir="/usr/local/nagios"
- function Hosts_cfg()
- {
- cd $nagios_dir/etc/objects
- [ -f new.host ] && rm -f new.host
- exec < hosts.list
- while read line
- do
- echo "define host {" >> new.host
- echo " use linux-server" >> new.host
- echo " host_name `echo $line|awk '{print $1}'`" >> new.host
- echo " alias `echo $line|awk '{print $1}'`" >> new.host
- echo " address `echo $line|awk '{print $2}'`" >> new.host
- echo "}" >> new.host
- echo >> new.host
- done
- cat new.host >> hosts.cfg
- }
- function Hostgrps_cfg()
- {
- cd $nagios_dir/etc/objects
- newhosts=`cat hosts.list|awk '{print $1}'|tr '\n' ','`
- if [ ! -f hostgrps.cfg ]; then
- echo "define hostgroup {" >> hostgrps.cfg
- echo " hostgroup_name lnx-srvs" >> hostgrps.cfg
- echo " alias lnx-srvs" >> hostgrps.cfg
- echo " members $newhosts" >> hostgrps.cfg
- echo "}" >> hostgrps.cfg
- sed -i.bak '/members/ s/,$//' hostgrps.cfg
- else
- sed -i.bak '/members/ s/^.*$/&,'$newhosts'/' hostgrps.cfg
- sed -i.bak '/members/ s/,$//' hostgrps.cfg
- fi
- }
- function Add_command()
- {
- echo"
- define command{
- command_name nrpe
- command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
- }" >> $natios_dir/etc/objects/commands.cfg
- }
- function Services_cfg()
- {
- cd $nagios_dir/etc/objects
- newhosts=`cat hosts.list|awk '{print $1}'|tr '\n' ','`
- if [ ! -f services.cfg ]; then
- echo "
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Current Users On Remote System
- check_command nrpe!check_users
- }
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Current System Loads
- check_command nrpe!check_load
- }
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Zombie Processes On Remote System
- check_command nrpe!check_zombie_procs
- }
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Total Processes On Remote System
- check_command nrpe!check_total_procs
- }
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Disk Using Of Remote System
- check_command nrpe!check_disk
- }
- define service{
- use remote-service
- host_name '$newhosts'
- service_description Swap Using Of Remote System
- check_command nrpe!check_swap
- }" > services.cfg
- sed -i.bak '/host_name/ s/,$//' services.cfg
- else
- sed -i.bak '/host_name/ s/^.*$/&,'$newhosts'/' services.cfg
- sed -i.bak '/host_name/ s/,$//' services.cfg
- fi
- }
- function Restart_service()
- {
- /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
- service nagios restart
- }
- case $1 in
- host)
- Hosts_cfg
- ;;
- group)
- Hostgrps_cfg
- ;;
- command)
- Add_command
- ;;
- service)
- Services_cfg
- ;;
- restart)
- Restart_service
- ;;
- *)
- echo "Usage: ./setnagios.sh {host|group|command|service|restart}"
- ;;
- esac
需要相应的功能时则运行脚本带上相应参数即可
- ./setnagios.sh host #添加主机
- ./setnagios.sh group #添加组或修改组成员
- ./setnagios.sh command #将nrpe添加到命令配置文件
- ./setnagios.sh service #添加服务文件或修改主机成员
- ./setnagios.sh restart #重启nagios 服务