设为首页 收藏本站
查看: 889|回复: 0

【nagios 二】nagios配置篇

[复制链接]

尚未签到

发表于 2019-1-15 09:36:02 | 显示全部楼层 |阅读模式
  上篇“自动化安装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 &quot;define host {&quot; >> new.host
  •           echo &quot;     use           linux-server&quot; >> new.host
  •           echo &quot;     host_name     `echo $line|awk '{print $1}'`&quot; >> new.host
  •           echo &quot;     alias         `echo $line|awk '{print $1}'`&quot; >> new.host
  •           echo &quot;     address       `echo $line|awk '{print $2}'`&quot; >> new.host
  •           echo &quot;}&quot; >> 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 &quot;define hostgroup {&quot; >> hostgrps.cfg                #添加hostgrps.cfg文件正文
  •          echo &quot;     hostgroup_name     lnx-srvs&quot; >> hostgrps.cfg
  •          echo &quot;     alias     lnx-srvs&quot; >> hostgrps.cfg
  •          echo &quot;     members     $newhosts&quot; >> hostgrps.cfg
  •          echo &quot;}&quot; >> 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&quot;
  • define command{
  •    command_name   nrpe
  •    command_line   $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
  •    }&quot; >> $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 &quot;                                                        #若不存在则新增模板正文
  • 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   #监控交换分区空间
  •          }&quot; > 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=&quot;/usr/local/nagios&quot;

  • function Hosts_cfg()
  • {
  •      cd $nagios_dir/etc/objects
  •      [ -f new.host ] && rm -f new.host
  •      exec < hosts.list
  •      while read line
  •      do
  •           echo &quot;define host {&quot; >> new.host
  •           echo &quot;     use           linux-server&quot; >> new.host
  •           echo &quot;     host_name     `echo $line|awk '{print $1}'`&quot; >> new.host
  •           echo &quot;     alias         `echo $line|awk '{print $1}'`&quot; >> new.host
  •           echo &quot;     address       `echo $line|awk '{print $2}'`&quot; >> new.host
  •           echo &quot;}&quot; >> 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 &quot;define hostgroup {&quot; >> hostgrps.cfg
  •           echo &quot;     hostgroup_name     lnx-srvs&quot; >> hostgrps.cfg
  •           echo &quot;     alias     lnx-srvs&quot; >> hostgrps.cfg
  •           echo &quot;     members     $newhosts&quot; >> hostgrps.cfg
  •           echo &quot;}&quot; >> 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&quot;
  • define command{
  •    command_name   nrpe
  •    command_line   $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
  •    }&quot; >> $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 &quot;
  • 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
  •          }&quot; > 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 &quot;Usage: ./setnagios.sh {host|group|command|service|restart}&quot;
  •     ;;
  • esac

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


  • ./setnagios.sh host    #添加主机  

  • ./setnagios.sh group   #添加组或修改组成员

  • ./setnagios.sh command  #将nrpe添加到命令配置文件

  • ./setnagios.sh service #添加服务文件或修改主机成员
  •   
  • ./setnagios.sh restart #重启nagios 服务  




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-663444-1-1.html 上篇帖子: Nagios安装与配置详解 下篇帖子: 【nagios 三】 nagios被监控端配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表