网站建设资讯

NEWS

网站建设资讯

FreeBSD下部署nagios监控

一、首先安装apache,apache应该是我们最常用的部署了,它可以被弄得很复杂,但在nagios 这个平台上,我们只需要简单的功能即可。
引用
tar xzvf httpd-2.2.16.tar.gz
cd httpd-2.2.16
./configure --prefix=/usr/local/apache
make
make install

         二、安装php

cd php-5.3.4
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-zlib --with-jpeg-dir=/usr/local/jpeg6 --with-png-dir=/usr/local/libpng2 --with-iconv=/usr/local/libiconv --with-config-file-path=/usr/local/lib --disable-debug --enable-safe-mode --enable-short-tags
make
make install
cp php.ini-production /usr/local/php/etc/php.ini


      三、安装nagios

     1.添加帐户,用来运行nagios

pw groupadd nagios
pw useradd nagios -g nagios -s /usr/sbin/nologin

    2.编译安装nagios

tar xzvf nagios-3.2.3.tar.gz
cd nagios-3.2.3
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make all
make install
make install-commandmode
make install-config


      四、配置apache   

     修改httpd.conf文件,找打


    DirectoryIndex index.html
  
    修改为


    DirectoryIndex index.php index.html

    找到

AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
  
   在后面添加:

AddType application/x-httpd-php .php
AddType application/x-httpd-php .php3
AddType application/x-httpd-php .htm
AddType application/x-httpd-php .html


     在httpd.conf末尾添加

ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"


# SSLRequireSSL
   Options ExecCGI
   AllowOverride None
   Order allow,deny
   Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd
   Require valid-user
# AddHandler cgi-script cgi pl


Alias /nagios "/usr/local/nagios/share"


# SSLRequireSSL
   Options None
   AllowOverride None
   Order allow,deny
   Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd
   Require valid-user
    
     创建web验证用户

/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd admin


        五、安装nagios插件

tar xzvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure -prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

      可以看到在/usr/local/nagios/libexec 目录下多出很多文件,这些就是nagios插件,如果编译安装后看不到check_MySQL插件,需要本机安装mysql 并添加编译选项 --with-mysql=/usr/bin/mysql_config 指出mysql_config 路径。
    
      六、安装nrpe

tar xzvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure --prefix=/usr/local/nrpe
make all
make install

       在其它被监控主机上安装nrpe同样需要添加nagios用户,并安装nagios插件,要不然被监控主机缺少插件有些监控功能不能正常使用。
    安装完nrpe后,在安装目录/usr/local/nrpe/libexec只有一个文件check_nrpe,而在nagios插件目录,却缺少这个文件,因此需要把这个文件复制到nagios插件目录;同样,  因为nrpe需要调用的诸如check_disk等插件在自己的目录没有,可是这些文件确是nagios插件所存在的,所以也需要从nagios目录复制一份过来。我们把复制过程列举出来:
引用
cp /usr/local/nrpe/libexec/check_nrpe  /usr/local/nagios/libexec
cp /usr/local/nagios/libexec/*  /usr/local/nrpe/libexec
    
    配置nrpe
   安装完nrpe以后,在安装目录并没有可用的配置文件,但我们只需把解压目录的样例文件复制到安装目录,然后修改这个文件.
    mkdir /usr/local/nrpe/etc
    cp sample-config/nrpe.cfg  /usr/local/nrpe/etc    
    修改nrpe配置文件,找到

#server_address=127.0.0.1

    修改为

server_address=172.16.27.23

   此IP地址为被监控主机的IP地址

    找到

allowed_hosts=127.0.0.1

      更改为

allowed_hosts=127.0.0.1,172.16.27.23

      此IP地址为监控主机的IP地址,因为我监控机和被监控机都是同一台及其,请注意区分。
  
    启动nrpe

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

# sockstat |grep 5666
nagios   nrpe       12439 4  tcp4   172.16.27.23:5666     *:*

      出现上述信息则代表nrpe已正常启动。

     检查被监控主机的插件功能

# /usr/local/nagios/libexec/check_nrpe -H 172.16.27.23
NRPE v2.12

      出现此信息代表被监控主机的插件功能Ok

        通过nrpe检查主机资源,此方法就是通过在监控主机上使用check_nrpe使用命令行方式检查被监控主机资源。
  -c check_load 参数中的check_load命令定义在被监控主机的nrpe.cfg文件中。

# /usr/local/nagios/libexec/check_nrpe -H 172.16.27.23 -c check_load
OK - load average: 0.00, 0.00, 0.00|load1=0.000;15.000;30.000;0; load5=0.000;10.000;25.000;0; load15=0.000;5.000;20.000;0;

       七、配置nagios
    
      nagios的配置文件路径位于:/usr/local/nagios/etc

     1.首先修改nagios.cfg

     查找到

cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

     修改为

#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

    查找到

#cfg_dir=/usr/local/nagios/etc/servers

    修改为

cfg_dir=/usr/local/nagios/etc/servers

   添加此行

cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
    
    然后

mkdir /usr/local/nagios/etc/servers
chown -R nagios:nagios /usr/local/nagios/etc/servers

    创建此文件夹用于存放被监控主机的配置文件

   2. 进入object目录 修改contacts.cfg文件,此文件用于创建联系人及其组。

define contact{
        contact_name                    zhangsan
        alias                           Nagios zhangsan
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    u,c,r
        host_notification_options       d,r
        service_notification_commands   notify-service-by-email
        host_notification_commands      notify-host-by-email
        email                           zhangsan@test.com.cn
        }

  可按照此方法继续添加联系人


define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 zhangsan,lisi,wangwu
        }

  添加联系人所在的组 多个联系人用逗号分开      
  
      3.创建hosts.cfg文件,此文件用于定义被监控主机的ip及其主机名和添加到主机组

define host{
        host_name                172-16-27-23
        alias                          BSD-HOST
        address                     172.16.27.23
        contact_groups           admins
        check_command           check-host-alive
        max_check_attempts       10
        notification_interval          20
        notification_period          24x7
        notification_options         d,u,r
        }


      八、创建被监控主机的配置文件  
     首先在/usr/local/nagios/etc/objects/commands.cfg中定义check_nrpe命令,添加如下:

define command{
        command_name    check_nrpe
        command_line    /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
  
    在/usr/local/nagios/etc/servers中创建文件(文件名最好能标示主机为好)例如BSD-HOST.cfg,检查根分区的配置文件为

define service{
        host_name                       172-16-27-23
        service_description             check load
        check_period                    24x7
        max_check_attempts              4
        normal_check_interval           5
        retry_check_interval            1
        contact_groups                  admins
        notification_interval           5
        notification_period             24x7
        notification_options            w,u,c,r
        check_command                check_nrpe!check_load
        }


     创建完成后检查nagios配置是否正确,如果出现错误则nagios不能启动

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg


Total Warnings: 0
Total Errors:   0

    错误为0的话,则代表nagios没有错误可以启动,启动方法如下:

/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

ps -axuww | grep nagios
nagios   12490  0.0  0.8  3608  1960  ??  Ss    6:02PM   0:00.06 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg


      nagios出现It appears as though you do not have permission to view information for any of the services you requested...解决办法为

vi /usr/local/nagios/etc/cgi.cfg
use_authentication=1 #把1修改为0,保存
  
   重启nagios,错误解决。http://172.16.27.23/nagios/ 访问。
网站题目:FreeBSD下部署nagios监控
网页路径:http://cdweb.net/article/pdhhjc.html