口十目童吧 关注:10贴子:496

【技术贴】搭建nagios监控系统

只看楼主收藏回复

转自linux吧 ,原帖地址: http://tieba.baidu.com/p/3230029862


1楼2015-08-05 11:04回复

    Nagios监控工作原理如下图,Nagios通过NRPE来获取远程主机各种信息。
    Nagios通过NRPE来远程管理服务:
    1. nagios执行安装的check_nrpe插件,并告诉check_nrpe去检测哪些服务。
    2. 通过SSL,check_nrpe连接到远端主机上的nrpe daemon
    3. NRPE运行本地的各种插件去检测本地的主机状态和服务(check_disk,check_cpu)。
    4. NRPE把检测到的结果回传给check_nrpe,check_nrpe再把结果送到nagios状态队列中
    5. Nagios依次读取队列中的信息,在把结果显示出来。
    纯手工打印。如果有错误还请指出来。阿里嘎多。
    这个图其实很明确的介绍了nagios的工作原理,刚接触nagios的时候我压根不看的,因为看也看不懂。知道个大概就行,等你真正会的时候,在回过头来理解就很简单了。


    4楼2015-08-05 11:05
    收起回复

      这个呢,就是我们需要实现的监控目标。


      5楼2015-08-05 11:06
      回复
        在安装之前,我先来谈谈自己对nagios的感受吧。
        虽然说nagios是监控系统,但是个人觉得它最厉害的应该是它的及时告警功能,用户可以自行的设置短信或者邮件等告警功能,帮助工程师第一时间发现问题并解决问题。
        这个也可能是它能脱颖而出的所在吧。


        7楼2015-08-05 11:07
        回复
          下面我们开始为搭建nagios服务器搭建环境吧。
          安装基础支持套件:
          [root@mickey ~]# yum install gcc*
          Loaded plugins: rhnplugin
          This system is not registered with RHN.
          RHN support will be disabled.
          myyum | 3.7 kB 00:00
          Setting up Install Process
          Package gcc-4.4.4-13.el6.x86_64 already installed and latest version
          Package gcc-objc-4.4.4-13.el6.x86_64 already installed and latest version
          Package gcc-gfortran-4.4.4-13.el6.x86_64 already installed and latest version
          Package gcc-gnat-4.4.4-13.el6.x86_64 already installed and latest version
          Package gcc-objc++-4.4.4-13.el6.x86_64 already installed and latest version
          Package gcc-java-4.4.4-13.el6.x86_64 already installed and latest version
          Package gcc-c++-4.4.4-13.el6.x86_64 already installed and latest version
          Nothing to do
          因为nagios是源码编译安装所以需要GCC套件,具体用到那几个,大家可以自行去查,不过这样是最保险的做法。原谅我吧。
          这个是通过yum源安装,至于yum服务器的搭建,很简单。不做介绍。
          [root@mickey ~]# yum install openssl*
          Loaded plugins: rhnplugin
          This system is not registered with RHN.
          RHN support will be disabled.
          Setting up Install Process
          Package openssl-1.0.0-4.el6.x86_64 already installed and latest version
          Package openssl-devel-1.0.0-4.el6.x86_64 already installed and latest version
          Package openssl098e-0.9.8e-17.el6.x86_64 already installed and latest version
          Nothing to do
          因为我们nagios需要使用ssl加密通道方式进行监控,所以这个是必不可少的。
          [root@mickey ~]# yum install httpd
          Loaded plugins: rhnplugin
          This system is not registered with RHN.
          RHN support will be disabled.
          Setting up Install Process
          Package httpd-2.2.15-5.el6.x86_64 already installed and latest version
          Nothing to do
          当然apache是必不可少的,因为我们需要在本地使用web查看监控状态。PHP也就附加装上。这里就不介绍了。都是yum 傻瓜式安装。


          8楼2015-08-05 11:07
          回复
            [root@mickey nagios]# useradd nagios
            useradd: user *nagios* already exists
            首先这里,我们需要新建nagios用户,无需设置密码。因为楼楼已经建立过了,所以会有这样的提示。
            [root@mickey nagios]# mkdir /usr/local/nagios
            [root@mickey nagios]#
            楼楼决定把nagios方面的软件包和插件安装到这个路径里面。
            [root@mickey nagios]# chown -R nagios.nagios /usr/local/nagios/
            [root@mickey nagios]#
            然后把nagios所有权交给nagios用户。
            至于chown -R 的用法: -R, --recursive operate on files and directories recursively
            对了,我所使用到的nagios软件包和插件,如果有需要可以在官网下载。当然也可以问我要噢。请叫我好人。
            -------------------------------
            这里创建用户的时候加上-s /bin/nologin 防止用户登录,处于安全考虑,因为一旦黑客获得了nagios的权限,就能登录系统了,非常危险
            报错的话百度就可以解决了,-s还是要加的,你可以在/etc/passwd中查看,公司用的是vsphere吧


            9楼2015-08-05 11:08
            回复
              [root@mickey nagios]# tar xf nagios-3.4.3.tar.gz
              [root@mickey nagios]# cd nagios
              [root@mickey nagios]# ./configure --prefix=/usr/local/nagios/
              上面的命令意思是:解压nagios并进行检查编译,--prefix的意思是将nagios安装到该目录下。
              检测完成。
              Review the options above for accuracy. If they look okay,
              type *make all* to compile the main program and CGIs.
              提示需要make all。
              [root@mickey nagios]# make all
              我们就满足它。这一步就开始编译,完成之后会有提示。
              If the main program and CGIs compiled without any errors, you
              can continue with installing Nagios as follows (type *make*
              without any arguments for a list of all possible options):
              make install
              - This installs the main program, CGIs, and HTML files
              make install-init
              - This installs the init script in /etc/rc.d/init.d
              make install-commandmode
              - This installs and configures permissions on the
              directory for holding the external command file
              make install-config
              - This installs *SAMPLE* config files in /usr/local/nagios//etc
              You*ll have to modify these sample files before you can
              use Nagios. Read the HTML documentation for more info
              on doing this. Pay particular attention to the docs on
              object configuration files, as they determine what/how
              things get monitored!
              make install-webconf
              - This installs the Apache config file for the Nagios
              web interface
              make install-exfoliation
              - This installs the Exfoliation theme for the Nagios
              web interface
              make install-classicui
              - This installs the classic theme for the Nagios
              web interface
              看这个我们就知道怎么安装的了。
              首先,
              [root@mickey nagios]# make install
              [root@mickey nagios]# make install-init
              [root@mickey nagios]# make install-commandmode
              [root@mickey nagios]# make install-config
              [root@mickey nagios]# make install-webconf
              这几条命令的具体介绍上面写的应该很详细了,如果英文不好,大家可以安装英文之类的软件反应,千万不要忘记英语的学习。
              这样,我们的nagios就装好了。是不是粉简单?
              当然,这只是仅仅安装好了nagios,下一步我们安装nagios的插件。


              10楼2015-08-05 11:08
              回复
                安装nagios-plugin
                [root@mickey nagios]# tar xf nagios-plugins-1.5.tar.gz
                [root@mickey nagios]# cd nagios-plugins-1.5
                [root@mickey nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios/ --with-nagios-user=nagios --with-nagios-group=nagios
                最后一条命令介绍下,--prefix前面已经介绍过了,就是将插件安装到该目录,--with-nagios-user 和--with-nagios-group 这个两个命令,大家也都能猜到。
                定义该文件的所有者为nagios,所有组为nagios。
                [root@mickey nagios-plugins-1.5]# make all
                这个中间的等待时间可能有点长。
                [root@mickey nagios-plugins-1.5]# make install
                这样我们的naigos-plugin就装好了。一样很简单是吧?


                11楼2015-08-05 11:08
                回复
                  [root@mickey nagios-plugins-1.5]# cd /usr/local/nagios/
                  [root@mickey nagios]# ls
                  bin etc libexec sbin share var
                  然后我们就可以切换到安装目录下,如果你目录下有这样几个目录,那恭喜你咯,nagios安装完毕。这几个目录都很熟悉吧?具体的解释与根目录下一样。libexec当然是插件位置啦。
                  [root@mickey nagios]# cd libexec/
                  ls一下就会看到很多check_*的二进制文件,而我们nagios就是调用这些插件来进行监控的。现在思路应该慢慢的清晰起来了吧?
                  然后我们这里主要是查看下配置文件。这个不用说,大家都是知道是在etc下面。
                  [root@mickey nagios]# cd etc/
                  [root@mickey etc]# ls
                  cgi.cfg nagios.cfg objects resource.cfg
                  具体的介绍如下:


                  12楼2015-08-05 11:08
                  回复
                    看了上面配置文件大家可能觉得很凌乱,我当时也是呀。不过现在先知道有这些东西就好。
                    先不管这些,现在nagios都装好了,我们当然要看看它长什么样子对吧?
                    这里我们先不要着急,先给apache创建一个验证的文件。
                    [root@mickey etc]# htpasswd -c /usr/local/nagios/etc/nagios.users nagiosadmin
                    New password:
                    Re-type new password:
                    Adding password for user nagiosadmin
                    这一步请大家跟我一样创建一个nagiosadmin这个用户。
                    [root@mickey etc]# htpasswd -c /usr/local/nagios//etc/htpasswd.users nagiosadmin
                    New password:
                    Re-type new password:
                    Adding password for user nagiosadmin
                    /usr/local/nagios//etc/htpasswd.users 注意该文件生成的路径位置。切记。
                    [root@mickey etc]# service httpd start
                    Starting httpd: [ OK ]
                    [root@mickey etc]# service nagios start
                    Starting nagios: done.
                    然后启动apache和nagios服务。

                    看到这个,就恭喜你咯,输入nagiosadmin 和密码。

                    这个就是nagios的庐山真面目啦。。。是不是很厉害的样子。


                    13楼2015-08-05 11:08
                    回复
                      好了,我们的nagios已经搭建起来了。是不是更加有自信了。。

                      左边有很多的选项,这里就不一一介绍了。大家都能看懂。这里我们点开services。
                      是不是看到localhost已经被监控到了。
                      可能你会很惊讶,到目前为止,我们只是搭建好了nagios环境并没有进行配置啊。
                      这时候,我们就开始学习配置文件吧。


                      14楼2015-08-05 11:09
                      回复
                        带着刚才的问题,我们进到配置文件里面
                        [root@mickey objects]# pwd
                        /usr/local/nagios/etc/objects
                        [root@mickey objects]# ls
                        commands.cfg contacts.cfg localhost.cfg printer.cfg switch.cfg templates.cfg timeperiods.cfg windows.cfg
                        进到该目录我们发现有很多cfg的文件。不要着急,楼楼会一个一个的讲给你听的。
                        刚才我们在web页面看到了localhost的监控是吧?
                        那这里我们也发现有个localhost.cfg的文件。我们不妨打开它看看。
                        [root@mickey objects]# vi localhost.cfg
                        现在知道了吧。刚才我们在web页面看到的localhost监控信息都是这个配置文件里面的。楼楼来给大家介绍下吧。
                        # HOST DEFINITION --定义主机。
                        define host{
                        use linux-server
                        host_name localhost
                        alias localhost
                        address 127.0.0.1
                        }
                        这个没问题吧?而唯一我们有疑惑的地方在 use 这个 linux-server是什么意思呢?
                        Name of host template to use 查看后面的注释,我们知道了 使用主模板名称 模板?哪里来的模板?有这样的问题就对了。
                        接下来我们就找找这个模板在哪里。
                        [root@mickey objects]# ls
                        commands.cfg contacts.cfg localhost.cfg printer.cfg switch.cfg templates.cfg timeperiods.cfg windows.cfg
                        我们再次ls一下。看到了吗? 我们发现了模板的配置文件:templates.cfg
                        很开心是吧。
                        我们打开它并找到linux-server。
                        name linux-server
                        use generic-host
                        check_period 24x7
                        check_interval 5
                        retry_interval 1
                        max_check_attempts 10
                        check_command check-host-alive
                        notification_period workhours
                        notification_interval 120
                        notification_options d,u,r
                        contact_groups admins
                        register 0
                        }
                        我们找到了linux-server对吧。我们来理解下这个模板。
                        name和use这里我们先不要管。
                        check_period 这里的check_period告诉nagios检查主机的时间段。这里就是7x24小时,就是整天咯。
                        check_interval nagios对主机的检查时间间隔,这里是5分钟。
                        retry_interval 重试检查时间间隔,单位是分钟。
                        max_check_attempts nagios对主机的最大检查次数,也就是nagios在检查发现某主机异常时,并不马上判断为异常状况;
                        check_command 指定检查主机状态的命令,其中“check-host-alive”在commands.cfg文件中定义。
                        notification_period 主机故障时,发送通知的时间范围,其中“workhours”在timeperiods.cfg中进行了定义;
                        notification_interval 在主机出现异常后,故障一直没有解决,nagios再次对使用者发出通知的时间。单位是分钟;
                        notification_options 定义主机在什么状态下可以发送通知给使用者,d即down,表示宕机状态;
                        contact_groups 指定联系人组,这个“admins”在contacts.cfg文件中定义。
                        现在大家应该很明确了吧。其实了,这些参数做什么大家不需要去死记硬背,只要大家知道linux-server在哪里定义的就好了。


                        15楼2015-08-05 11:09
                        回复
                          然后我们回到刚才localhost.cfg配置文件里面。
                          # HOST GROUP DEFINITION
                          define hostgroup{
                          hostgroup_name linux-servers
                          alias Linux Servers
                          members localhost
                          }
                          这里就是定义组啦。这个很简单就不介绍了。
                          # SERVICE DEFINITIONS
                          定义服务,这个就是重点啦。因为我们需要监控的东西都是定义在服务里面的。
                          一个个来看看吧。
                          define service{
                          use local-service
                          host_name localhost
                          service_description PING
                          check_command check_ping!100.0,20%!500.0,60%
                          }
                          这个大家都知道啦。ping嘛,谁不知道。这个是nagios中最常用到的。下面分析下。
                          use 引用模板,这个上面已经介绍过了。有兴趣大家可以找到查看下。
                          host_name 这个注意啦,还记得我们上面定义的主机吗?对吧。上面的host_name是什么这里就写什么。
                          service_description 服务说明。
                          check_command 检查命令。这个就是重中之重啦。后面那些是什么?什么乱七八糟的。对吧。
                          那么楼楼就来帮大家分析下吧。
                          还记得插件在那个目录下面吗? check_ping 这个是不是有点眼熟?
                          [root@mickey objects]# cd ../../libexec/
                          我们切换到插件的目录下。对吧,我们找到了check_ping 现在知道了吧。插件就是在定义服务里面引用的。但是后面的参数看不懂是什么意思啊!别急,我们再来看看。
                          还记得object里面有个command.cfg的文件吗?
                          command.cfg 命令定义配置文件,其中定义的命令可以被其他配置文件引用
                          对应上面的表我们知道了这个文件的作用。我们尝试打开它并找找check_ping看看。
                          define command{
                          command_name check-host-alive
                          command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
                          }
                          果然,不出所料,ping在这里定义过了。
                          问题又来了。$USER1$是什么东西?
                          我们找到变量定义文件:
                          $USER1$=/usr/local/nagios//libexec
                          # Sets $USER2$ to be the path to event handlers
                          #$USER2$=/usr/local/nagios//libexec/eventhandlers
                          # Store some usernames and passwords (hidden from the CGIs)
                          #$USER3$=someuser
                          #$USER4$=somepassword
                          看到了吧。$USER1$在这里定义过了。它是什么呢?它定义的是一个目录。这个目录就是我们插件所在的目录对吧。
                          现在我们懂了。
                          $USER1$/check_ping
                          /usr/local/nagios//libexec/check_ping
                          这个两个是一样的。
                          -H 后面跟的地址,这个地址就是IP。-w warning 告警。3000.0 毫秒。80%指的是丢包率
                          -w我们就可以理解了,当延迟在3000.0毫秒,丢包率到达80%的时候,nagios就开始告警提示。
                          -c 就是Critical 当延迟为5000.0毫秒,丢包率为100%的时候,这时候就是很严重的告警了。
                          这下大家都理解了check_ping的用法了吧?


                          16楼2015-08-05 11:09
                          收起回复
                            ping的用法及参数给大家介绍完了,接下来就该 check_local_disk
                            因为很多都根ping一样的,下面楼楼介绍check_command的时候就只介绍后面的参数咯。
                            define service{
                            use local-service
                            host_name localhost
                            service_description Root Partition
                            check_command check_local_disk!20%!10%!/
                            }
                            define command{
                            command_name check_local_disk
                            command_line $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
                            }
                            其实大家发现了没有,我们将define service 和 define command对应起来看,一起都很明白了。
                            define service 是在localhost.cfg中。
                            define command是在command.cfg中。
                            上面的命令我们就可以这样解释了:
                            -p后面目录。
                            当根目录使用率只剩下20%的时候,-w就开始告警了。
                            当根目录使用率只剩下10%的时候,-c就开始严重警告了。
                            讲过两个之后,后面的大家都应该知道怎么分析的了吧?
                            我就不在一一列举了。


                            17楼2015-08-05 11:10
                            回复
                              正好。楼楼自己的本子现在nagios有个问题。我们一起来看看。

                              IP地址请忽略掉哈,这是在家里。现在这台就是nagios-server
                              楼楼现在遇到这样一个问题。输入用户名密码死活都进不去。为嘛?
                              大家知道吗?
                              让我们先定位问题出在哪里?
                              这个应该是apache的验证机制。我之前有做过这个操作吗?
                              还记得吗?
                              我们刚开始的时候写过一个命令:
                              htpasswd -c /usr/local/nagios//etc/htpasswd.users nagiosadmin
                              那我们到该目录下去找找 htpasswd.users 看它还在不。
                              [root@super etc]# ls /usr/local/nagios//etc/htpasswd.users
                              ls: cannot access /usr/local/nagios//etc/htpasswd.users: No such file or directory
                              看到了吧。这个文件丢失了。
                              那我们重新来创建一下吧。
                              [root@super etc]# htpasswd -c /usr/local/nagios//etc/htpasswd.users nagiosadmin
                              New password:
                              Re-type new password:
                              Adding password for user nagiosadmin
                              [root@super etc]# service httpd reload
                              之后我们再次输入验证。就可以登录进去了。
                              所以遇到问题一定要很好的定位,再找到处理方法。


                              18楼2015-08-05 11:10
                              回复