关于 nagios 服务端的安装,请参考:

系统环境

标题 版本 备注
系统版本 ubuntu 22.04.3 LTS (Jammy Jellyfish) -
内核版本 5.15.0-91-generic #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux -
Nagios Plugins v2.4.10 -
NRPE V4.0.2 -

安装 Nagios Plugins

1.点击这里下载 Nagios Plugins 到服务器的 /usr/local/src/ 目录下:

1
wget -O /usr/local/src/nagios-plugins-2.4.10.tar.gz https://nagios-plugins.org/download/nagios-plugins-2.4.10.tar.gz

2.将其解压至下载目录:

1
tar -zxf /usr/local/src/nagios-plugins-2.4.10.tar.gz -C /usr/local/src/

3.进入解压目录:

1
cd /usr/local/src/nagios-plugins-2.4.10

4.配置 ./configure 参数:

1
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios

5.执行 make && make install 进行编译安装

1
make && make install

安装 NRPE

1.点击这里下载 NRPE 源码包 到服务器的 /usr/local/src/ 目录下:

1
wget -O /usr/local/src/nrpe-4.0.2.tar.gz https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.0.2/nrpe-4.0.2.tar.gz

2.将下载的源码文件解压到下载目录:

1
tar -zxf /usr/local/src/nrpe-4.0.2.tar.gz -C /usr/local/src/

3.进入解压目录:

1
cd /usr/local/src/nrpe-4.0.2/

4.配置 ./configure 参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
root@instance-2UkeKOzr:/usr/local/src/nrpe-4.0.2# ./configure --prefix=/usr/local/nagios 
checking for a BSD-compatible install... /usr/bin/install -c
checking what the operating system is ... linux
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
*** Configuration summary for nrpe 4.0.2 2020-03-09 ***:

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
or type 'make' to get a list of make options.

也可以使用 make 命令列出 make 参数对应的功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@instance-2UkeKOzr:/usr/local/src/nrpe-4.0.2# make

Please enter make [option] where [option] is one of:

all builds nrpe and check_nrpe
nrpe builds nrpe only
check_nrpe builds check_nrpe only
install-groups-users add the users and groups if they do not exist
install install nrpe and check_nrpe
install-plugin install the check_nrpe plugin
install-daemon install the nrpe daemon
install-config install the nrpe configuration file
install-inetd install the startup files for inetd, launchd, etc.
install-init install the startup files for init, systemd, etc.

5.接下来依次执行 make allmake installmake install-pluginmake install-config 以及 make install-init

执行 make all:

1
2
3
4
5
6
7
8
9
10
11
root@instance-2UkeKOzr:/usr/local/src/nrpe-4.0.2# make all
cd ./src/; make
make[1]: Entering directory '/usr/local/src/nrpe-4.0.2/src'
gcc -g -O2 -I/usr/include/krb5 -I/usr/local/openssl/include -DHAVE_CONFIG_H -I ../include -I ./../include -o nrpe ./nrpe.c ./utils.c ./acl.c -L/usr/local/openssl/lib -lssl -lcrypto -lnsl -lwrap
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
*** Compile finished ***

You can now continue with the installation or upgrade process.

Read the PDF documentation (docs/NRPE.pdf) for information on the next
steps you should take to complete the installation or upgrade.

执行 make install:

1
make install

执行 make install-plugin:

1
2
3
4
5
6
7
8
9
root@instance-2UkeKOzr:/usr/local/src/nrpe-4.0.2# make install-plugin
cd ./src/; make install-plugin
make[1]: Entering directory '/usr/local/src/nrpe-4.0.2/src'
/usr/bin/install -c -m 755 -d /usr/local/nrpe/bin
/usr/bin/install -c -m 755 ../uninstall /usr/local/nrpe/bin/nrpe-uninstall
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nrpe/libexec
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nrpe/libexec
/usr/bin/install -c -m 775 -o nagios -g nagios check_nrpe /usr/local/nrpe/libexec
make[1]: Leaving directory '/usr/local/src/nrpe-4.0.2/src'

执行 make install-config:

1
2
3
root@instance-2UkeKOzr:/usr/local/src/nrpe-4.0.2# make install-config
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nrpe/etc
/usr/bin/install -c -m 644 -o nagios -g nagios sample-config/nrpe.cfg /usr/local/nrpe/etc

执行 make install-init:

1
2
root@instance-2UkeKOzr:/usr/local/src/nrpe-4.0.2# make install-init
/usr/bin/install -c -m 644 startup/default-service /lib/systemd/system/nrpe.service

6.编辑 /usr/local/nagios/etc/nrpe.cfg 文件,做如下修改:

6.1.设置允许服务端访问:找到 allowed_hosts 关键字,在后面添加服务端IP,如下:

1
allowed_hosts=127.0.0.1,::1,153.24.32.38

6.2.添加自定义监控事项:找到关键字 command ,将默认的几个 command 注释掉,并且在下面添加如下两个:

1
2
3
4
5
6
7
8
9
10
# 系统默认的 command
#command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
#command[check_load]=/usr/local/nagios/libexec/check_load -r -w .15,.10,.05 -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

# 添加的自定义 command
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,4 -c 30,25,20
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 6% -p /

说明:

1
2
3
4
5
6
7
8
9
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,4 -c 30,25,20
命令解释:当1分钟多于15个进程等待,5分钟多于10个,15分钟多于4个则为warning状态
当1分钟多于30个进程等待,5分钟多于25个,15分钟多于20个则为critical状态
[check_load]:相当于模块名
/usr/local/nagios/libexec/check_load:获取资源的命令
也就是说通过调用check_load就相当于执行/usr/local/nagios/libexec/check_load这个命令,下同。

command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 6% -p /
命令解释:-w 20% -c 6% -p /(根分区剩余空间为总大小的20%为warning状态,剩余6%为critical状态,-p后是根分区)

7.启动 nrpe 服务并将其设置为开机启动:

1
systemctl enable --now nrpe.service

8.查看启动状态:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
root@instance-2UkeKOzr:/usr/local/src/nrpe-4.0.2# systemctl status nrpe.service 
● nrpe.service - Nagios Remote Plugin Executor
Loaded: loaded (/lib/systemd/system/nrpe.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2024-05-10 21:07:16 CST; 8s ago
Docs: http://www.nagios.org/documentation
Main PID: 730499 (nrpe)
Tasks: 1 (limit: 4661)
Memory: 816.0K
CPU: 11ms
CGroup: /system.slice/nrpe.service
└─730499 /usr/local/nrpe/bin/nrpe -c /usr/local/nrpe/etc/nrpe.cfg -f

May 10 21:07:16 instance-2UkeKOzr systemd[1]: Started Nagios Remote Plugin Executor.
May 10 21:07:16 instance-2UkeKOzr nrpe[730499]: Starting up daemon
May 10 21:07:16 instance-2UkeKOzr nrpe[730499]: Server listening on 0.0.0.0 port 5666.
May 10 21:07:16 instance-2UkeKOzr nrpe[730499]: Server listening on :: port 5666.
May 10 21:07:16 instance-2UkeKOzr nrpe[730499]: Listening for connections on port 5666
May 10 21:07:16 instance-2UkeKOzr nrpe[730499]: Allowing connections from: 127.0.0.1,::1

在查看下其监听的端口:

1
2
3
root@Win10:/usr/local/src/nrpe-4.1.0# ss -lntp |egrep nrpe
LISTEN 0 5 0.0.0.0:5666 0.0.0.0:* users:(("nrpe",pid=218202,fd=4))
LISTEN 0 5 [::]:5666 [::]:* users:(("nrpe",pid=218202,fd=5))

9.nrpe 测试:

1
2
3
4
5
6
7
8
9
10
root@Win10:/usr/local/src/nrpe-4.1.0# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v4.0.2


root@Win10:/usr/local/src/nrpe-4.1.0# /usr/local/nagios/libexec/check_nrpe -H localhost -c check_disk
DISK OK - free space: / 34515 MiB (90.60% inode=96%);| /=3580MiB;31953;37545;0;39942


root@Win10:/usr/local/src/nrpe-4.1.0# /usr/local/nagios/libexec/check_nrpe -H localhost -c check_load
OK - load average: 0.00, 0.03, 0.12|load1=0.000;15.000;30.000;0; load5=0.030;10.000;25.000;0; load15=0.120;4.000;20.000;0;

安装报错及解决方法

Configure 报错:

错误一:configure: error: Cannot find ssl headers

报错详细信息:

1
2
3
checking for Kerberos include files... configure: WARNING: could not find include files
checking for pkg-config... no
checking for SSL headers... configure: error: Cannot find ssl headers

解决方法:

安装 libssl-dev

1
apt install -y libssl-dev

Make 报错

错误一: undefined reference to `get_dh2048’

报错相信信息:

1
2
3
4
5
6
/usr/bin/ld: /tmp/ccNtbhB5.o: in function `init_ssl':
/usr/local/src/nrpe-4.0.2/src/./nrpe.c:474: undefined reference to `get_dh2048'
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:48: nrpe] Error 1
make[1]: Leaving directory '/usr/local/src/nrpe-4.0.2/src'
make: *** [Makefile:65: all] Error 2

报这个错误的原因是因为 openssl 版本太高,需要装低版本的 openssl,如下:

解决方法:

1.先将系统默认的 openssl 可执行文件备份:

1
mv /usr/bin/openssl{,.bak}

2.然后安装低版本的 openssl:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 备份系统自带高版本 openssl
[ -f '/usr/bin/openssl' ] && mv /usr/bin/openssl{,.bak}

# 从 openssl 官网下载低版本的 openssl
wget -O /usr/lcoal/src/openssl-1.1.1i.tar.gz https://www.openssl.org/source/old/1.1.1/openssl-1.1.1i.tar.gz

# 解压
tar -zxf /usr/local/src/openssl-1.1.1i.tar.gz -C /usr/local/src/

# 进入解压的 openssl 目录
cd /usr/local/src/openssl-1.1.1i

# 配置 openssl 编译参数
./Configure --prefix=/usr/local/openssl --openssldir=/usr/local/openssl -fPIC -shared linux-x86_64

# 编译安装 openssl
make -j $(nproc) && make install

# 然后软连接 openssl1.1.1 两个库文件:
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib/x86_64-linux-gnu/libssl.so.1.1

# 配置新装低版本的 openssl 环境变量:
echo 'PATH=$PATH:/usr/local/openssl/bin' > /etc/profile.d/openssl.sh


# 设置pkgconfig,为默认openssl.cnf设置ENV
echo 'export PKG_CONFIG_PATH=/usr/local/openssl/lib/pkgconfig' >>/etc/profile
echo 'export OPENSSL_CONF=/usr/lib/ssl/openssl.cnf' >>/etc/profile
source /etc/profile

# 最后执行命令 openssl version ,确保输出的版本信息为 1.1.1x 即可!

3.然后重新执行 ./configuremake all 即可!