红联Linux门户
Linux帮助

Linux下Nagios的安装与配置

发布时间:2015-03-29 15:22:47来源:linux网站作者:linux人

一、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的安装与配置

注:以上图片上传到红联Linux系统教程频道中。

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

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

emailnagios@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/

4、编译、安装nagios-plugins

nagios的所有监控工作都是通过插件完成的,因此,在启动nagios之前还需要为其安装官方提供的插件。

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

[root@DB2 ~]# cd nagios-plugins-1.4.15

[root@DB2 nagios-plugins-1.4.15]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios

[root@DB2 nagios-plugins-1.4.15]# make && make install

5、配置并启动Nagios

(1)把nagios添加为系统服务并将之加入到自动启动服务队列:

[root@DB2 ~]# chkconfig --add nagios

[root@DB2 ~]# chkconfig --level 35 nagios on

[root@DB2 ~]# chkconfig --list nagios

nagios0:off1:off2:off3:on4:on5:on6:off

(2)检查其主配置文件的语法是否正确:

[root@DB2 nagios]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

(3)如果上面的语法检查没有问题,接下来就可以正式启动nagios服务了:

[root@DB2 nagios]# service nagios start

Starting nagios: done.

(4)配置selinux

如果您的系统开启了selinux服务,则默认为拒绝nagios web cgi程序的运行。您可以通过下面的命令来检查您的系统是否开启了selinux:

[root@DB2 ~]# getenforce

如果上面命令的结果显示开启了selinux服务,您可以通过下面的命令暂时性的将其关闭:

[root@DB2 ~]# setenforce 0

[root@DB2 ~]# service iptables stop

iptables: Setting chains to policy ACCEPT: filter [OK]

iptables: Flushing firewall rules: [OK]

iptables: Unloading modules: [OK]

[root@DB2 ~]# set enforce 0

如果您想在以后完全关闭selinux,可以通过编辑/etc/sysconfig/selinux文件,将其中的selinux后面的值“force”修改为“disable”即可。

当然,您也可以通过以下方式将nagios的CGI程序运行于SELinux/targeted模式而不用关闭selinux:

[root@DB2 ~]# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin

[root@DB2 ~]# chcon -R -t httpd_sys_content_t /usr/local/nagios/share

(5)通过web界面查看nagios:

http://192.168.123.132/nagios

登录时需要指定前面设定的web认证帐号和密码。

注意:为了nagios系统的安全,建议您使用信息摘要强认证模式(如MD5),强制使用web的TSL/SSL安全通讯模式,并且通过访问控制列表设定能访问nagios的web界面的客户端地址。


四、配置Nagios

1、Nagios的主配置文件

Nagios的主配置文件为nagios.cfg,其语法非常简洁,通常#开头的行为注释行,而参数的设置格式为<parameter>=<value>;其中,有些参数是可以重复出现的。其中常用的参数说明如下:

log_file: 设定Nagios的日志文件;

cfg_file: Nagios对象定义的相关文件,此参数可重复使用多次以指定多个文件;

cfg_dir:设定Nagios对象定义的相关文件所在的目录,此目录中的所有文件都会被作为对象定义的文件;此参数可重复使用多次以指定多个目录;

resource_file: 设定Nagios附加的宏定义的相关文件;

status_file: 设定Nagios存储所有主机和服务当前状态信息的文件;

status_update_interval: 设定status_file指定的文件中状态信息的更新频率;

service_check_timeout: 设定服务检测的超时时间,默认为60秒;

host_check_timeout: 设定主机检测的超时时间,默认为30秒;

notification_timeout: 设定通知信息发送尝试的超时时间,默认为30秒;
 
2、resource_file和宏定义

在主配置文件中,参数resource_file用于定义所有用户变量(即“宏”)的存储文件,它用于存储对象定义中的可以访问的额外信息,如访问某服务的密码等;因此,这些信息通常都是些敏感数据,一般不允许通过Web接口来访问。此文件中可以定义的宏可多达32个,它们分别为$USER1$,$USER2$...$USER32,这些宏一般在check命令中引用。通常情况下$USER1$用于引用Nagios插件所在目录这个路径信息,因此,一般不建议修改其值。

Nagios事先定义了许多宏,它们的值通常依赖于其上下文。如下:

HOSTNAME: 用于引用host_name指定所定义的主机的主机名;每个主机的主机名都是唯一的;

HOSTADDRESS: 用于引用host对象中的address指令的值,它通常可以为IP地址或主机名;

HOSTDISPLAYNAME: 用于引用host对象中alias指令的值,用以描述当前主机,即主机的显示名称;

HOSTSTATE:某主机的当前状态,为UP,DOWN,UNREACHABLE三者之一;

HOSTGROUPNAMES: 用于引用某主机所属的所有主机组的简名,主机组名称之间以逗号分隔;

LASTHOSTCHECK:用于引用某主机上次检测的时间和日期,Unix时间戳格式;

LISTHOSTSTATE:用于引用某主机前一次检测时的状态,为UP,DOWN或UNREACHABLE三者之一;

SERVICEDESC: 用于引用对应service对象中的desccription指令的值;

SERVICESTATE: 用于引用某服务的当前状态,为OK,WARNING,UNKOWN或CRITICAL四者之一;

SERVICEGROUPNAMES: 用于引用某服务所属的所有服务组的简名,服务组名称之间以逗号分隔;

CONTACTNAME: 用于引用某contact对象中contact_name指令的值;

CONTACTALIAS: 用于引用某contact对象中alias指令的值;

CONTACTEMAIL: 用于引用某contact对象中email指令的值;

CONTACTGROUPNAMES: 用于引用某contact所属的所有contact组的简名,contact组名称之间以逗号分隔;

Nagios 3还支持自定义宏,只是它的定义和使用方式比较独特。管理员可以在某类型对象的定义中使用额外的指令,并能够在命令中使用特别格式的宏来引用此指令的值。其引用方式根据对象类型的不同也有所不同,具体如下:

$_HOST<variable>$ – 引用在主机对象中定义的指令的值;

$_SERVICE<variable>$ – 引用在服务对象中定义的指令的值;

$_CONTACT<variable>$ – 引用在联系人对象中定义的指令的值;

一个简单的例子如下:

如某主机定义为:

define host

{

host_name somemachine

address 10.0.0.1

_MAC 12:34:56:78:90:ab

check_command check-host-by-mac

}

对应的检测命令则可以定义为:

define command

{

command_name check-host-by-mac

command_line $USER1$/check_hostmac -H $HOSTADDRESS$ -m $_HOSTMAC$

}

3、定义主机对象

“主机”指的是被监控的机器,可是物理主机,也可以是虚拟设备。一个主机对象的定义至少应该包含一个简名(short name)、一个别名、一个IP地址和用到的检测命令。此外,很多时候,其定义中还应该包含监控时段、联系人及要通知的相关问题、检测的频率、重试检测的方式、发送通知的频率等。具体的各指令及说明请参见官方文档:http://nagios.sourceforge.net/docs/3_0/objectdefinitions.html#host。

一个主机定义的例子:

define host

{

host_name webserver1

hostgroups webservers

alias www.magedu.com

address 172.16.100.11

check_command check-host-alive

check_interval 5

retry_interval 1

max_check_attempts 5

check_period 24x7

contact_groups linux-admins

notification_interval 30

notification_period 24x7

notification_options d,u,r

}

其中的notification_options用于指定当主机处于什么状态时应该发送通知。其各状态及其表示符如下:

d —— DOWN

u —— UNREACHABLE

r —— UP(host recovery)

f —— flapping

s —— 调试宕机时间开始或结束

主机可以被划分成组,这些组即主机组。每一个主机组对象一般包含一个全局唯一的简名、一个描述名以及属于这个组的成员。此外,一个主机组的成员也可以是其它主机组。主机组的定义例子如下:

define hostgroup

{

hostgroup_name webservers

alias Linux web servers

members webserver1

}

4、定义服务对象

“服务”即某“主机”所提供的功能或资源对象,如HTTP服务、存储空间资源或CPU负载等。服务附属于主机,每一个服务使用服务名来标识,此服务名要求在特定的主机上具有唯一性。每一个服务对象还通常定义一个检测命令及如何进行问题通知等。

define service

{

host_name webserver1

service_description www

check_command check_http

check_interval 10

check_period 24x7

retry_interval 3

max_check_attempts 3

notification_interval 30

notification_period 24x7

notification_options w,c,u,r

contact_groups linux-admins

}

其中的notification_options用于指定当服务处于什么状态时应该发送通知。其各状态及其表示符如下:

w —— WARNING

u —— UNKNOWN

c —— CRITICAL

r —— OK(recovery)

f —— flapping

s —— 调试宕机时间开始或结束

与主机对象有所不同的是,有时个,多个主机可能会提供同样的服务,比如多台服务器同时提供Web等。因此,在定义服务对象时,其host_name可以为逗号隔开的多个主机。

服务可以被划分成组,这些组即服务组。每一个服务组对象一般包含一个全局唯一的简名、一个描述名以及属于这个组的成员。此外,一个服务组的成员通常是某主机上的某服务,其指定时使用<host>,<service>的格式,多个服务也使用逗号分隔。服务组的定义例子如下:

define servicegroup

{

servicegroup_name webservices

alias All services related to web

members webserver1,www,webserver2,www

}

5、定义命令对象

“命令”用于描述如何对主机或服务进行状态检测。服务对象的定义包含两个指令:名字(command_name)和命令行(command_line);名字用于标识此命令对象,命令行则是执行检测时真正要执行的命令。

当命令对象用于检测其它对象时,其通常需要用到额外的参数以标识要检测的某特定对象,此时,命令对象需要以command_name[!arg1][!arg2][...]的语法格式进行引用。因此,命令对象的定义中,命令行指令中通常会用到宏$ARG1$, $ARG2$...,对应用于接收[!arg1][!arg2][...]传递而来的参数。

如下命令对象的定义:

define command

{

command_namecheck_local_swap

command_line$USER1$/check_swap -w $ARG1$ -c $ARG2$

}

如下的服务中使用上面定义的命令对象来检测服务对象:

define service

{

host_namelocalhost

service_descriptionSwap Usage

check_command check_local_swap!20!10

}

6、定义“时段”对象

“时段”用于定义某“操作”可以执行或不能执行的日期和时间跨度,如工作日内的每天8:00-18:00等,其可以在多个不同的操作中重复引用。一个时段对象的定义包含一个全局唯一的名称标识及一个或多个时间跨度。例如:

define timeperiod

{

timeperiod_name workinghours

alias Working Hours, from Monday to Friday

monday 09:00-17:00

tuesday 09:00-17:00

wednesday 09:00-17:00

thursday 09:00-17:00

friday 09:00-17:00

}

其中,时间的指定格式有许多方式:

日历时间:格式为YYYY-MM-DD,如2012-04-21;

日期:如 April 21;

每月的某一天:如 day 21,指每月的21号;

每月的第几个周几:如 saturday 1,指每月的第一个星期六;

星期几:如monday, tuesday等;
 
7、定义联系人对象

“联系人”对象用于定义某主机设备的拥有者或某问题出现时接受通知者。联系人对象的定义包含一个全局唯一的标识名称、一个描述名及一个或多个邮件地址等。此外,其通常还应该包括对相应的主机或服务出现故障时所用到的通知命令。例如:

define contact

{

contact_name mageedu

alias Mage Education

email linuxedu@magedu.com

host_notification_periodworkinghours

service_notification_periodworkinghours

host_notification_optionsd,u,r

service_notification_optionsw,u,c,r

host_notification_commandshost-notify-by-email

service_notification_commandsnotify-by-email

}

联系人也可划分为组,即联系人组。一个联系人组对象包含一个全局惟一的标识名称,一个描述名称和属于此联系人组的联系人成员(members)或其人联系人组成员(contactgroup_members)。例如:

define contactgroup

{

contactgroup_name linux-admins

alias Linux Administrators

members magedu, mageedu

}

在主机或服务对象的定义中,既可以指定联系人,也可以指定联系人组。当然,某主机的问题联系人与其上运行的服务的联系人也可以不同。
 
8、模板及对象继承

Nagios通过功能强大的继承引擎来实现基于模板的对象继承。这就意味着可以定义将某类型的对象的通用属性组织起来定义为对象模板,并在定义其类型中的对象时直接从此模板继承其相关属性的定义。定义对象模板的方法很简单,通常只需要在定义某类型对象时使用register指令并将其值设定为0即可。对象模板的名称通常使用name指令定义,这与某特定类型对象使用的指令也有所不同。而定义此种类型的对象时,只需要使用use指令并将其值设定为对应模板的名称即可。例如:

define host

{

name generic-server

check_command check-host-alive

check_interval 5

retry_interval 1

max_check_attempts 5

check_period 24x7

notification_interval 30

notification_period 24x7

notification_options d,u,r

register 0

}

define host

{

use generic-server

name webserver1

alias Web Server 01

address 172.16.100.11

contact_groups linux-admins

}

一个对象在定义时也以同时继承多个模板,此时只需要为use指令指定以逗号分隔的多个模板名称即可。同时,Nagios也支持模板的多级继承。
 
9、依赖关系

为了描述Nagios对象间的依赖关系,这里要用到两个术语:master(被依赖的主机或服务)和dependent(依赖关系中的依赖于master的Nagios对象)。Nagios可以定义对象间的彼此依赖性,也可以为某对象定义其父对象,甚至也可以指定此依赖关系生效的时段。下面是一个关于依赖关系定义的例子:

define hostdependency

{

dependent_host_name backuphost

host_name vpnserver1

dependency_period maintenancewindows

}

其中host_name用于定义master主机,dependent_host_name定义dependent主机。而在依赖关系的定义中,通常还会用到execution_failure_criteria定义master主机为何种状态时不再对依赖于此master的主机进行检测,notification_failure_criteria用于定义master处于何种状态时不会发送dependent相关的主机问题通知到联系人。

服务间依赖关系的定义类似于主机间的依赖关系,例如:

define servicedependency

{

host_name mysqlserver

service_description mysql

dependent_hostgroup_name apacheservers

dependent_service_description webservice

execution_failure_criteria c,u

notification_failure_criteria c,u,w

}