上一篇文章介绍了 Nagios服务器的安装,本文继续介绍如何添加被监控的服务器,内容比较枯燥,都是安装过程,但希望能对准备使用Nagios的小伙伴有点帮助
远程监控原理
Nagios 与远程主机的沟通有多种方式,例如 SNMP、Nrpe、SSH 等,这里使用 Nrpe
Nrpe 有两部分组成:
1)check_nrpe插件,位于Nagios服务器
2)nrpe daemon,运行在远程主机上,是被监控主机的agent
监控远程主机的过程:
1)Nagios 运行 check_nrpe 插件,说明要获取哪些信息
2)check_nrpe 连接到远程的 nrpe daemon
3)nrpe daemon 运行相应的插件执行检查
4)nrpe daemon 将检查结果返回给 check_nrpe,然后交给nagios处理
安装配置过程
1)被监控主机中安装 Nagios-plugins 和 Nrpe
添加用户
$ useradd -s /sbin/nologin nagios
安装 Nagios-plugins
$ yum -y install gcc gcc-c++ make openssl openssl-devel
$ wget http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz
$ tar zxf nagios-plugins-2.1.1.tar.gz
$ cd nagios-plugins-2.1.1
$ ./configure --with-nagios-user=nagios --with-nagios-group=nagios
$ make && make install
安装 Nrpe
下载地址
https://sourceforge.net/projects/nagios/files/latest/download?source=files
得到 nrpe-3.0.1.tar.gz
$ tar xf nrpe-3.0.1.tar.gz
$ cd nrpe-3.0.1
$ ./configure --with-nrpe-user=nagios \
> --with-nrpe-group=nagios \
> --with-nagios-user=nagios \
> --with-nagios-group=nagios \
> --enable-command-args \
> --enable-ssl
$ make all
$ make install-plugin
$ make install-daemon
$ cp sample-config/nrpe.cfg /usr/local/nagios/etc
编辑 /usr/local/nagios/etc/nrpe.cfg
找到 allowed_hosts(这项是设置允许哪些IP访问,多个IP间用逗号分隔),在后面添加 nagios 服务器的IP
保存后启动 nrpe
$ /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
执行 netstat -tnlp 检查 5666 端口是否正常
2)Nagios服务器的安装与配置
安装 Nrpe
下载 nrpe-3.0.1.tar.gz
$ tar xf nrpe-3.0.1.tar.gz
$ cd nrpe-3.0.1
$ ./configure --with-nrpe-user=nagios \
> --with-nrpe-group=nagios \
> --with-nagios-user=nagios \
> --with-nagios-group=nagios \
> --enable-command-args \
> --enable-ssl
(注:user与group名称根据自己情况修改)
$ make all
$ make install-plugin
安装完成后,会在Nagios安装目录的libexec下生成check_nrpe的插件
$ cd /usr/local/nagios/libexec/
$ ll -d check_nrpe
检查和被监控主机是否正常沟通
./check_nrpe -H 被监控主机IP
如果成功,会返回被监控主机中nrpe的版本号
配置
$ cd /usr/local/nagios/etc/objects
修改 commands.cfg
末尾添加:
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H "$HOSTADDRESS$" -c "$ARG1$"
}
新建服务配置文件
vim myserver1.cfg
添加内容:
define host{
use linux-server
host_name MyServer1
alias MyServer1
address 192.168.31.188
}
define hostgroup{
hostgroup_name linux-servers2
alias linux Servers
members MyServer1
}
define service{
use generic-service
host_name MyServer1
service_description Load
check_command check_nrpe!check_load
}
需要修改 host 中 address 为被监控主机的IP,保存退出,这里是定义了被监控主机信息,和监控服务,只定义了一个 check_load 服务,以后需要其他服务时,就在此文件中添加
把新建的服务配置文件添加到 nagios 配置文件中
vim /usr/local/nagios/etc/nagios.cfg
找到 cfg_file 这个部分,在后面添加:
cfg_file=/usr/local/nagios/etc/objects/myserver1.cfg
保存退出
配置文件语法检查
$ service nagios configtest
重新启动nagios服务
$ service nagios restart
登录 nagios web 界面,就可以看到新增加的主机信息了