您的位置:首页 > 运维架构 > Linux

Nagios3.0 NRPE 安装 监控远程Linux服务

2009-05-08 18:21 621 查看
Nagios3.0 NRPE 安装 监控远程Linux服务

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
-------------------------------------------------------
11. 安装Xinetd 相关配置文件
[root@KCentOS5A nrpe-2.9]# make install-xinetd
12.相关文件的修改
/etc/xinetd.d/nrpe 文件的修改。
only_from = 127.0.0.1 192.168.1.2
(注意,192.168.1.2监控服务器的IP,也就是所允许192.168.1.2 来监控本机的服务)
/etc/services 文件作如下修改:
nrpe 5666/tcp # NRPE
[root@KCentOS5A nrpe-2.9]# service xinetd restart

如果配置没有问题的话,输入如下命令。查看配置是否配好
[root@KCentOS5A nrpe-2.9]# netstat -at | grep nrpe
tcp 0 0 *:nrpe *:* LISTEN
五.在被监测主机上配置并运行NRPE
1.配置NRPE的主配置文件
usr/local/nagios/etc/nrpe.cfg 配置文件默认就可以了。
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 <config_file> <mode>
运行方法:nrpe -c NRPE配置文件路径 运行模式

Options:
可用选项

-n = Do not use SSL
不使用SSL方式,一般都使用SSL。

<config_file> = Name of config file to use
指定NRPE配置文件路径,这个NRPE主配置文件就是nrpe.cfg。

<mode> = 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
测试NEPE本地是否正常启动
/usr/local/nagios/libexec/check_nrpe –H localhost
如果正确的话,回返回NRPE的版本号
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 和 Nagios 插件)
相对被检测主机来说,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 <host> [-n] [-u] [-p <port>] [-t <timeout>] [-c <command>]
使用方式:check_nrpe -H 主机名 -p NRPE端口 -c NRPE命令名

Options:
选项:

<host> = 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守护进程运行着的。
-------------------------------------------------------
查看监控服务器是否能和远程Linux 192.168.1.3正常通信。能正常通信返回 NRPE的版本号

[root@KCentOS5C ~]#/usr/local/nagios/libexec/check_nrpe –H 192.168.1.3


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远程监控服务:
usr/local/nagios/etc/object/localhost.cfg
在localhost.cfg 添加 define host define service
define host{
use linux-server
host_name remotehost
alias Remote Host
address 192.168.1.3 ;远程Linux服务器的地址
contact_groups admins
}

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

Define service{
use genericPservice
host_name remotehost
service_description SwapRUsage
check_command check_nrpeycheck_swap
}
通过这样的方法可以在localhost文件里添加更多的服务
运行如下命令。如果没有错误就可以重启Nagios 服务
[root@KCentOS5C nrpe-2.9]# /usr/local/nagios/bin/nagios –v /usr/local/ngaios/etc/nagios.cfg

重启 Nagios Service 使配置生效
[root@KCentOS5C nrpe-2.9]# service nagios reload

注意:监控主机和被监控主机如果启动Iptables防火墙的话,测试的时候可以停止iptables服务 Service iptables stop 正真的使用中,需要开启Iptables,同时配置防火墙是Iptables 的5666端口开放。
防火墙配置方法:
/sbin/iptables -A FORWARD -i eth0 -p tcp --dport 5666 -j ACCEPT
参考资料:
1.http://www.diybl.com/course/6_system/linux/Linuxjs/2008712/133002_3.html
2.http://www.thegeekstuff.com/2008/06/how-to-monitor-remote-linux-host-using-nagios-30/
3.官方文档
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: