上篇“自动化安装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

实现步骤:

  1. 添加被监控服务器的机器名到配置文件hosts.cfg
  2. 添加被监控服务器的机器名到所属组的配置文件hostgrps.cfg
  3. 添加被监控服务器的服务类型到配置文件services.cfg
  4. 将nrpe添加到命令配置文件commands.cfg
  5. 将上述配置文件的路径放在nagios的主配置文件nagios.cfg里声明

开始配置:

声明一个变量为nagios安装路径

  1. nagios_dir="/usr/local/nagios" 

一,添加被监控的主机到hosts.cfg

  1. cd $nagios_dir/etc/objects  #进入nagios配置文件所在目录
  2.      [ -f new.host ] && rm -f new.host  #判断new.host文件是否存在,若在则删除旧主机文件
  3.      exec < hosts.list  #hosts.list为手动创建的文件,用于指定主机名和IP
  4.      while read line  #从hosts.list提取主机名和IP并添加到new.host文件
  5.      do 
  6.           echo "define host {" >> new.host 
  7.           echo "     use           linux-server" >> new.host 
  8.           echo "     host_name     `echo $line|awk '{print $1}'`" >> new.host 
  9.           echo "     alias         `echo $line|awk '{print $1}'`" >> new.host 
  10.           echo "     address       `echo $line|awk '{print $2}'`" >> new.host 
  11.           echo "}" >> new.host      
  12.           echo >> new.host 
  13.      done 
  14.      cat new.host >> hosts.cfg  #将做好的主机模板添加到hosts.cfg文件

hosts.list文件需手动创建,格式为“主机名    IP地址”,可以一次添加多个,

若以后有新主机需要增加,则直接在hosts.list里清空旧内容,将新主机信息添加进去

  1. test-machine1    192.168.0.100 
  2. test-machine2    192.168.0.101
  3. test-machine3    192.168.0.102

new.host为生成的主机模板

  1. define host { 
  2.      use           linux-server 
  3.      host_name     test-machine1 
  4.      alias         test-machine1
  5.      address       192.168.0.100

最后将生成好的主机模板添加到nagios所需要的主机文件hosts.cfg,里面都是生成好的所定义的被监控主机模板。

二,添加主机所属组

将被监控主机归到一个监控组里,这里仅生成一个组,其他以此类推

  1. cd $nagios_dir/etc/objects 
  2.     newhosts=`cat hosts.list|awk '{print $1}'|tr '\n' ','`  #提取host.list文件里的主机名
  3.     if [ ! -f hostgrps.cfg ]; then  #判断hostgrps.cfg文件是否存在
  4.          echo "define hostgroup {" >> hostgrps.cfg  #添加hostgrps.cfg文件正文
  5.          echo "     hostgroup_name     lnx-srvs" >> hostgrps.cfg 
  6.          echo "     alias     lnx-srvs" >> hostgrps.cfg 
  7.          echo "     members     $newhosts" >> hostgrps.cfg 
  8.          echo "}" >> hostgrps.cfg 
  9.          sed -i.bak '/members/ s/,$//' hostgrps.cfg  #去掉主机名结尾的逗号
  10.     else 
  11.          sed -i.bak '/members/ s/^.*$/&,'$newhosts'/' hostgrps.cfg #若hostgrps文件已存在,则仅将新主机名追加到members那一列最后
  12.          sed -i.bak '/members/ s/,$//' hostgrps.cfg 
  13.     fi 

最终所生成的hostgrps.cfg文件名格式为

  1. define hostgroup{ 
  2.         hostgroup_name  lnx_srvs 
  3.         alias           lnx_srvs 
  4.         members         test-machine1,test-machine2 

三,将nrpe添加到命令配置文件

  1. echo" 
  2. define command{ 
  3.    command_name   nrpe 
  4.    command_line   $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ 
  5.    }" >> $natios_dir/etc/objects/commands.cfg 

四,添加服务文件

接下来是比较重要的nagios监控的服务文件services.cfg

  1.      cd $nagios_dir/etc/objects 
  2.      newhosts=`cat hosts.list|awk '{print $1}'|tr '\n' ','`  #定义变量newhosts为提取的主机名
  3.      if [ ! -f services.cfg ]; then  #判断services.cfg文件是否存在
  4. echo "  #若不存在则新增模板正文
  5. define service{ 
  6.          use                              remote-service 
  7.          host_name                        '$newhosts'  
  8.          service_description              Current Users On Remote System 
  9.          check_command                    nrpe!check_users  #监控主机当前登录的用户数量
  10.          } 
  11.  
  12. define service{ 
  13.          use                              remote-service 
  14.          host_name                        '$newhosts' 
  15.          service_description              Current System Loads 
  16.          check_command                    nrpe!check_load  #监控系统的负载
  17.          } 
  18.  
  19.  
  20. define service{ 
  21.          use                              remote-service 
  22.          host_name                        '$newhosts' 
  23.          service_description              Zombie Processes On Remote System 
  24.          check_command                    nrpe!check_zombie_procs  #监控主机僵尸进程数量
  25.          } 
  26.  
  27. define service{ 
  28.          use                              remote-service 
  29.          host_name                        '$newhosts' 
  30.          service_description              Total Processes On Remote System 
  31.          check_command                    nrpe!check_total_procs  #监控主机总进程数量
  32.          } 
  33.  
  34. define service{ 
  35.          use                              remote-service 
  36.          host_name                        '$newhosts' 
  37.          service_description              Disk Using Of Remote System 
  38.          check_command                    nrpe!check_disk  #监控磁盘空间
  39.          } 
  40.  
  41. define service{ 
  42.          use                              remote-service 
  43.          host_name                        '$newhosts' 
  44.          service_description              Swap Using Of Remote System 
  45.          check_command                    nrpe!check_swap  #监控交换分区空间
  46.          }" > serverces.cfg 
  47.  
  48.           sed -i.bak '/host_name/ s/,$//' services.cfg  #去掉host_name最后的逗号
  49.      else 
  50.           sed -i.bak '/host_name/ s/^.*$/&,'$newhosts'/' services.cfg  #若services.cfg已存在,则直接追加新主机名即可
  51.           sed -i.bak '/host_name/ s/,$//' services.cfg 
  52.      fi 

五,重启nagios服务

以上配置文件都需在nagios主配置文件nagios.cfg里声明

  1. cfg_file=/usr/local/nagios/etc/objects/hosts.cfg 
  2. cfg_file=/usr/local/nagios/etc/objects/hostgrps.cfg 
  3. cfg_file=/usr/local/nagios/etc/objects/services.cfg 

重启nagios服务

  1. /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 
  2. service nagios restart 

至此,nagios服务器端配置基本完成,剩下的则需要在被监控主机做相关配置。

完整的配置脚本如下:

  1. #!/bin/bash 
  2. #auto configure nagios settings 
  3. #2012-11-28 
  4.  
  5. nagios_dir="/usr/local/nagios" 
  6.  
  7. function Hosts_cfg() 
  8.      cd $nagios_dir/etc/objects 
  9.      [ -f new.host ] && rm -f new.host 
  10.      exec < hosts.list 
  11.      while read line 
  12.      do 
  13.           echo "define host {" >> new.host 
  14.           echo "     use           linux-server" >> new.host 
  15.           echo "     host_name     `echo $line|awk '{print $1}'`" >> new.host 
  16.           echo "     alias         `echo $line|awk '{print $1}'`" >> new.host 
  17.           echo "     address       `echo $line|awk '{print $2}'`" >> new.host 
  18.           echo "}" >> new.host      
  19.           echo >> new.host 
  20.      done 
  21.      cat new.host >> hosts.cfg 
  22.  
  23. function Hostgrps_cfg() 
  24.      cd $nagios_dir/etc/objects 
  25.      newhosts=`cat hosts.list|awk '{print $1}'|tr '\n' ','
  26.      if [ ! -f hostgrps.cfg ]; then 
  27.           echo "define hostgroup {" >> hostgrps.cfg 
  28.           echo "     hostgroup_name     lnx-srvs" >> hostgrps.cfg 
  29.           echo "     alias     lnx-srvs" >> hostgrps.cfg 
  30.           echo "     members     $newhosts" >> hostgrps.cfg 
  31.           echo "}" >> hostgrps.cfg 
  32.           sed -i.bak '/members/ s/,$//' hostgrps.cfg 
  33.      else 
  34.           sed -i.bak '/members/ s/^.*$/&,'$newhosts'/' hostgrps.cfg 
  35.           sed -i.bak '/members/ s/,$//' hostgrps.cfg 
  36.      fi 
  37.  
  38. function Add_command() 
  39.     echo" 
  40. define command{ 
  41.    command_name   nrpe 
  42.    command_line   $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ 
  43.    }" >> $natios_dir/etc/objects/commands.cfg 
  44.  
  45. function Services_cfg() 
  46.      cd $nagios_dir/etc/objects 
  47.      newhosts=`cat hosts.list|awk '{print $1}'|tr '\n' ','
  48.      if [ ! -f services.cfg ]; then 
  49. echo " 
  50. define service{ 
  51.          use                              remote-service 
  52.          host_name                        '$newhosts'  
  53.          service_description              Current Users On Remote System 
  54.          check_command                    nrpe!check_users 
  55.          } 
  56.  
  57. define service{ 
  58.          use                              remote-service 
  59.          host_name                        '$newhosts' 
  60.          service_description              Current System Loads 
  61.          check_command                    nrpe!check_load 
  62.          } 
  63.  
  64.  
  65. define service{ 
  66.          use                              remote-service 
  67.          host_name                        '$newhosts' 
  68.          service_description              Zombie Processes On Remote System 
  69.          check_command                    nrpe!check_zombie_procs 
  70.          } 
  71.  
  72. define service{ 
  73.          use                              remote-service 
  74.          host_name                        '$newhosts' 
  75.          service_description              Total Processes On Remote System 
  76.          check_command                    nrpe!check_total_procs 
  77.          } 
  78.  
  79. define service{ 
  80.          use                              remote-service 
  81.          host_name                        '$newhosts' 
  82.          service_description              Disk Using Of Remote System 
  83.          check_command                    nrpe!check_disk 
  84.          } 
  85.  
  86. define service{ 
  87.          use                              remote-service 
  88.          host_name                        '$newhosts' 
  89.          service_description              Swap Using Of Remote System 
  90.          check_command                    nrpe!check_swap 
  91.          }" > services.cfg 
  92.  
  93.           sed -i.bak '/host_name/ s/,$//' services.cfg 
  94.      else 
  95.           sed -i.bak '/host_name/ s/^.*$/&,'$newhosts'/' services.cfg 
  96.           sed -i.bak '/host_name/ s/,$//' services.cfg 
  97.      fi 
  98.  
  99.  
  100. function Restart_service() 
  101.     /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 
  102.     service nagios restart   
  103.  
  104. case $1 in 
  105. host) 
  106.     Hosts_cfg    
  107.     ;; 
  108. group
  109.     Hostgrps_cfg 
  110.     ;; 
  111. command) 
  112.     Add_command 
  113.     ;; 
  114. service) 
  115.     Services_cfg 
  116.     ;; 
  117. restart) 
  118.     Restart_service 
  119.     ;; 
  120. *) 
  121.     echo "Usage: ./setnagios.sh {host|group|command|service|restart}" 
  122.     ;; 
  123. esac 

需要相应的功能时则运行脚本带上相应参数即可

  1. ./setnagios.sh host    #添加主机  
  2.  
  3. ./setnagios.sh group   #添加组或修改组成员 
  4.  
  5. ./setnagios.sh command  #将nrpe添加到命令配置文件 
  6.  
  7. ./setnagios.sh service #添加服务文件或修改主机成员 
  8.   
  9. ./setnagios.sh restart #重启nagios 服务