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

nagios搭建(三):nagios监控linux主机

2012-08-14 13:33 597 查看
本文档介绍nagios监控linux服务器, 因为监控都是依靠插件去完成的,而监控linux主要使用NRPE插件,本文首先简单介绍一下NRPE监控基础,及监控过程,然后一步步的配置一个实例实现监控linux服务

1.NRPE监控插件基础
NRPE总共由两部分组成:
check_nrpe插件,运行在监控主机上。
NRPE daemon,运行在远程的linux主机上(通常就是被监控机)
整个的监控过程:(如下图)
当Nagios需要监控某个远程linux主机的服务或者资源情况时:
1).nagios运行check_nrpe插件,我们要在nagios配置文件中告诉它要检查什么.
2).check_nrpe插件会通过SSL连接到远程的NRPE daemon.
3).NRPE daemon会运行相应的nagios插件来执行检查本地资源或服务.
4).NRPE daemon将检查的结果返回给check_nrpe插件,插件将其递交给nagios做处理.
注意:NRPE daemon需要nagios插件安装在远程被监控linux主机上,否则,daemon不能做任何的监控.别外因为它们间的通信是加密的SSL,所以在编译安装时都要加上选项, ./configure --enable-ssl --with-ssl-lib=/lib/,否则也会出错.



1.平台介绍:
监控机:centos5.5 nagios-3.2.0.tar.gz nagios-cn-3.2.0.tar.bz2 nagios-plugins-1.4.15.tar.gz nrpe-2.8.1.tar.gz 192.168.0.170
被监控机:centos5.5 nrpe-2.8.1.tar.gz nagios-plugins-1.4.15.tar.gz 192.168.0.158
先看一下配置出来的效果吧:




2.安装被监控端:

1.解决perl编译问题
echo "export LC_ALL=C">>/etc/profile
source /etc/profile
service iptables stop
chkconfig iptables off
setenforce 0 关掉selinux
2.解决时间同步问题
当然前提是你的服务器已经安装了ntp的程序,如果没有安装可以:
yum -y install ntp
/usr/sbin/ntpdate pool.ntp.org
uptime
3.安装Nagios所需要的基础软件包
yum install gcc glibc glibc-common -y
yum install gd gd-devdel -y
yum install openssl-devel -y
4.建立nagios的用户
#useradd nagios
#passwd nagios
5.安装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

6.更改目录权限:

chown nagios.nagios /usr/local/nagios

chown -R nagios.nagios /usr/local/nagios/libexec

7.安装nrpe

tar -zxvf nrpe-2.8.1.tar.gz

cd nrpe-2.8.1

./configure

输出结果如下:





make all

输出结果如下:





安装check_nrpe这个插件:

make install-plugin
注:之前说过了check_nrpe这个插件只在监控机上安装就可以了,这里安装它是为了以后的测试

安装daemon:

make install-daemon
安装配置文件:

make install-daemon-config
将NRPE deamon作为xinetd 下的一个服务运行

yum -y install xinetd

make install-xinetd

输出结果如下:





可以看到创建了这个文件/etc/xinetd.d/nrpe

编辑这个脚本:

vim /etc/xinetd.d/nrpe





编辑/etc/services这个文件,添加nrpe服务

vim /etc/services

在最后添加如下:





重启xinetd的服务:

service xinetd restart





查看nrpe是否已经启动:









看到5666已经在监听了

查看nrpe是否能够正常工作





也就是在本地用check_nrpe 连接nrpe daemon 是正常的

8.查看nrpe的监控命令:

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





上面这5行定义的命令分别是检测登陆用户数,cpu负载,sda1的容量,僵尸进程,总进程数

注:监控机能够监控被监控机的命令就在这里定义的,被监控机nrpe.cfg里没有写的监控命令监控机是不能使用的,比如nrpe.cfg里没有定义交换分区check_swap的命令,那监控机就不能监控被监控机的交换分区的使用情况,所以要想使用就需要添加:command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

另外command[]中[]的是说明,不毕和实际命令相同,他只是监控结果的里的说明,比如你要监控/dev/sda1的信息,那就可以这么写:

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

9.定义完了以后可以在本机用check_nrpe的命令来查看一下:

/usr/local/nagios/libexec/check_nrpe –h查看这个命令的用法可以看到用法是check_nrpe –H 被监控的主机 -c要执行的监控命令

注意:-c 后面接的监控命令必须是 nrpe.cfg文件中定义的.也就是NRPE daemon只运行nrpe.cfg中所定义的命令

如我们看一下磁盘的信息:





3.配置监控端:

监控端nagios、插件、汉化包和配置请查看这里:

/article/4257657.html

安装nrpe

1.先安装openssl-devel

yum -y install openssl-devel

2. 安装nrpe

tar -zxvf nrpe-2.8.1.tar.gz

cd nrpe-2.8.1

./configure --enable-ssl --with-ssl-lib=/usr/lib/

make all

make install-plugin

只运行之一步就行了,因为只需要check_nrpe这个插件

3.测试一下监控机check_nrpe和被监控机运行的nrpedaemon的通信





返回nrpe的版本说明通信正常

4.在command.cfg里添加外部构件nrpe

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

添加

#check nrpe

define command{

command_name check_nrpe

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

}

5.配置被监控主机:

vim /usr/local/nagios/etc/nagios.cfg

添加:





6.配置mylinux.cfg

cd /usr/local/nagios/etc/objects/

cp windows.cfg mylinux.cfg

chown nagios.nagios mylinux.cfg

vim mylinux.cfg

写入如下:

define host{

use linux-server

host_name mylinux

alias mylinux

address 192.168.0.158

}

define service{

use generic-service

host_name mylinux

service_description HTTP

check_command check_http

}

define service{

use generic-service

host_name mylinux

service_description SSH

check_command check_ssh

}

define service{

use generic-service

host_name mylinux

service_description check-swap

check_command check_nrpe!check_swap

}

define service{

use generic-service

host_name mylinux

service_description check-load

check_command check_nrpe!check_load

}

define service{

use generic-service

host_name mylinux

service_description check-disk

check_command check_nrpe!check_disk

}

define service{

use generic-service

host_name mylinux

service_description check-users

check_command check_nrpe!check_users

}

define service{

use generic-service

host_name mylinux

service_description otal_procs

check_command check_nrpe!check_total_procs

}

7.查看配置文件是否正确







说明配置正常,我们启动nagios

8.启动nagios





9.登入web查看:





OK!

本文出自 “linux开源-不断的总结....” 博客,请务必保留此出处http://fantefei.blog.51cto.com/2229719/963143
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: