Warning: Undefined array key "cperpage" in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 255

Warning: Undefined variable $output in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 325

Warning: Undefined variable $fixed_tags in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 326

Warning: Undefined variable $isshowdots in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 327

Warning: Undefined variable $tag_aditional in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 330

Warning: Undefined variable $tag_aditional in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 333

Warning: Undefined variable $tag_aditional in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 336

Warning: Undefined variable $post in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 345

Warning: Attempt to read property "ID" on null in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 345

Warning: Undefined variable $post in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 345

Warning: Attempt to read property "ID" on null in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 345

Warning: Undefined variable $more_text_link in /www/wwwroot/www.now163.com/wp-content/themes/twentytwentyfive/functions.php on line 345
Ubuntu 11.10下构建Nagios监控平台 – 理想社会

Ubuntu 11.10下构建Nagios监控平台

Nagios是一个监视系统运行状态和网络信息的监视系统。Nagios能监视所指定的本地或远程主机以及服务,同时提供异常通知功能等

Nagios可运行在Linux/Unix平台之上,同时提供一个可选的基于浏览器的WEB界面以方便系统管理人员查看网络状态,各种系统问题,以及日志等等。

Nagios的主要功能特点:
  –监视网络服务 (SMTP, POP3, HTTP, NNTP, PING等)
  –监视主机资源 (进程, 磁盘等)
  –简单的插件设计可以轻松扩展Nagios的监视功能
  –服务等监视的并发处理
  –错误通知功能 (通过email, pager, 或其他用户自定义方法)
  –可指定自定义的事件处理控制器
  –可选的基于浏览器的WEB界面以方便系统管理人员查看网络状态,各种系统问题,以及日志等等
  –可以通过手机查看系统监控信息

一、安装前准备

安装的机器上必须有一个WEB服务,本文是在Apache环境上安装的。

下载nagios主程序和相关插件程序包

wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz 
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz 
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz

安装GD库(Nagios中的statusmap和trends模块必须)

apt-get install libgd2-noxpm libgd2-noxpm-dev

二、Nagios监控端安装

1、创建Nagios用户及组

建立Nagios账号

/usr/sbin/useradd -m -s /sbin/nologin nagios

2、创建一个名为nagcmd的用户组,用于从web接口执行外部命令。将Nagios用户和Apache用户加入组中。

groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd www-data

注:上面的www-data是Apache用户所属的组,如有不同请自行调整。

3、编译安装Nagios

tar zxvf nagios-3.2.3.tar.gz
cd nagios-3.2.3
./configure --with-command-group=nagcmd
make
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf
cd ..

注:make install 用于安装主要的程序、CGI及HTML文件

make install-init 用于生成init启动脚本

make install-config 用于安装示例配置文件

make install-commandmode 用于设置相应的目录权限

make install-webconf 用于安装Apache配置文件

4、验证程序是否被正确安装

切换目录到安装路径,这里是/usr/local/nagios,看是否存在etc、bin、 sbin、 share、 var这五个目录,如果存在则可以表明程序被正确的安装到系统了。

ls  /usr/local/nagios/
bin/     etc/     sbin/    share/   var/

注;bin–Nagios执行程序所在目录,其中的nagios文件即为主程序。

etc–Nagios配置文件位置

sbin–Nagios cgi文件所在目录,也就是执行外部命令所需文件所在的目录

Share–Nagios网页文件所在的目录

var–Nagios日志文件、spid 等文件所在的目录

var/archives–日志归档目录

var/rw–用来存放外部命令文件

5、配置Nagios Web界面登陆帐号及密码

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

修改Nagios配置文件,给新增的用户增加访问权限

6、编译并安装Nagios插件

由于Nagios主程序只是提供一个运行框架,其具体监控是靠运行在其下的插件完成的,所以Nagios插件是必须安装的。

tar zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

验证Nagios插件是否正确安装

ls /usr/local//nagios/libexec

显示安装的插件文件,即所有的插件都安装在libexec这个目录下。

7、启动服务

启动前先检查下配置文件是否正确

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

如果没有报错,可以启动Nagios服务

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

查看Nagios运行状态

/usr/local/nagios/bin/nagiostats

8、安装NRPE

由于Nagios只能监测自己所在的主机的一些本地情况,例如,cpu负载、内存使用、硬盘使用等等。如果想要监测被监控的服务器上的这些本地情况,就要用到NRPE。NRPE(Nagios Remote Plugin Executor)是Nagios的一个扩展,它被用于被监控的服务器上,向Nagios监控平台提供该服务器的一些本地的情况。NRPE可以称为Nagios的Linux客户端。

由于NRPE是通过SSL方式在监控和被监控主机上进行数据传输的,所以必须先安装ssl相关的软件包。

apt-get install libssl-dev libssl0.9.8

编译安装NRPE

tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config

注:监控主机上只需要make install-plugin这一步就可以了。监控机上只要有一个check_nrpe插件用于连接被监控端nrpe的daemon就行了。

启动NRPE

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

验证NRPE是否正确安装

/usr/local/nagios/libexec/check_nrpe -H localhost

注:如果成功,会返回NRPE的版本号。
三、Nagios被控端安装配置

1、创建Nagios用户及组

建立Nagios账号

/usr/sbin/useradd -m -s /sbin/nologin nagios

2、编译并安装Nagios插件

tar zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
cd ..

验证程序是否被正确安装:

ls /usr/local/nagios/libexec

显示安装的插件文件,即所有的插件都安装在libexec这个目录下。

3、安装NRPE

tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
cd ..

4、启动NRPE

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

验证NRPE是否正确安装

/usr/local/nagios/libexec/check_nrpe -H localhost

注:如果成功,会返回NRPE的版本号。

5、修改NRPE配置文件,让监控主机可以访问被监控主机的NRPE。

缺省NRPE配置文件中只允许本机访问NRPE的Daemon

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

#缺省为127.0.0.1,只能本机访问
allowed_hosts=192.168.1.108

6、重启nrpe的方法

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

四、Nagios配置文件关系说明

Nagios的配置文件

#控制cgi访问的配置文件
cgi.cfg
#Nagios主配置文件
nagios.cfg
#resource.cfg
定义了一些变量,以便被其他文件引用,如$USER1$resource.cfg
#objects是一个目录,用于定义Nagios对象
objects
#servers是自己创建的一个目录,Nagios可以加载一个目录下面的所有配置文件(需要在nagios.cfg中配置)
servers

./objects:
#命令定义配置文件,里面定义的命令可以被其他文件引用
commands.cfg
#联系人和联系人组配置文件
contacts.cfg
#监控本地机器的配置文件
localhost.cfg
#监控打印机的一个事例配置文件(默认未启用)
printer.cfg
# 监控路由器的一个事例配置文件(默认未启用)
switch.cfg
# 模板配置文件,在此可以定义模板,在其他文件中引用
templates.cfg
# 定义监控时间段的配置文件
timeperiods.cfg
# 监控Windows的一个事例配置文件(默认未启用)
windows.cfg

./server:

# 自己创建的主机群组配置文件
hostgroup.cfg
# 自己创建的监控远程Linux主机的配置文件
linux.cfg

五、监控应用

监控一个项目最重要的有下面三点:首先是监控哪台机器,然后是这个监控要用什么命令实现,最后就是出了问题的时候要通知哪个联系人。

A、定义监控的主机

创建一个用于存放监控的项目的目录

mkdir -p /usr/local/nagios/etc/server

配置nagios.cfg文件

让Nagios能够访问自定义的目录下的配置文件,增加以下内容:

cfg_dir=/usr/local/nagios/etc/server

创建要监控主机的配置文件

这里我以示例的localhost.cfg为基础进行创建

cp /usr/local/nagios/etc/objects/localhost.cfg  /usr/local/nagios/etc/server/linuxsever1.cfg

先修改对linuxsever.cfg文件中对主机定义部分,服务部分的定义放在后面来说。

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

define host{
        use                     linux-server            ; Name of host template to use
							; This host definition will inherit all variables that are defined
							; in (or inherited by) the linux-server host template definition.
        host_name               linuxsever1
        alias                   linuxsever1
        address                 192.168.1.106
        }

# Define an optional hostgroup for Linux machines

define hostgroup{
        hostgroup_name  linuxsever ; The name of the hostgroup
        alias           linuxsever ; Long name of the group
        members         linuxsever1     ; Comma separated list of hosts that belong to this group
        }

B、定义监控的命令

要对主机上的服务进行监控,首先需要定义监控服务所使用的监控命令。

监控远程服务和资源的命令以及如何发送邮件命令的定义一般都在commands.cfg中进行。大部分监控远程服务和资源的命令都是通过/usr/local/nagios/libexec下的脚本实现,如ping命令为check_ping。

配置commands.cfg文件

我们这里要对被监控服务器的一些本地资源情况,这里就先加入NRPE的支持。

定义NRPE支持,commands.cfg中增加以下内容

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

define command{
	command_name check_nrpe
	command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
	}

定义发送邮件的命令,实现邮件报警

修改commands.cfg中的notify-host-by-email和notify-service-by-email命令

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

# 'notify-host-by-email' command definition
define command{
	command_name	notify-host-by-email
	command_line    /usr/bin/printf "%b" "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nHost: $HOSTNAME$nState: $HOSTSTATE$nAddress: $HOSTADDRESS$nInfo: $HOSTOUTPUT$nnDate/Time: $LONGDATETIME$n" | /usr/bin/sendEmail -f nagiosdemo@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -xu nagiosdemo -xp nagiosdemo
	}

# 'notify-service-by-email' command definition
define command{
	command_name	notify-service-by-email
	command_line    /usr/bin/printf "%b" "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nnService: $SERVICEDESC$nHost: $HOSTALIAS$nAddress: $HOSTADDRESS$nState: $SERVICESTATE$nnDate/Time: $LONGDATETIME$nnAdditional Info:nn$SERVICEOUTPUT$n" | /usr/bin/sendEmail -f nagiosdemo@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu nagiosdemo -xp nagiosdemo
	}

注:监控主机上没有安装任何SMTP服务,这里就采用了外部邮件服务器上SMTP服务进行发送邮件。这样就需要一个可自定义SMTP服务器的邮件客户端。这里采用的是sendEmail。

SendEmail简单介绍

SendEmail官方地址:http://caspian.dotconf.net/menu/Software/SendEmail/

安装

apt-get install sendemail

基本用法

sendemail -f nagiosdemo@163.com -t nagiosdemo@163.com -s smtp.163.com -u “from nagios” -xu nagiosdemo -xp nagiosdemo -m happy

各参数具体含义

-f 表示发送者的邮箱
-t 表示接收者的邮箱
-s 表示SMTP服务器的域名或者ip
-u 表示邮件的主题
-xu  表示SMTP验证的用户名
-xp  表示SMTP验证的密码(注意:这个密码有限制,一些特殊符号或者超长度密码不能被正确识别)
-m 表示邮件的内容

更详细用法可参考官方文档或直接运行sendEmail就会显示详细的用法。

小技巧:如果这里的收件邮箱采用139的邮箱,就一举两得的同时实现了邮件报警和短信报警的两个功能。因为139邮箱提供了邮件到达后,免费短信提醒通知功能。也算是短信通知了另一种实现方式吧,呵呵!

注:飞信机器人在Ubuntu 10.04环境下不能成功运行,提示’No such file or directory’,由于无更多提示信息,暂不能解决。CentOS5.2下能成功运行,不知是不是由于官方提供的飞信机器人支持库是在CentOS环境编译的原因!

C、定义监控联系人和联系人组

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

define contact{
        contact_name                    mike            ; Short name of user
        use                             generic-contact         ; Inherit default values from generic-contact template (defined above)
        alias                           Nagios Admin            ; Full name of user

        email                           nagios@nagios.com  ; <

D、定义常用监控的服务

Nagios监控功能非常的强大,这里只说下比较常见的一些监控项目。

A、通过NRPE监控被监控主机的本地资源信息

1、监控被监控主机的负载情况

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             Current Load
	check_command			check_nrpe!check_load
        }

2、监控被监控主机上的登陆用户数

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             Current Users
	check_command			check_nrpe!check_users
        }

3、监控被监控主机上的根分区磁盘使用情况

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             Root Partition
	check_command			check_nrpe!check_sda1
        }

4、监控被监控主机上的Swap使用情况

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             Swap Usage
	check_command			check_nrpe!check_swap
        }

5、监控被监控主机上的运行的总进程数

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             Total Processes
	check_command			check_nrpe!check_total_procs
        }

B、监控被监控主机上的应用服务

1、监控MySQL

监控MySQL有两种方法,一种是通过官方的check_mysql插件,另一种是通过第三方的check_mysql_health的插件。check_mysql_health的功能更为强大一些,这里我两种都简单的说下。

a、通过官方的check_mysql插件实现

在被监控主机上的数据库添加监控帐号密码

GRANT ALL PRIVILEGES ON *.* TO 'nagios'@'192.168.1.%' IDENTIFIED BY '000000';
Flush Privileges;

commands.cfg加入以下内容

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

define command{
	command_name check_mysql
	command_line $USER1$/check_mysql -H $ARG1$ -P $ARG2$ -u $ARG3$ -p $ARG4$
	}

linuxsever.cfg中加入以下内容

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

define service{
	 use                     generic-service
	 host_name               linuxsever1
	 service_description     mysql
	 check_command           check_mysql!192.168.1.106!3306!nagios!000000
	 notifications_enabled   1
	}

b、通过check_mysql_health插件实现MySQL监控

check_mysql_health插件比起官方的check_mysql插件功能更为强大,check_mysql_health不但能监控MySQL是否正常运行,还能监控MySQL主从、MySQL连接数情况、MySQL慢查询等多种监控指标。

check_mysql_health官方主页:http://labs.consol.de/nagios/check_mysql_health/

安装check_mysql_health

wget http://labs.consol.de/wp-content/uploads/2011/04/check_mysql_health-2.1.5.1.tar.gz
tar xvzf check_mysql_health-2.1.5.1.tar.gz 
cd check_mysql_health-2.1.5.1
./configure
make
make install

check_mysql_health插件基本用法

check_mysql_health --hostname 192.168.1.106 --port 3306 --username nagios --password 000000 --mode slave-io-running

各参数具体含义

--hostname 定义被监控主机的IP或机器名
--port     定义被监控主机上MySQL的运行端口
--username 定义被监控主机上MySQL的用户名
--password  定义被监控主机上MySQL的密码
--mode   定义被监控主机上MySQL的监控指标

监控MySQL主从

GRANT super,process ON *.* TO 'nagios'@'192.168.1.%' IDENTIFIED BY '000000';
Flush Privileges;

commands.cfg加入以下内容

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

define command{
        command_name check_mysql_health
        command_line $USER1$/check_mysql_health --hostname $ARG1$ --port $ARG2$ --username $ARG3$ --password $ARG4$ --mode $ARG5$
        }

linuxsever.cfg中加入以下内容

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             check_mysql_slave_io
        check_command                   check_mysql_health!192.168.1.106!3306!nagios!000000!slave-io-running
        notifications_enabled           1
        }

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             check_mysql_slave_sql
        check_command                   check_mysql_health!192.168.1.106!3306!nagios!000000!slave-sql-running
        notifications_enabled           1
        }

监控MySQL连接时间

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             check_mysql_connection_time
        check_command                   check_mysql_health!192.168.2.50!3306!nagios!PoGtvEu4qkhP8jILruo0!connection-time
        notifications_enabled           1
        }

监控MySQL连接数

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             check_mysql_threads_connected
        check_command                   check_mysql_health!192.168.2.50!3306!nagios!PoGtvEu4qkhP8jILruo0!threads-connected
        notifications_enabled           1
        }

监控MySQL慢查询情况

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             check_mysql_slow_queries
        check_command                   check_mysql_health!192.168.2.50!3306!nagios!PoGtvEu4qkhP8jILruo0!slow-queries
        notifications_enabled           1
        }

监控MySQL锁表情况

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             check_mysql_table_lock_contention
        check_command                   check_mysql_health!192.168.2.50!3306!nagios!PoGtvEu4qkhP8jILruo0!table-lock-contention
        notifications_enabled           1
        }

2、监控Rsync

下载监控脚本check_rsync2

cd /usr/local/nagios/libexec/
wget -O check_rsync2 'http://exchange.nagios.org/components/com_mtree/attachment.php?link_id=307&cf_id=29' 
chown nagios:nagios check_rsync2
chmod 755 check_rsync2

commands.cfg加入以下内容

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

define command{
	command_name check_rsync
	command_line $USER1$/check_rsync2 -H $HOSTADDRESS$ -p $ARG1$
	}

linuxsever.cfg中加入以下内容

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

define service{
	 use                     generic-service
	 host_name               linuxsever1
	 service_description     rsync
	 check_command           check_rsync!873
	 notifications_enabled   1
	}

3、监控HTTP

linuxsever.cfg中加入以下内容

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             HTTP
	check_command			check_http
	notifications_enabled		1
        }

用check_http插件监控某个页面是否包含特定的内容

以监控http://192.168.1.106:8080/checkstatus.php为例

commands.cfg加入以下内容

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

define command{
        command_name check_http_page
        command_line $USER1$/check_http -H $ARG1$ -p $ARG2$ -u $ARG3$ -R $ARG4$
        }

linuxsever.cfg中加入以下内容

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

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             check_http_page_status
        check_command                   check_http_page!192.168.1.106!8080!/checkstatus.php!'ok'!
        notifications_enabled           1
        }

注:这里是用的check_http的-R参数来做搜索的。-R参数是不区分大小的,另外还有-s和-r也可达到类似目的。如下面的例子:

./check_http -H 192.168.1.106 -p 8080 -u /checkstatus.php -R 'ok'
./check_http -H 192.168.1.106 -p 8080 -u /checkstatus.php -s 'Ok'
./check_http -H 192.168.1.106 -p 8080 -u /checkstatus.php -r 'Ok'

-s  预期页面代码中的内容(区分大小写)
-r  检查页面的返回值(区分大小写)
-R  检查页面的返回值(不区分大小写)

4、监控SSH

linuxsever.cfg中加入以下内容

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             SSH
	check_command			check_ssh
	notifications_enabled		1
        }

5、监控PING

linuxsever.cfg中加入以下内容

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       linuxsever1
        service_description             PING
	check_command			check_ping!100.0,20%!500.0,60%
        }

六、一些说明

我整体的Nagios的配置都是以官方自带模板文件(objects/templates.cfg)为基础进行扩展的,这里把模板文件中一些常用选项做一个简单的说明。

主要修改的也只有templates.cfg的一些选项,监控时段定义采用的缺省定义(objects/timeperiods.cfg)。

check_interval          5       #每5分钟检测一次
retry_check_interval    1       #出现问题时每1分钟重检测一次
max_check_attempts      3       #检测失败后最大重试次数
notification_interval   10      #10分钟发送一次警报
notification_period     24x7    #发送警报的时间段
notification_options    w,u,c,r #什么情况发送警报

检测间隔的单位是由nagios.cfg里interval_length定义的,缺省nagios.cfg里面的nterval_length为60秒,就是1分钟!

报警状态说明:

w—报警(warning)
u—未知(unkown)
c—严重(critical)
r—从异常情况恢复正常(recoveries)
d—当机(down)
f—(flapping)检测主机或服务处于抖动状态
s—(scheduled)计划的开始和结束停机。

注:当主机状态过度频繁地变换状态时可以考虑状态处于“抖动”(flapping)。一个明显的例子就是一台主机由于加载操作系统而不断地重启动,这种状态就是处于抖动。

七、给Nagios加入性能图表

PNP4Nagios是一个用于显示Nagios性能图表的插件,基于RRDTool日志分析,并以Kohana(一个PHP开发框架)呈现在Web端,可以以Action Url的形式集成到Nagios中。

A、安装相关环境

apt-get install rrdtool librrds-perl php5-gd

Apache启用rewrite模块

cp /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/

B、安装PNP4Nagios

wget http://sourceforge.net/projects/pnp4nagios/files/PNP-0.6/pnp4nagios-0.6.13.tar.gz
tar zxvf  pnp4nagios-0.6.13.tar.gz
cd pnp4nagios-0.6.13
./configure --with-nagios-user=nagios --with-nagios-group=nagcmd
make all
make install
make install-webconf
make install-config
make install-init

#使用下面这条命令可以替换所有的make命令
make fullinstall

#重启Apache,让PNP4Nagios的Web访问生效
apache2ctl restart

注:如果单独编译安装的rrdtoool,则需使用–with-rrdtool显示指定rrdtool的位置。如:–with-rrdtool=/usr/local/rrdtool-1.2.xx/bin/rrdtool这样的形式

C、配置PNP4Nagios

PNP4Nagios有三种工作模式,分别是Synchronous Mode、Bulk Mode和Bulk Mode with NPCD,下面以Synchronous Mode来配置PNP4Nagios。

修改Nagios的主配置文件

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

process_performance_data=1
service_perfdata_command=process-service-perfdata
host_perfdata_command=process-host-perfdata

修改Nagios的command.cfg配置文件

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

#增加以下两个命令

#process-service-perfdata command definition

define command {
       command_name    process-service-perfdata
       command_line    /usr/local/pnp4nagios/libexec/process_perfdata.pl
}

#process-host-perfdata command definition

define command {
       command_name    process-host-perfdata
       command_line    /usr/local/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA
}

注意:commands.cfg已经定义了相应命令,需要先注释掉。否则后面重启Nagios时,会因为配置文件中命令重复而报错。

D、检查PNP4Nagios是否正确安装

访问http://ip/php4nagios,会进行一些必要的环境测试,如果页面最下方出现[Your environment passed all requirements. Remove or rename the /usr/local/pnp4nagios/share/install.php file now.]这一行绿色的提示就表示PNP4Nagios环境已正确。

如果所有的环境都配置正确了,删除或重命名install.php文件

mv  /usr/local/pnp4nagios/share/install.php  /usr/local/pnp4nagios/share/install.php.bak

E、将性能图集成到nagios的web页面中

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

#在最后添加

define host {
   name       host-pnp
   action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
   register   0
}

define service {
   name       srv-pnp
   action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
   register   0
}

让PNP4Nagios显示的性能图表在Nagios的主机或服务中显示,需要在定义主机和服务时加入host-pnp和srv-pnp配置实例。

具体修改文件视Nagios中定义而定,下面只是一个示例,主要的修改就是在use行加入相应的模板。

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

define host{
        use                     linux-server,host-pnp            ; Name of host template to use
                                                        ; This host definition will inherit all variables that are defined
                                                        ; in (or inherited by) the linux-server host template definition.
        host_name               192.168.1.106
        alias                   192.168.1.106
        address                 192.168.1.106
        }

define service{
        use                             generic-service,srv-pnp         ; Name of service template to use
        host_name                       192.168.1.106
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }

注:如果有多个主机和服务,每一个定义中都需要加入。

F、重启Nagios服务

/etc/init.d/nagios restart

访问http://ip/nagios,点击主机或服务后面小太阳图标就可查看相应图表。

G、弹窗方式显示性能图表

上面的方法必须点击主机或服务后面小太阳图标才能查看相应的性能图表,下面的方法可直接在Nagios页面中以弹出窗口显示性能图表。

cp pnp4nagios-0.6.13/contrib/ssi/status-header.ssi /usr/local/nagios/share/ssi/

注:contrib/ssi/status-header.ssi在源代码目录中
status-header.ssi文件必须没有执行权限

修改Nagios的模板文件

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

define host {
   name       host-pnp
   action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=_HOST_
   register   0
}

define service {
   name       srv-pnp
   action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=$SERVICEDESC$
   register   0
}

注:如果按文中之前的方式定义过,请先注释掉。

重启Nagios服务

/etc/init.d/nagios restart

访问http://ip/nagios,鼠标移动到主机或服务后面小太阳图标上就会显示相应的性能图表。

八、参考文档
  http://www.google.com
  http://www.codelast.com/?p=1037
  http://www.codelast.com/?p=2301
  http://nagios-cn.sourceforge.net
  http://www.rossi.url.tw/wordpress/?p=327
  http://www.viyin.net/2010/03/nginx-nagios.html
  http://blog.csdn.net/wangxiaosen/article/details/5941384
  http://nomyself.info/index.php/2011/06/15/the-alarm-control-nagios/
文章转自:http://www.mike.org.cn/articles/ubuntu-nagios-monitor-platform/

评论

发表回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据