nagios 安装手册

来源:互联网 发布:用户画像 聚类算法 编辑:程序博客网 时间:2024/05/03 10:56

nagios简介

一、使用Nagios的必要性

1、大量的IT基础设施系统管理繁琐、复杂。
2、减少管理和维护成本
3、优化系统、合理利用服务资源

   监控软件局限性:
1、 没有任何工具可以监视您所需的一切内容。 
 2、要让工具完全适应您的需求需要做许多自定义工作。

二、 什么是Nagios
 Nagios—Nagios Ain’t Gonna Insist on Sainthood
 最初项目名字是NetSaint,程序主体由ANSI C编制.
 Nagios是一款开源的免费网络监视工具,其功能强大,灵活性强。能有效监控Windows、Linux和Unix的主机状态,交换机、路由器等网络设置,打印机等,在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。

三、Nagios的结构
 Nagios 可分为核心和插件两个部分。
 Nagios 的核心部分只提供了很少的监控功能,因此要搭建一个完善的IT监控管理系统,用户还需要给Nagios安装相应的插件,插件可以从Nagios官方网站下载,也可以根据实际要求自己编写所需的插件。
 Nagios必须运行在linux/Unix平台下,需要gcc编译器支持,nagios还提供了一个web监控界面。
 

四、  Nagios可实现的功能
    (1)监视本地或者远程主机资源 (内存、进程、磁盘等)
 (2)监视网络服务资源 (HTTP、PING、FTP、SMTP、POP3等)
 (3)允许用户编写自己的插件来监控特定的服务。
 (4)当被监控对象出现异常时,可以通过邮件、短信等方式通知管理人员。
 (5)可以事先定义事件处理程序,当主机或者服务出现故障时自动调用指定的处理程序。
 (6)可以通过web界面来监控各个主机或服务的运行状态。

安装教程

一、环境搭建

主节点:192.168.56.130

从节点:192.168.56.131

iptables和selinux关闭

二、安装准备

1、依赖包检测

rpm -q gcc glibc glibc-devel gd gd-devel openssl-devel xinetd

2、下载相应的软件包

php apache nagios nagios-plugins nrpe

3、创建用户和用户组

useradd nagios -d /home/nagios

4、软件安装位置

nagios /usr/local/nagios/

apache /usr/local/apache

php /usr/local/apache/axps

三、安装编译

1、主节点编译

apache安装

./configure --prefix=/usr/local/apache2

make && make install

php安装

./configure --prefix=/usr/local/apache2/axps

make && make install

nagios安装

./configure --prefix=/usr/local/nagios

make all

make install && make install-init && make install-commandmode && make install-config &&  make install-webconf && make install-exfoliation && make install-classicui

nagios-plugins安装

./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios

make && make install

nrpe安装

./configure 

make all && make install-plugin

为网页登录设置密码和用户名

管理员用户名有效命名看nagios/etc/cgi.cfg里的

default_user_name=nagioaadmin选项,也可以在这里新增管理用户

具体密码存放文件名看/etc/httpd/conf.d/nagios.conf里的

AuthUserFile /usr/local/nagios/etc/htpasswd.users选项

密码设置

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

密码自定义

2、从节点编译

nagios-plugins安装

./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios

make && make install

nrpe安装

./configure ????????

make all && make install-plugin&& make install-daemon && make install-daemon-config && make install-xinetd


三、主节点配置

1、配置文件说明

2、nagios.cfg配置文件

为了方便管理,取消了单个配置文件指定,采用配置存放目录方式进行管理

定义配置文件目录,取消单个配置文件路径。


3、定义监控模块配置文件

vi /usr/local/nagios/etc/templates/weblates.cfg

  1. define contact{
  2.         name                            web-contact         ; 这是联系人模块名字
  3.         service_notification_period     24x7                    ; 服务通知可以随时发送
  4.         host_notification_period        24x7                    ; 主机通知可以随时发送
  5.         service_notification_options    w,u,c,r,f,s             ; send notifications for all service states, flapping events, and scheduled downtime events
  6.         host_notification_options       d,u,r,f,s               ; send notifications for all host states, flapping events, and scheduled downtime events
  7.         service_notification_commands   notify-service-by-email ; 通过电子邮件发送服务通知
  8.         host_notification_commands      notify-host-by-email    ; 通过电子邮件发送主机通知
  9.         register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
  10.         }
  11. define host{
  12.         name                            web-host    ; 该主机模板的名称
  13.         notifications_enabled           1               ; 主机启用通知
  14.         event_handler_enabled           1               ; 主机事件处理程序已启用
  15.         flap_detection_enabled          1               ; 启用瓣检测
  16.         process_perf_data               1               ; 处理性能数据
  17.         retain_status_information       1               ; 保留跨程序重新启动状态信息
  18.         retain_nonstatus_information    1               ; 保留跨程序重新启动无身份信息
  19.         notification_period             24x7            ; 主机全天候随时发送的通知
  20.         register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
  21.         }
  22. define host{
  23.         name                            linux-web-server    ;该主机模板的名称
  24.         use                             web-host    ; 这个模板继承了通用主机模板其他值
  25.         check_period                    24x7            ; 全天候;默认情况下,Linux主机进行检查日以继夜
  26.         check_interval                  5               ; 积极检查主机每隔5分钟
  27.         retry_interval                  1               ; 附表主机检查重试间隔为1分钟
  28.         max_check_attempts              10              ; 检查每个Linux主机10倍(最大)
  29.         check_command                   check-host-alive ; 默认命令检查Linux主机存活
  30.         notification_period             workhours       ;Linux的管理员讨厌被吵醒了,所以我们只在白天通知
  31.                                                         ; 需要注意的是notification_period变量被从覆盖
  32.                                                         ; 这是从一般的主机模板继承了价值!
  33.         notification_interval           2             ; 重发通知,每隔2小时
  34.         notification_options            d,u,r           ; 只发送特定的主机状态通知
  35.         contact_groups                  websadmin       ; 通知被发送到管理员默认联系人
  36.         register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
  37.         }
  38. define service{
  39.         name                            generic-web-service         ; 该服务模板的'名字'
  40.         active_checks_enabled           1                       ; 现役检查启用
  41.         passive_checks_enabled          1                       ; 被动式服务检查启用/接受
  42.         parallelize_check               1                       ; 主动服务检查应并行(禁用这可能导致重大的性能问题)
  43.         obsess_over_service             1                       ; 我们应该迷恋这项服务(如有必要)
  44.         check_freshness                 0                       ; 默认是不检查服务'新鲜感'
  45.   notifications_enabled           1                       ; 服务启用通知
  46.         event_handler_enabled           1                       ; 服务事件处理程序已启用
  47.         flap_detection_enabled          1                       ; 启用瓣检测
  48.         process_perf_data               1                       ; 处理性能数据
  49.         retain_status_information       1                       ; 保留跨程序重新启动状态信息
  50.         retain_nonstatus_information    1                       ; 保留跨程序重新启动无身份信息
  51.         is_volatile                     0                       ; 该服务是不挥发
  52.         check_period                    24x7                    ; 全天候;该服务可以在一天中的任何时间进行检查
  53.         max_check_attempts              3                       ; 重新检查服务最多3次,以确定其最终(硬)状态
  54.         normal_check_interval           3                    ; 检查服务在正常条件下,每10分钟
  55.         retry_check_interval            2                       ; 重新检查服务每两分钟,直到硬状态可以被确定
  56.         contact_groups                  websadmin               ; 通知被发送出去给大家的“管理员”组
  57.         notification_options            w,u,c,r                 ; 发送通知警告(warning),未知的(unknown),关键的(critical),和恢复活动(recovery events)
  58.         notification_interval           4                      ; 再通知有关每隔4小时服务问题
  59.         notification_period             24x7                    ; 全天候;通知可以被发送出去,在任何时间
  60.          register                        0                      ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
  61.         }
  62. define service{
  63.         name                            web-service           ; 该服务模板的'名字
  64.         use                             generic-web-service         ;  使用通用 - 网络服务;从一般服务定义继承默认值
  65.         max_check_attempts              4                       ; 重新检查服务高达4倍,以确定其最终(硬)状态
  66.         normal_check_interval           2                       ; 检查服务在正常条件下,每5分钟
  67.         retry_check_interval            1                       ; 重新检查服务每分钟,直到硬状态可以被确定
  68.         register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
  69.         }


3、定义监控主机和主机组

  1. vi /usr/local/nagios/etc/hosts/webs.cfg
  2. define host{
  3.         use             linux-web-server
  4.         host_name       web
  5.         alias           web-1
  6.         address         192.168.56.131
  7. }
  8. define host{
  9.         use             linux-web-server
  10.         host_name       wab2
  11.         alias           web-2
  12.         address         192.168.56.132
  13. }
  14. define hostgroup{
  15.         hostgroup_name  webs
  16.         alias           webs
  17.         members         web,web2
  18. }

将web和web2的主机加入监控组

4、定义联系人和联系组

vi /usr/local/nagios/etc/contacts/web_contacts.cfg

  1. define contact{
  2.         contact_name                    webadmin
  3.         alias                           webs Admin
  4.         use                             web-contact
  5.         email                           g544132024@163.com
  6.         }


  7. define contactgroup{
  8.         contactgroup_name       websadmin
  9.         alias                   websgroup
  10.         members                 webadmin
  11. }

5、命令监控命令

因为要监控远程主机,需要用到check_nrpe这个命令,所以需要定义后才可以使用

vi /usr/local/nagios/etc/commands/commands.cfg

  1. define command{


  2.        command_name    check_nrpe


  3.        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -t 30 -c $ARG1$


  4.        }


  5. define command{


  6.        command_name    check_nrpe_args


  7.        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -t 30 -c $ARG1$ -a $ARG2$


  8.        }

5、定义监控sever配置

这个是定义远程主机(从节点)的,如果是本地主机(主节点)就把参照localhost.cfg配置文件

vi /usr/local/nagios/etc/server/web_server.cfg

define service{
        use                             web-service         ; Name of service template to use
        host_name                       web
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }
define service{
        use                             web-service
        host_name                       web
        service_description             Root Partition
        check_command                   check_nrpe_args!check_disk!20% 10% /
        }
define service{
        use                             web-service         ; Name of service template to use
        host_name                       web
        service_description             Current Users
        check_command                   check_nrpe_args!check_users!20 50
        }
define service{
        use                             web-service         ; Name of service template to use
        host_name                       web
        service_description             Total Processes
        check_command                   check_nrpe_args!check_procs!250 400 RSZDT
        }

define service{
        use                             web-service         ; Name of service template to use
        host_name                       web
        service_description             Current Load
        check_command                   check_nrpe_args!check_load!5.0,4.0,3.0 10.0,6.0,4.0
        }
define service{
        use                             web-service         ; Name of service template to use
        host_name                       web
        service_description             Swap Usage
        check_command                   check_nrpe_args!check_swap!20 10
        }
define service{
        use                             web-service         ; Name of service template to use
        host_name                       web
        service_description             SSH
        check_command                   check_ssh
        notifications_enabled           0
        }

附上别人的配置文件图:

define host{
       use                     linux-server
       host_name               duangr-2
       alias                   duangr-2
       address                 192.168.56.11
       }
 
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             Host Alive
       check_command                   check-host-alive
       }
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             Users
       check_command                   check_nrpe_args!check_users!5 10
       }
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             CPU
       check_command                   check_nrpe_args!check_load!15,10,5 30,25,20
       }
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             Disk Root
       check_command                   check_nrpe_args!check_disk!20% 10% /
       }
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             Disk /export/home
       check_command                   check_nrpe_args!check_disk!20% 10% /export/home
       }
define service{
      use                             local-service
      host_name                       duangr-2
      service_description             Procs Zombie
      check_command                   check_nrpe_args!check_procs!5 10 Z
      }
define service{
      use                             local-service
      host_name                       duangr-2
      service_description             Procs Total
      check_command                   check_nrpe_args!check_procs_args!"-w400 -c600"
      }
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             Swap Usage
       check_command                   check_nrpe_args!check_swap!20% 10%
       }
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;  下面是一些常用进程的监控,主要是云平台相关进程
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 监控crond进程
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             PS: crond
       check_command                   check_nrpe_args!check_procs_args!"-c1:1 -Ccrond"
       }
;; 监控zookeeper进程
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             PS: QuorumPeerMain
       check_command                   check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.quorum.QuorumPeerMain"
       }
;;监控storm的从节点进程
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             PS: supervisor
       check_command                   check_nrpe_args!check_procs_args!"-c1:1 -Cjava -adaemon.supervisor"
       }
;; 监控storm的主节点进程
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             PS: nimbus
       check_command                   check_nrpe_args!check_procs_args!"-c1:1 -Cjava -adaemon.nimbus"
       }
;; 监控MetaQ进程
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             PS: MetaQ
       check_command                   check_nrpe_args!check_procs_args!"-c1:1 -Cjava -ametamorphosis-server-w"
       }
;; 监控Redis进程
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             PS: redis-server
       check_command                   check_nrpe_args!check_procs_args!"-c1:1 -Credis-server"
       }
;; 监控hadoop主节点NameNode进程
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             PS: NameNode 
       check_command                   check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.namenode.NameNode"
       }
;; 监控hadoop主节点SecondaryNameNode进程
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             PS: SecondaryNameNode
       check_command                   check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.namenode.SecondaryNameNode"
       }
;; 监控hadoop主节点ResourceManager进程
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             PS: ResourceManager
       check_command                   check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.resourcemanager.ResourceManager"
       }
;; 监控hadoop从节点DataNode进程
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             PS: DataNode
       check_command                   check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.datanode.DataNode"
       }
;;监控hadoop从节点NodeManager进程
define service{
       use                             local-service
       host_name                       duangr-2
       service_description             PS: NodeManager
       check_command                   check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.nodemanager.NodeManager"
       }

四.Nrpe详解

1.先用表格列举一下我的监控对象和阀值:

监控对象
监控阀值
 
 
 
 
 
 
主机存活:
check_ping
 
-w 3000.0,80% -c 5000.0,100% -p 5(3000毫秒响应时间内,
丢包率超过80%报警告,5000毫秒响应时间内,丢包率超过
100%报危急,一共发送5个包)
登录用户:
check_user
-w 5 -c 10(w为警告,c为危急)
系统负载:
check_load
-w 15,10,5 -c 30,25,20(1分钟,5分钟,15分钟大于对应
的等待进程数则警告或危急)
磁盘占用率:
check_disk
-w 20% -c 10% -p /(根分区剩余空间为总大小的20%警告,
10%危急,-p后是根分区)
脚本检测磁盘I/O:
check_iostat
-w 5 –c 10 (磁盘I/O的iowait超过5%报警告,超过10%报危急)
检测僵尸进程:
check_zombie _procs
-w 5 -c 10 -s Z(有5个僵尸进程报警告,10个报危急)
检测总进程数:
check_total_procs
-w 150 -c 200(总进程到150个警告,200个报危急)
脚本检测内存剩余:
check_mem
-w 90% -c 95%(内存空闲率90%以上报警告,95%以上报危急)
检测交换分区使用率:
check_swap
-w 20% -c 10%(交换分区剩余空间为总大小的20%警告,
10%危急)
监控服务端口:
check_tcp
-H localhost2 -p 80(主机与对应的端口号)
 
监控页面响应时间:
check_http
-H localhost2 -u http:\/\/localhost2/test.jsp –w 5 –c 10(检查页面,超过5s报警告,超过10s报危急)
脚本检测IP连接数:
check_ips
-w 200 –c 250(IP连接数超过200报警告,超过250报危急)
流量
监控
监控server流量:
Check_traffic
-V 2c -C public -H localhost2 -I 2 -w 12,30 -c 15,35 -M –b(snmp版本,用户,主机,对应网卡,警告阀值,危急阀值)

五、从节点配置
1、配置nrpe文件
vi /usr/local/nagios/etc/nrpe.cfg
  1. log_facility=daemon
  2. pid_file=/var/run/nrpe.pid
  3. server_port=5666
  4. nrpe_user=nagios
  5. nrpe_group=nagios
  6. allowed_hosts=192.168.56.130
  7. dont_blame_nrpe=1
  8. allow_bash_command_substitution=0
  9. debug=0
  10. command_timeout=60
  11. connection_timeout=300
  12. command[check_users]=/usr/local/nagios//libexec/check_users -w $ARG1$ -c $ARG2$
  13. command[check_load]=/usr/local/nagios//libexec/check_load -w $ARG1$ -c $ARG2$
  14. command[check_hda1]=/usr/local/nagios//libexec/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
  15. command[check_zombie_procs]=/usr/local/nagios//libexec/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$
  16. command[check_total_procs]=/usr/local/nagios//libexec/check_procs -w $ARG1$ -c $ARG2$
  17. command[check_users]=/usr/local/nagios//libexec/check_users -w $ARG1$ -c $ARG2$
  18. command[check_load]=/usr/local/nagios//libexec/check_load -w $ARG1$ -c $ARG2$
  19. command[check_disk]=/usr/local/nagios//libexec/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
  20. command[check_procs]=/usr/local/nagios//libexec/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$
  21. command[check_swap]=/usr/local/nagios//libexec/check_swap -w $ARG1$ -c $ARG2$
  22. command[check_ping]=/usr/local/nagios//libexec/check_ping -w $ARG1$ -c $ARG2$
由于硬盘、swap监控需要开启dont_blame_nrpe选项,把它设为1才可以监控硬盘等。

2、运行nrpe守护进程

/usr/local/nagios/bin/nrpe -c /usr/local/nagios/bin/nrpe.cfg -d

在重启xinetd服务: service xinetd restart

3、将nrpe加入系统服务,方便关闭和开启服务

vi /etc/init.d/nrpe

  1. #!/bin/bash
  2. #nrpe server startscript
  3. PATH=$PATH
  4. prog=nrpes
  5. pidfile=${PIDFILE-/var/run/nrpe.pid}
  6. lockfile=${LOCKFILE-/var/lock/subsys/nrpe}
  7. npre_home=/usr/local/nagios/bin/
  8. start_script=$npre_home/nrpestart.sh
  9. stop_script=$npre_home/nrpestop.sh
  10. start(){
  11.         echo -n $"Starting $prog:"
  12.         cd $npre_home
  13.         bash $start_script
  14.         RETVAL=$?
  15.         echo
  16.         [ $RETVAL = 0 ] && touch ${lockfile}
  17.         return $RETVAL
  18. }
  19. stop(){
  20.         echo -n $"Stoping $prog:"
  21.         cd $npre_home
  22.         bash $stop_script
  23.         RETVAL=$?
  24.         echo
  25.         [ $RETVAL =0 ] && rm -f ${lockfile} ${pidfile}
  26. }
  27. case "$1" in
  28.   start)
  29.     start
  30.     ;;
  31.   stop)
  32.     stop
  33.     ;;
  34.   restart)
  35.     stop
  36.     start
  37.     ;;
  38.   *)
  39.     echo $"Usage: $prog {start|stop|restart}"
  40.     exit 1
  41. esac
  42. exit $RETVAL  

vi /usr/local/nagios/bin/nrpestart.sh

  1. #!/bin/bash
  2. #start_script
  3. PATH=$PATH
  4. command_nrpe=/usr/local/nagios/bin/./nrpe
  5. cfg=/usr/local/nagios/etc/nrpe.cfg
  6. $command_nrpe -c $cfg -d

vi /usr/local/nagios/bin/nrpestop.sh

  1. #!/bin/bash
  2. #start_script
  3. PATH=$PATH
  4. pid=`ps -ef | grep nrpe | grep -v grep | awk '{printf $2 "\n"}'`
  5. kill $pid

0 0
原创粉丝点击