您的位置:首页 > 移动开发 > IOS开发

nagios上增加nrpe模块达到监控主机CPU、内存、磁盘使用情况

2011-11-08 23:06 821 查看
agios的实现方式:nagios采用分布-集中的管理模式。在nagios服务器上安装nagios主程序,

在被监控主机上安装nagios代理程序。通过nagios主程序和nagios代理程序之间的通讯,监视主机状态。

主程序和代理程序之间有两种工作方式,一种是主程序-->Active-->代理程序;另一种是主程序-->passive-->代理程序;顾名思义,是以一方向另一方主动提出通讯需求来划分的。

一.NRPE的功能和意义:

之 前已经理解了Nagios的服务-命令-插件的工作关系,因此也明确了基本的Nagios服务是无法实现对远程系统的状况进行监测的,基本的Nagios 监测服务只能是本地系统监测以及对远程主机的连通性监测。为了使Nagios的监测服务器能够远程对被监测主机系统上的信息进行获取,比如远程系统上的进 程数、磁盘空间使用状况、所运行的服务等等这些必须要登录远程主机系统上才能了解的信息的话,就必须要依靠NRPE这个核心扩展插件程序,NRPE作为中 间的代理程序,扮演着一手接受着Nagios监测服务器发来的请求,另一手在远程主机系统上获取指定的信息的中间人角色。

二.NRPE的主要工作方式:

如 以上提到的实现Nagios对远程系统的监测,那么首先必须要在被监测的远程主机上除了安装Nagios-plugins插件程序之外还必须安装NRPE 核心扩展插件程序,并将NRPE在被监测的远程主机系统上以守护进程的方式运行起来,开放指定的NRPE监听端口监听着Nagios监测服务器发送过来的 所有监测请求。另外,在Nagios监测服务器上,在必须要安装Nagios-plugins插件程序和NRPE核心扩展插件程序。唯一不同的是, Nagios监测服务器不需要将NRPE作为守护进程运行着,因为它本身一般不需要被别人监测着而是去监测别人,对Nagios监测服务器而言,它只需要 使用到Nagio-plugins插件程序和NRPE扩展插件程序就足够了。

三.在被监控主机上安装Nagios-plugins插件程序:

1.添加Nagio用户

[root@KCentOS5A ~]# useradd nagios

2.准备好Nagios-plugins和NRPE源代码包

[root@KCentOS5A ~]# ll

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

total 2008

-rw------- 1 root root 928 Oct 3 10:33 anaconda-ks.cfg

-rw-r--r-- 1 root root 22760 Oct 3 10:33 install.log

-rw-r--r-- 1 root root 2888 Oct 3 10:33 install.log.syslog

-rw-r--r-- 1 root root 1693979 Sep 30 22:51 nagios-plugins-1.4.10.tar.gz

Nagios-plugins插件程序包

-rw-r--r-- 1 root root 305019 Sep 30 22:58 nrpe-2.9.tar.gz

NRPE扩展插件功能包

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

3.解压Nagios-plugins压缩包

[root@KCentOS5A ~]# tar -zxvf nagios-plugins-1.4.10.tar.gz

4.进入Nagios-plugins插件包目录

[root@KCentOS5A ~]# cd nagios-plugins-1.4.10

5.预配置Nagios-plugins的安装路径

[root@KCentOS5A nagios-plugins-1.4.10]# ./configure --prefix=/usr/local/nagios

6.预配置完成后会反馈信息并生成Makefile

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

config.status: creating po/Makefile

--with-apt-get-command:

--with-ping6-command: /bin/ping6 -n -U -w %d -c %d %s

--with-ping-command: /bin/ping -n -U -w %d -c %d %s

--with-ipv6: yes

--with-mysql: no

--with-openssl: yes

--with-gnutls: no

--with-perl: /usr/bin/perl

--enable-perl-modules: no

--with-cgiurl: /nagios/cgi-bin

--with-trusted-path: /bin:/sbin:/usr/bin:/usr/sbin

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

7.编译Nagios-plugins插件程序

[root@KCentOS5A nagios-plugins-1.4.10]# make

8.安装Nagios-plugins插件程序

[root@KCentOS5A nagios-plugins-1.4.10]# make install

9.查看Nagios-plugins插件程序的安装

[root@KCentOS5A nagios-plugins-1.4.10]# ll /usr/local/nagios/

total 8

drwxr-xr-x 2 root root 4096 Oct 7 01:02 libexec

drwxr-xr-x 3 root root 4096 Oct 7 01:02 share

10.递归更改Nagios主路径的属主

[root@KCentOS5A ~]# chown -R nagios.nagios /usr/local/nagios/

11.检查Nagios主路径的属性

[root@KCentOS5A ~]# ll /usr/local/|grep nagios

drwxr-xr-x 4 nagios nagios 4096 Oct 7 01:02 nagios

[root@KCentOS5A ~]# ll /usr/local/nagios/

total 8

drwxr-xr-x 2 nagios nagios 4096 Oct 7 01:02 libexec

drwxr-xr-x 3 nagios nagios 4096 Oct 7 01:02 share

四.再在被监控主机上安装NRPE扩展插件程序:

1.解压NRPE的压缩包

[root@KCentOS5A ~]# tar -zxvf nrpe-2.9.tar.gz

2.进入NRPE包目录

[root@KCentOS5A ~]# cd nrpe-2.9

3.预配置NRPE安装程序

[root@KCentOS5A nrpe-2.9]# ./configure

4.预配置完毕后会反馈提示信息以及建立Makefile

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

configure: creating ./config.status

config.status: creating Makefile

config.status: creating src/Makefile

config.status: creating subst

config.status: creating include/config.h

*** Configuration summary for nrpe 2.9 08-13-2007 ***:

General Options:

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

NRPE port: 5666

NRPE user: nagios

NRPE group: nagios

Nagios user: nagios

Nagios group: nagios

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

type 'make all' to compile the NRPE daemon and client.

最后提示继续使用“make all”命令来对NRPE守护程序以及客户端程序进行编译。

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

5.对NRPE程序进行编译

[root@KCentOS5A nrpe-2.9]# make all

6.编译成功后会反馈提示信息

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

*** Compile finished ***

If the NRPE daemon and client compiled without any errors, you

can continue with the installation or upgrade process.

Read the PDF documentation (NRPE.pdf) for information on the next

steps you should take to complete the installation or upgrade.

在NRPE的包目录中有一份NRPE.pdf的手册,接下来就可以参考那份手册进行后续的操作了。

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

安装NRPE插件程序,守护程序以及模板守护程序配置文件。

Install the NRPE plugin (for testing), daemon, and sample daemon config file.

7.安装NRPE插件程序

[root@KCentOS5A nrpe-2.9]# make install-plugin

8.安装NRPE守护程序

[root@KCentOS5A nrpe-2.9]# make install-daemon

9.安装NRPE守护程序配置文件

[root@KCentOS5A nrpe-2.9]# make install-daemon-config

10.检查NRPE程序的安装

[root@KCentOS5A nrpe-2.9]# ll /usr/local/nagios/

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

total 16

drwxrwxr-x 2 nagios nagios 4096 Oct 7 01:16 bin

drwxrwxr-x 2 nagios nagios 4096 Oct 7 01:16 etc

drwxr-xr-x 2 nagios nagios 4096 Oct 7 01:15 libexec

drwxr-xr-x 3 nagios nagios 4096 Oct 7 01:02 share

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

五.在被监测主机上配置并运行NRPE:

1.配置NRPE的主配置文件

[root@KCentOS5A ~]# vi /usr/local/nagios/etc/nrpe.cfg

这里主要交待一些关键重要的设定。

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

pid_file=/var/run/nrpe.pid

设定NRPE运行的PID文件,这个默认就设定好了,不用更改。

server_port=5666

这个是NRPE守护进程占用的系统端口。

#server_address=0.0.0.0

server_address=192.168.1.9 #这是被控端IP地址

设定系统监听NRPE的网络接口。一般设定具体的IP地址,如果要多个网卡的话就用逗号“,”来分隔多个IP地址。如果要监听系统全部的网络接口的话,可以使用“0.0.0.0”这个表示全部网络接口的特殊地址,但是不可以用通配符“*”。

nrpe_user=nagios

nrpe_group=nagios

设定NRPE的宿主用户。

allowed_hosts=192.168.1.12 #这是充许监控服务器IP地址

这里是设定允许与本机进行NRPE交互的主机的IP地址,也就是Nagios监测服务器的地址,如果要指定多个Nagios服务器的话,那么用逗号“,”来分隔多个IP地址。

command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10

command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1

command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z

command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

以上是NRPE默认就有的扩展插件命令,这是至关重要的地方了。当被监测主机运行NRPE守护进程的时候,同时就运行起来了这些在NRPE配置文件中定义的NRPE扩展命令了。可以看出这些扩展命令都已经指定了好了命令名、插件路径以及选项参数了。格式如下:

command[NRPE命令名]=插件路径/插件 选项1 参数1 选项2 参数2 ...

注意,如果要指定该被监测主机需要被监控的服务的话,就必须在这里都配置好NRPE命令并运行在NRPE守护进程里。这里没有配置的NRPE命令则Nagios监测服务器是无法监控到的。

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

2.找到NRPE运行的执行程序

[root@KCentOS5A ~]# ll /usr/local/nagios/bin/nrpe

-rwxrwxr-x 1 nagios nagios 93420 Oct 7 01:16 /usr/local/nagios/bin/nrpe

3.了解NRPE的执行程序的使用方式

[root@KCentOS5A ~]# /usr/local/nagios/bin/nrpe -h

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

Usage: nrpe [-n] -c

运行方法:nrpe -c NRPE配置文件路径 运行模式

Options:

可用选项

-n = Do not use SSL

不使用SSL方式,一般都使用SSL。

= Name of config file to use

指定NRPE配置文件路径,这个NRPE主配置文件就是nrpe.cfg。

= One of the following two operating modes:

指定NRPE的运行方式,一共有2种可用的NRPE运行方式:

-i = Run as a service under inetd or xinetd

以超级守护进程inetd或xinetd方式运行NRPE,要通过这种方式运行的话还要安装和配置xinetd,一般不用。

-d = Run as a standalone daemon

以独立守护进程方式运行NRPE,一般常用这种运行方式。

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

4.运行NRPE守护进程

[root@KCentOS5A ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

5.查看系统日志检查NRPE运行情况:

[root@KCentOS5A ~]# tail /var/log/messages

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

Oct 7 01:45:10 KCentOS5A nrpe[21659]: Starting up daemon

说明已经顺利启动了NRPE守护进程。

Oct 7 01:45:10 KCentOS5A nrpe[21659]: Listening for connections on port 5666

说明已经启动了5666端口为NRPE的监听端口。

Oct 7 01:45:10 KCentOS5A nrpe[21659]: Allowing connections from: 192.168.1.12

说明已经打开对指定的Nagios监测服务器的访问允许。

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

6.检查NRPE的进程信息

[root@KCentOS5A ~]# ps aux | grep nrpe

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

nagios 21659 0.0 0.3 4396 868 ? Ss 01:45 0:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

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

7.检查NRPE的网络连接信息

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

[root@KCentOS5A ~]# netstat -nap | grep nrpe

tcp 0 0 192.168.1.9:5666 0.0.0.0:* LISTEN 21659/nrpe

unix 2 [ ] DGRAM 23374 21659/nrpe

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

8.将NRPE运行添加加入系统启动脚本中

[root@KCentOS5A ~]# echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.d/rc.local

到此,被监控主机要做的工作已经完成了。

六.在Nagios监测服务器上安装NRPE核心扩展插件程序

相对被检测主机来说,Nagios监测服务器上做的NRPE安装就比较简单了,而且它主要监测别人而不是被别人监测,所以它不需要将NRPE作为守护进程运行起来,只要拥有NRPE的扩展插件功能就可以了。

1.解压NRPE压缩包

[root@KCentOS5C ~]# tar -zxvf nrpe-2.9.tar.gz

2.进入NRPE包目录

[root@KCentOS5C ~]# cd nrpe-2.9

3.预配置NRPE安装程序

[root@KCentOS5C nrpe-2.9]# ./configure

4.编译NRPE程序

[root@KCentOS5C nrpe-2.9]# make all

5.安装NRPE扩展插件程序

[root@KCentOS5C nrpe-2.9]# make install-plugin

Nagios 只要拥有NRPE的扩展插件功能就可以了,所以在Nagios监测服务器安装NRPE工作到这步就可以了。接下来要做的就是在Nagios监测服务器上配 置NRPE的扩展插件命令,使得Nagios监测服务器能够定义nrpe命令,从而可以定义出“远程系统监测服务”。

七.在监控主机上配置NRPE的服务

1.查看新扩展插件check_nrpe的使用方法

[root@KCentOS5C ~]# /usr/local/nagios/libexec/check_nrpe -h|less

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

Usage: check_nrpe -H [-n] [-u] [-p ] [-t ] [-c ]

使用方式:check_nrpe -H 主机名 -p NRPE端口 -c NRPE命令名

Options:

选项:

= The address of the host running the NRPE daemon

主机,运行着NRPE守护进程的远程被监测主机名,并且该主机名必须在host里定义过。

[port] = The port on which the daemon is running (default=5666)

端口,被监测的远程主机上运行NRPE的端口,默认是5666,如果是默认就不用指定。

[command] = The name of the command that the remote daemon should run

命令,这些命令名必须是被监测主机上NRPE守护进程运行着的。

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

2.在command.cfg命令定义文件中添加NRPE命令。

[root@KCentOS5C ~]# vi /usr/local/nagios/etc/commands.cfg

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

# NRPE Command

添加NRPE功能命令。

define command(

command_name nrpe

command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

}

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

这里要说明几点:

(1)这里定义的命令名就叫作nrpe。

(2)$USER1$/check_nrpe会通过引用resource.cfg获得/usr/local/nagios/libexec/check_nrpe这个绝对路径。

(3)-H $HOSTADDRESS$ 用来获得指定被监测主机的IP地址,$HOSTADDRESS$变量会通过定义主机名查找到host段中的IP地址。

(4)-c $ARG1$ 用来指定被监测主机上NRPE守护进程运行着的NRPE命令名。

6.在Nagios监控服务器上然后按照NRPE命令定义来添加NRPE远程监控服务:

在hosts.cfg 添加

实现监控119服务器硬盘使用情况

define host {

host_name 119

alias bj qmail server

address 被监控主机IP

contact_groups BJ-servers

check_command nrpe!check_sda11

max_check_attempts 5

notification_interval 10

notification_period 24x7

notification_options d,u,r

}

在services.cfg 添加

define service {

host_name 119

service_description Disk Using Of Remote System

check_period 24x7

max_check_attempts 4

normal_check_interval 3

retry_check_interval 2

contact_groups BJ-servers

notification_interval 10

notification_period 24x7

notification_options w,u,c,r

check_command nrpe!check_sda11

}

define service {

保证被监控的主机nrpe.cfg里有check_disk -w 20 -c 10 -p /dev/sda11 #比如要监控第一块硬盘11号分区

command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10

command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

command[check_sda11]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda11

command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z

command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: