Linux下Nagios的安装与配置


一、Nagios概述

1、简介

Nagios是插件式的结构,它本身没有任何监控功能,所有的监控都是通过插件进行的,因此其是高度模块化和富于弹性的。Nagios监控的对象可分为两类:主机和服务。主机通常指的是物理主机,如服务器、路由器、工作站和打印机等,这里的主机也可以是虚拟设备,如xen虚拟出的Linux系统;而服务通常指某个特定的功能,如提供http服务的httpd进程等。而为了管理上的方便,主机和服务还可以分别被规划为主机组和服务组等。

Nagios不监控任何具体数值指标(如操作系统上的进程个数),它仅用四种抽象属性对被监控对象的状态进行描述:OK、WARNING, CRITICAL和UNKNOWN。于是,管理员只需要对某种被监控对象的WARNING和CRITICAL状态的阈值进行关注和定义即可。Nagios通过将WARTING和CRTICAL的阈值传递给插件,并由插件负责某具体对象的监控及结果分析,其输出信息为状态信息(OK,WARNING,CRITICAL或UNKOWN)以及一些附加的详细说明信息。

2、特性

由上述说明可以,Nagios是极富弹性的,其监控功能完全可以按照管理员的期望进行。此外,它外提供了对问题的自动响应能力和一个功能强大的通知系统。所有这些功能的实现是基于一个结构明晰的对象定义系统和少数几个对象类型实现的。

1) 命令(Commands)

“命令”用于定义Nagios如何执行某特定的监控工作。它是基于某特定的Nagios插件定义出的一个抽象层,通常包含一组要执行的操作。

2)时段(Time periods)

“时段”用于定义某“操作”可以执行或不能执行的日期和时间跨度,如工作日内的每天8:00-18:00等;

3)联系人和联系人组(Contacts and contact groups)

“联系人”用于定义某监控事件的通知对象、要通知的信息以及这些接收通知者何时及如何接收通知;一个或多个联系人可以定义为联系人组,而一个联系人也可以属于多个组;

4) 主机和主机组(Host and Host groups)

“主机”通常指某物理主机,其包括此主机相关的通知信息的接收者(即联系人)、如何及何时进行监控的定义。主机也可以分组,即主机组(host groups),一个主机可同时属于多个组;

5) 服务(Services)

“服务”通常指某主机上可被监控的特定的功能或资源,其包括此服务相关的通知信息的接收者、如何及何时进行监控等。服务也可以分组,即服务组(Service groups),一个服务可同时属于多个服务组;

3、依赖关系

Nagios的强大功能还表现在其成熟的依赖关系系统上。比如,某路由设备故障必然会导致关联在其上的其它主机无法被正常访问,如果不能定义这些设备间的依赖关系,那么监控系统上必然会出现大量的设备故障信息。而Nagios则通过依赖关系来描述网络设备的拓扑结构,并能够实现在某设备故障时不再对依赖于此设备的其它设备进行检测,从而避免了无谓的故障信息,方便管理员及时定位并排除故障。此外,Nagios的依赖关系还可以在服务级别上实现,如果某服务依赖于其它服务时,也能实现类似主机依赖关系的功能。

4、宏

Nagios还能够使用宏,并且宏的定义在整个Nagios系统中具有一致性。宏是能够用于对象定义中的变量,其值通常依赖于上下文。在“命令”中定义的宏,相对于主机、服务或其它许多参数来说,其值会随之不同。比如,某命令可以根据向其传递的IP地址的不同来监控不同的主机。

5、计划中宕机

Nagios还提供了调度性计划中的宕机机制,管理员可以周期性的设定某主机或服务为计划中的不可用状态。这种功能可以阻止Nagios在调度宕机时段通知任何信息。当然,这也可以让Nagios自动通知管理员该进行主机或服务维护了。

6、软状态和硬状态(Soft and Hard States)

如上所述,Nagios的主要工作是检测主机或服务的状态,并将其存储下来。某一时刻,主机或服务状态仅可以是四种可用状态之一,因此,其状态能够正确反映主机或服务的实际状况就显得特别关键。为了避免某偶然的临时性或随机性问题,Nagios引入了软状态和硬状态。在实际的检测中,Nagios一旦发现某主机或服务的状态为UNKOWN或不同于上一次检测时的状态,其将会对此主机或服务进行多次测试以确保此状态的变动是非偶然性的。具体共要做出几次测试是可以配置的,在这个指定次数的测试时段内,Nagios假设此变化后的状态为软件状态。一旦测试完成后状态仍然为新变的状态时,此状态就成了硬状态。

二、安装配置Nagios

1、Nagios程序简介

Nagios通常由一个主程序(Nagios)、一个插件程序(Nagios-plugins)和四个可选的ADDON(NRPE、NSCA、NSClient++和NDOUtils)组成。Nagios的监控工作都是通过插件实现的,因此,Nagios和Nagios-plugins是服务器端工作所必须的组件。而四个ADDON中,NRPE用来在监控的远程Linux/Unix主机上执行脚本插件以实现对这些主机资源的监控;NSCA用来让被监控的远程Linux/Unix主机主动将监控信息发送给Nagios服务器(这在冗余监控模式中特别要用到);NSClient++是用来监控Windows主机时安装在Windows主机上的组件;而NDOUtils则用来将Nagios的配置信息和各event产生的数据存入数据库,以实现这些数据的快速检索和处理。这四个ADDON(附件)中,NRPE和NSClient++工作于客户端,NDOUtils工作于服务器端,而NSCA则需要同时安装在服务器端和客户端。

Linux下Nagios的安装与配置

目前,Nagios只能安装在Linux系统主机上,其编译需要用到gcc。同时,如果打算使用web界面的管理工具的话,还需要有apache服务器和GD图形库的支持。

在Ubuntu下配置Mrtg监控Nginx和服务器系统资源

使用 snmp+Mrtg 监控 Linux 系统

Mrtg服务器搭建(监控网络流量)

网络监控器Nagios全攻略

Nagios搭建与配置详解

Nginx环境下构建Nagios监控平台

在RHEL5.3上配置基本的Nagios系统(使用Nagios-3.1.2)

CentOS 5.5+Nginx+Nagios监控端和被控端安装配置指南

Ubuntu 13.10 Server 安装 Nagios Core 网络监控运用

2、安装前的准备工作

(1)解决安装Nagios的依赖关系:

Nagios基本组件的运行依赖于httpd、gcc、gd。可以通过以下命令来检查nagios所依赖的rpm包是否已经完全安装:

[root@DB2 ~]# yum -y install httpd gcc glibc glibc-common gd gd-devel php php-mysql mysql mysql-devel mysql-server

说明:以上软件包您也可以通过编译源代码的方式安装,只是后面许多要用到的相关文件的路径等需要按照您的源代码安装时的配置逐一修改。此外,您还得按需启动必要的服务,如httpd等。

(2)添加nagios运行所需要的用户和组:

[root@DB2 ~]# groupadd nagcmd

[root@DB2 ~]# useradd -G nagcmd nagios

[root@DB2 ~]# passwd nagios

把apache加入到nagcmd组,以便于在通过web Interface操作nagios时能够具有足够的权限:

[root@DB2 ~]# usermod -a -G nagcmd apache

3、编译安装nagios:

[root@DB2 ~]# tar -zxf nagios-3.3.1.tar.gz

[root@DB2 ~]# cd nagios

[root@DB2 nagios]# ./configure --with-command-group=nagcmd --enable-event-broker


*** Configuration summary for nagios 3.3.1 07-25-2011 ***:

 General Options:

 -------------------------

        Nagios executable:  nagios

        Nagios user/group:  nagios,nagios

      Command user/group:  nagios,nagcmd

            Embedded Perl:  no

            Event Broker:  yes

        Install ${prefix}:  /usr/local/nagios

                Lock file:  ${prefix}/var/nagios.lock

  Check result directory:  ${prefix}/var/spool/checkresults

          Init directory:  /etc/rc.d/init.d

  Apache conf.d directory:  /etc/httpd/conf.d

            Mail program:  /bin/mail

                  Host OS:  linux-gnu

 


 Web Interface Options:

 ------------------------

                HTML URL:  http://localhost/nagios/

                  CGI URL:  http://localhost/nagios/cgi-bin/

 Traceroute (used by WAP): 

Review the options above for accuracy.  If they look okay,

type 'make all' to compile the main program and CGIs.
 

[root@DB2 nagios]# make all


*** Compile finished ***

 


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

 

 

 

*** Support Notes *******************************************

 


If you have questions about configuring or running Nagios,

please make sure that you:

 


    - Look at the sample config files

    - Read the documentation on the Nagios Library at:

          http://library.nagios.com

 


before you post a question to one of the mailing lists.

Also make sure to include pertinent information that could

help others help you.  This might include:

 


    - What version of Nagios you are using

    - What version of the plugins you are using

    - Relevant snippets from your config files

    - Relevant error messages from the Nagios log file

 


For more information on obtaining support for Nagios, visit:

 


      http://support.nagios.com

 


*************************************************************

 


Enjoy.
 

[root@DB2 nagios]# make install


*** Main program, CGIs and HTML files installed ***

 


You can continue with installing Nagios as follows (type 'make'

without any arguments for a list of all possible options):

 


  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

 


make[1]: Leaving directory `/root/nagios'
 

[root@DB2 nagios]# make install-init

 


/usr/bin/install -c -m 755 -d -o root -g root /etc/rc.d/init.d

/usr/bin/install -c -m 755 -o root -g root daemon-init /etc/rc.d/init.d/nagios

 


*** Init script installed ***
 

[root@DB2 nagios]# make install-commandmode

 


/usr/bin/install -c -m 775 -o nagios -g nagcmd -d /usr/local/nagios/var/rw

chmod g+s /usr/local/nagios/var/rw

 


*** External command directory configured ***
 

[root@DB2 nagios]# make install-config


/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc

/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc/objects

/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/nagios.cfg /usr/local/nagios/etc/nagios.cfg

/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/cgi.cfg /usr/local/nagios/etc/cgi.cfg

/usr/bin/install -c -b -m 660 -o nagios -g nagios sample-config/resource.cfg /usr/local/nagios/etc/resource.cfg

/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/templates.cfg /usr/local/nagios/etc/objects/templates.cfg

/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/commands.cfg /usr/local/nagios/etc/objects/commands.cfg

/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/contacts.cfg /usr/local/nagios/etc/objects/contacts.cfg

/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/timeperiods.cfg /usr/local/nagios/etc/objects/timeperiods.cfg

/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/localhost.cfg /usr/local/nagios/etc/objects/localhost.cfg

/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/windows.cfg /usr/local/nagios/etc/objects/windows.cfg

/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/printer.cfg /usr/local/nagios/etc/objects/printer.cfg

/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/switch.cfg /usr/local/nagios/etc/objects/switch.cfg

 


*** Config files installed ***

 


Remember, these are *SAMPLE* config files.  You'll need to read

the documentation for more information on how to actually define

services, hosts, etc. to fit your particular needs.
 

 


为email指定您想用来接收nagios警告信息的邮件地址,默认是本机的nagios用户:

[root@DB2 nagios]# vi /usr/local/nagios/etc/objects/contacts.cfg

email                          nagios@localhost      #这个是默认设置

 


在httpd的配置文件目录(conf.d)中创建Nagios的Web程序配置文件:

[root@DB2 nagios]# cd /etc/httpd/conf.d/

[root@DB2 nagios]# make install-webconf


/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf

 


*** Nagios/Apache conf file installed ***
 

 


创建一个登录nagios web程序的用户,这个用户帐号在以后通过web登录nagios认证时所用:

 


[root@DB2 nagios]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin


New password:

Re-type new password:

Adding password for user nagiosadmin
 

 


以上过程配置结束以后需要重新启动httpd:

[root@DB2 nagios]# service httpd restart


Stopping httpd: [  OK  ]

Starting httpd: httpd: apr_sockaddr_info_get() failed for DB2

httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

[  OK  ]
 

 


Apache的安装:

[root@DB2 ~]# wget http://archive.apache.org/dist/httpd/httpd-2.2.23.tar.gz

[root@DB2 ~]# tar -zxf httpd-2.2.23.tar.gz

[root@DB2 ~]# cd httpd-2.2.23

[root@DB2 httpd-2.2.23]# ./configure --prefix=/usr/local/apache2

[root@DB2 httpd-2.2.23]# make && make install

 


[root@DB2 php-5.6.2]# yum -y install libxml2

[root@DB2 php-5.6.2]# yum install libxml2-devel -y

 


PHP的安装:

[root@DB2 ~]# wget http://cn2.php.net/distributions/php-5.6.2.tar.gz

[root@DB2 ~]# tar zxf php-5.6.2.tar.gz

[root@DB2 ~]# cd php-5.6.2

[root@DB2 php-5.6.2]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs

 


Generating files

configure: creating ./config.status

creating main/internal_functions.c

creating main/internal_functions_cli.c

+--------------------------------------------------------------------+

| License:                                                          |

| This software is subject to the PHP License, available in this    |

| distribution in the file LICENSE.  By continuing this installation |

| process, you are bound by the terms of this license agreement.    |

| If you do not agree with the terms of this license, you must abort |

| the installation process at this point.                            |

+--------------------------------------------------------------------+

 


Thank you for using PHP.

 


config.status: creating php5.spec

config.status: creating main/build-defs.h

config.status: creating scripts/phpize

config.status: creating scripts/man1/phpize.1

config.status: creating scripts/php-config

config.status: creating scripts/man1/php-config.1

config.status: creating sapi/cli/php.1

config.status: creating sapi/cgi/php-cgi.1

config.status: creating ext/phar/phar.1

config.status: creating ext/phar/phar.phar.1

config.status: creating main/php_config.h

config.status: executing default commands
 

[root@DB2 php-5.6.2]# make && make install


Build complete.

Don't forget to run 'make test'.

 


Installing PHP SAPI module:      apache2handler

/usr/local/apache2/build/instdso.sh SH_LIBTOOL='/usr/local/apache2/build/libtool' libphp5.la /usr/local/apache2/modules

/usr/local/apache2/build/libtool --mode=install cp libphp5.la /usr/local/apache2/modules/

cp .libs/libphp5.so /usr/local/apache2/modules/libphp5.so

cp .libs/libphp5.lai /usr/local/apache2/modules/libphp5.la

libtool: install: warning: remember to run `libtool --finish /root/php-5.6.2/libs'

chmod 755 /usr/local/apache2/modules/libphp5.so

[activating module `php5' in /usr/local/apache2/conf/httpd.conf]

Installing shared extensions:    /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

Installing PHP CLI binary:        /usr/local/php/bin/

Installing PHP CLI man page:      /usr/local/php/php/man/man1/

Installing PHP CGI binary:        /usr/local/php/bin/

Installing PHP CGI man page:      /usr/local/php/php/man/man1/

Installing build environment:    /usr/local/php/lib/php/build/

Installing header files:          /usr/local/php/include/php/

Installing helper programs:      /usr/local/php/bin/

  program: phpize

  program: php-config

Installing man pages:            /usr/local/php/php/man/man1/

  page: phpize.1

  page: php-config.1

Installing PEAR environment:      /usr/local/php/lib/php/

[PEAR] Archive_Tar    - installed: 1.3.12

[PEAR] Console_Getopt - installed: 1.3.1

[PEAR] Structures_Graph- installed: 1.0.4

[PEAR] XML_Util      - installed: 1.2.3

[PEAR] PEAR          - installed: 1.9.5

Wrote PEAR system config file at: /usr/local/php/etc/pear.conf

You may want to add: /usr/local/php/lib/php to your php.ini include_path

/root/php-5.6.2/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin

ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar

Installing PDO headers:          /usr/local/php/include/php/ext/pdo/

更多详情见请继续阅读下一页的精彩内容:

  • 1
  • 2
  • 下一页

相关内容