系统环境

标题 版本 备注
系统版本 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 -
Tengine v3.1.0 -
PHP v7.4.30 -
MariaDB v10.11.7 -
Nagios Core v4.5.2 -
Nagios Plugins v2.4.10 -
NRPE V4.0.2 -

nagios 简介

Nagios 官方网址: https://www.nagios.org/

Nagios 是一款流行的开源网络监控系统,它提供了全面的主机、服务器、网络监控和报警功能。以下是对 Nagios 的一个简要介绍:

  1. 监控能力

    • 主机资源监控(CPU、内存、磁盘等)
    • 网络服务监控(HTTP、SMTP、FTP等)
    • 网络协议监控(PING、SNMP等)
    • 日志文件监控
    • 数据库监控
    • 中间件监控(如JMX)
    • 云资源监控
  2. 通知警报

    • 支持多种通知方式(邮件、短信、脚本执行等)
    • 支持基于阈值的警报触发
    • 支持多级别的通知escalation
  3. 集中管理

    • 提供基于Web的管理界面
    • 支持分布式监控环境的中央管理
    • 支持LDAP/AD用户认证
  4. 扩展性

    • 提供插件架构,支持自定义插件扩展
    • 支持主动/被动检查
    • 支持并行服务检查
  5. 报告和数据存储

    • 提供多种格式的报告(HTML、CSV等)
    • 使用高性能的日志文件存储监控数据
    • 可通过第三方工具实现数据可视化
  6. 社区支持

    • 拥有庞大的社区支持和文档资源
    • 丰富的第三方插件

Nagios 核心是开源免费的,但其公司 Nagios Enterprises 也提供了商业版本和支持服务。Nagios 被广泛应用于各种规模的企业网络监控,是事实上的网络监控标准之一。

LTMP 环境安装

关于 LTMP 环境中的 Tengine 安装,请参考:在 ubuntu 22.04 中,通过源码编译安装 LTMP 中的 Tengine 服务(不支持 Lua 脚本语言)

关于 nagios 虚拟主机的配置如下:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
server {
listen 80;
server_name _;

# nginx 自带的 http_auth_request_module 认证模块,安装时需要编进去。可以使用 nginx -V 查看是否有该参数
auth_basic "Nagios Access";
auth_basic_user_file /usr/local/nagios/etc/.htpassword;

location / {
root /usr/local/nagios/share;
index index.php;
}

location ~ \.php$ {
root /usr/local/nagios/share;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location /nagios {
alias /usr/local/nagios/share;
}
location /cgi-bin/images {
alias /usr/local/nagios/share/images;
}
location /cgi-bin/stylesheets {
alias /usr/local/nagios/share/stylesheets;
}
location /cgi-bin {
alias /usr/local/nagios/sbin;
}
location ~ .*\.(cgi|pl)?$ {
gzip off;
root /usr/local/nagios/sbin;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
fastcgi_pass unix:/usr/local/nginx/logs/perl-fcgi.sock;
fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
fastcgi_index index.cgi;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_param HTTP_ACCEPT_LANGUAGE en_US;
include fastcgi_params;
fastcgi_read_timeout 60;
}
}

需要注意的是: 运行 nginx 的用户(ps aux|egrep ‘worker process’)必须和下面运行 start_perl_cgi.sh 中定义的用户一致,否则在 nginx 的错误日志中会出现 connect() to unix:/usr/local/nginx/logs/perl-fcgi.sock failed (13: Permission denied) 问题,进而影响 nagios web 端显示不正常的情况!!!

关于 LTMP 环境中的 MariaDB 安装,请参考:在 ubuntu 22.04 中,通过二进制安装 MariaDB 服务

这里不需要创建数据库信息!

关于 LTMP 环境中的 PHP 安装,请参考:在 ubuntu 22.04 中,通过源码编译安装 LNMP 中的 PHP 环境

Perl 模块安装

Perl 模块 - FCGI

1.点击这里下载 FCGI 模块源码包文件 到服务器的 /usr/local/src/ 目录下:

1
wget -O /usr/local/src/FCGI-0.82.tar.gz https://cpan.metacpan.org/authors/id/E/ET/ETHER/FCGI-0.82.tar.gz

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

1
tar -zxf /usr/local/src/FCGI-0.82.tar.gz -C /usr/local/src/

3.进入解压目录:

1
cd /usr/local/src/FCGI-0.82/

4.执行 perl Makefile.PL 命令:

1
perl Makefile.PL

5.执行 make 命令进行编译:

1
make

6.执行 make install 命令进行安装:

1
make install

Perl 模块 - FCGI::ProcManager

1.点击这里下载 FCGI::ProcManager 模块源码包文件 到服务器的 /usr/local/src/ 目录下:

1
2
wget -O /usr/local/src/FCGI-ProcManager-0.28.tar.gz https://cpan.metacpan.org/authors/id/A/AR/ARODLAND/FCGI-ProcMan
ager-0.28.tar.gz

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

1
tar -zxf /usr/local/src/FCGI-ProcManager-0.28.tar.gz -C /usr/local/src/

3.进入解压目录:

1
cd /usr/local/src/FCGI-ProcManager-0.28/

4.执行 perl Makefile.PL 命令:

1
perl Makefile.PL

5.执行 make 命令进行编译:

1
make

6.执行 make install 命令进行安装:

1
make install

Perl 模块 - IO

1.点击这里下载 IO 模块源码包文件 到服务器的 /usr/local/src/ 目录下:

1
wget -O /usr/local/src/IO-1.55.tar.gz https://cpan.metacpan.org/authors/id/T/TO/TODDR/IO-1.55.tar.gz

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

1
tar -zxf /usr/local/src/IO-1.55.tar.gz -C /usr/local/src/

3.进入解压目录:

1
cd /usr/local/src/IO-1.55/

4.执行 perl Makefile.PL 命令:

1
perl Makefile.PL

5.执行 make 命令进行编译:

1
make

6.执行 make install 命令进行安装:

1
make install

Perl 模块 - IO::ALL

1.点击这里下载 IO::ALL 模块源码包文件 到服务器的 /usr/local/src/ 目录下:

1
wget -O /usr/local/src/IO-All-0.87.tar.gz https://cpan.metacpan.org/authors/id/F/FR/FREW/IO-All-0.87.tar.gz

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

1
tar -zxf /usr/local/src/IO-All-0.87.tar.gz -C /usr/local/src/

3.进入解压目录:

1
cd /usr/local/src/IO-All-0.87/

4.执行 perl Makefile.PL 命令:

1
perl Makefile.PL

5.执行 make 命令进行编译:

1
make

6.执行 make install 命令进行安装:

1
make install

安装 perl 脚本

1.在 nginx 安装目录 /usr/local/nginx/ 下创建 perl-fcgi.plstart_perl_cgi.sh 文件:

/usr/local/nignx/ 目录下创建 perl-fcgi.pl 文件,内容如下

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/perl
#
# author Daniel Dominik Rudnicki
# thanks to: Piotr Romanczuk
# email [email protected]
# version 0.4.3
# webpage http://www.nginx.eu/
#
# BASED @ http://wiki.codemongers.com/NginxSimpleCGI
#
#
# use strict;
use FCGI;
use Getopt::Long;
use IO::All;
use Socket;

sub init {
GetOptions( "h" => \$help,
"verbose!"=>\$verbose,
"pid=s" => \$filepid,
"l=s" => \$logfile,
"S:s" => \$unixsocket,
"P:i" => \$unixport) or usage();
usage() if $help;

print " Starting Nginx-fcgi\n" if $verbose;
print " Running with $> UID" if $verbose;
print " Perl $]" if $verbose;

if ( $> == "0" ) {
print "\n\tERROR\tRunning as a root!\n";
print "\tSuggested not to do so !!!\n\n";
exit 1;
}

if ( ! $logfile ) {
print "\n\tERROR\t log file must declared\n"
. "\tuse $0 with option -l filename\n\n";
exit 1;
}
print " Using log file $logfile\n" if $verbose;
"\n\n" >> io($logfile);
addlog($logfile, "Starting Nginx-cfgi");
addlog($logfile, "Running with $> UID");
addlog($logfile, "Perl $]");
addlog($logfile, "Testing socket options");

if ( ($unixsocket && $unixport) || (!($unixsocket) && !($unixport)) ) {
print "\n\tERROR\tOnly one option can be used!\n";
print "\tSuggested (beacuse of speed) is usage UNIX socket -S \n\n";
exit 1;
}

if ($unixsocket) {
print " Daemon listening at UNIX socket $unixsocket\n" if $versbose;
addlog($logfile, "Deamon listening at UNIX socket $unixsocket");
} else {
print " Daemon listening at TCP/IP socket *:$unixport\n" if $verbose;
#
addlog($logfile, "Daemon listening at TCP/IP socket *:$unixport");
}

if ( -e $filepid ) {
print "\n\tERROR\t PID file $filepid already exists\n\n";
addlog($logfile, "Can not use PID file $filepid, already exists.");
exit 1;
}

if ( $unixsocket ) {
print " Creating UNIX socket\n" if $verbose;
$socket = FCGI::OpenSocket( $unixsocket, 10 );
if ( !$socket) {
print " Couldn't create socket\n";
addlog($logfile, "Couldn't create socket");
exit 1;
}
print " Using UNIX socket $unixsocket\n" if $verbose;
} else {
print " Creating TCP/IP socket\n" if $verbose;
$portnumber = ":".$unixport;
$socket = FCGI::OpenSocket( $unixport, 10 );
if ( !$socket ) {
print " Couldn't create socket\n";
addlog($logfile, "Couldn't create socket");
exit 1;
}
print " Using port $unixport\n" if $verbose;
}
addlog($logfile, "Socket created");

if ( ! $filepid ) {
print "\n\tERROR\t PID file must declared\n"
. "\tuse $0 with option -pid filename\n\n";
exit 1;
}
print " Using PID file $filepid\n" if $verbose;
addlog($logfile, "Using PID file $filepid");

my $pidnumber = $$;
$pidnumber > io($filepid);
print " PID number $$\n" if $verbose;
addlog($logfile, "PID number $pidnumber");

}

sub addzero {
my ($date) = shift;
if ($date < 10) {
return "0$date";
}
return $date;
}

sub logformat {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$iddst) = localtime(time);
my $datestring;
$year += 1900;
$mon++;
$mon = addzero($mon);
$mday = addzero($mday);
$min = addzero($min);
$datestring = "$year-$mon-$mday $hour:$min";
return($datestring);
}

sub addlog {
my ($log_file, $log_message) = @_;
my $curr_time = logformat();
my $write_message = "[$curr_time] $log_message";
$write_message >> io($log_file);
"\n" >> io($log_file);
}

sub printerror {
my $message = @_;
print "\n Nginx FastCGI\tERROR\n"
. "\t $message\n\n";
exit 1;
}

sub usage {
print "\n Nginx FastCGI \n"
. "\n\tusage: $0 [-h] -S string -P int\n"
. "\n\t-h\t\t: this (help) message"
. "\n\t-S path\t\t: path for UNIX socket"
. "\n\t-P port\t\t: port number"
. "\n\t-p file\t\t: path for pid file"
. "\n\t-l file\t\t: path for logfile"
. "\n\n\texample: $0 -S /var/run/nginx-perl_cgi.sock -l /var/log/nginx/nginx-cfgi.log -pid /var/run/nginx-fcgi.pid\n\n";
exit 1;
}


init;
#
END() { } BEGIN() { }
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit};
if ($@) {
exit unless $@ =~ /^fakeexit/;
} ;

# fork part
my $pid = fork();

if( $pid == 0 ) {
&main;
exit 0;
}

print " Forking worker process with PID $pid\n" if $verbose;
addlog($logfile, "Forking worker process with PID $pid");
print " Update PID file $filepid\n" if $verbose;
addlog($logfile, "Update PID file $filepid");
$pid > io($filepid);
print " Worker process running.\n" if $verbose;
addlog ($logfile, "Parent process $$ is exiting");
exit 0;

sub main {
$request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );
if ($request) { request_loop()};
FCGI::CloseSocket( $socket );
}

sub request_loop {
while( $request->Accept() >= 0 ) {
# processing any STDIN input from WebServer (for CGI-POST actions)
$stdin_passthrough = '';
$req_len = 0 + $req_params{'CONTENT_LENGTH'};
if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){
while ($req_len) {
$stdin_passthrough .= getc(STDIN);
$req_len--;
}
}

# running the cgi app
if ( (-x $req_params{SCRIPT_FILENAME}) &&
(-s $req_params{SCRIPT_FILENAME}) &&
(-r $req_params{SCRIPT_FILENAME})
){
foreach $key ( keys %req_params){
$ENV{$key} = $req_params{$key};
}
if ( $verbose ) {
addlog($logfile, "running $req_params{SCRIPT_FILENAME}");
}
# http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens
#
###20160120 liaoxg
#open $cgi_app, '-|', $req_params{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n"; # addlog($logfile, "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !");
open $cgi_app, '-|', "echo '$stdin_passthrough' | '$req_params{SCRIPT_FILENAME}'" or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n"; # addlog($logfile, "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !");


if ($cgi_app) {
print <$cgi_app>;
close $cgi_app;
}
} else {
print("Content-type: text/plain\r\n\r\n");
print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
addlog($logfile, "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.");
}
}
}

/usr/local/nignx/ 目录下创建 start_perl_cgi.sh 文件,内容如下

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
33
34
35
36
37
38
39
40
#!/bin/bash
#set -x
dir=/usr/local/nginx/

stop ()
{
#pkill -f $dir/perl-fcgi.pl
kill $(cat $dir/logs/perl-fcgi.pid)
rm $dir/logs/perl-fcgi.pid 2>/dev/null
rm $dir/logs/perl-fcgi.sock 2>/dev/null
echo "stop perl-fcgi done"
}

start ()
{
rm $dir/now_start_perl_fcgi.sh 2>/dev/null

chown nginx.nginx $dir/logs
echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh

chown nginx.nginx $dir/now_start_perl_fcgi.sh
chmod u+x $dir/now_start_perl_fcgi.sh

sudo -u nginx $dir/now_start_perl_fcgi.sh
echo "start perl-fcgi done"
}

case $1 in
stop)
stop
;;
start)
start
;;
restart)
stop
start
;;
esac

2.赋予两个脚本可执行权限:

1
chmod +x /usr/local/nginx/{perl-fcgi.pl,start_perl_cgi.sh}

3.启动 perl-cgi:

1
2
root@instance-2UkeKOzr:/usr/local/nginx# ./start_perl_cgi.sh start
start perl-fcgi done

4.确保在 nginx 日志目录 /usr/local/nginx/logs/ 目录下有生成 perl-fcgi.sockperl-fcgi.log 以及 perl-fcgi.pid 三个文件:

1
2
3
4
root@instance-2UkeKOzr:/usr/local/nginx# ls -al logs/ |egrep -i perl
-rw-rw-r-- 1 nginx nginx 581 May 11 07:32 perl-fcgi.log
-rw-rw-r-- 1 nginx nginx 6 May 11 07:32 perl-fcgi.pid
srwxrwxr-x 1 nginx nginx 0 May 11 07:32 perl-fcgi.sock

nagios 安装

1.添加运行 nagios 系统用户:

1
useradd -s /sbin/nologin nagios

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

1
wget -O /usr/local/src/nagios-4.5.2.tar.gz https://go.nagios.org/get-core/4-5-2/nagios-4.5.2.tar.gz

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

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

4.进入解压目录:

1
cd /usr/local/src/nagios-4.5.2

5.配置 ./configure 参数:

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

6.接下来,依次执行 makemake testmake installmake install-daemoninitmake install-initmake install-commandmode 以及 make install-config,如下:

执行 make :

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
root@instance-2UkeKOzr:/usr/local/src/nagios-4.5.2# make all 
d ./base && make
make[1]: Entering directory '/usr/local/src/nagios-4.5.2/base'
gcc -Wall -I.. -I. -I../lib -I../include -I../include -I.. -g -O2 -I/usr/include/krb5 -I/usr/local/openssl/include -DHAVE_CONFIG_H -DNSCORE -c -o nagios.o ./nagios.c
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
*** Compile finished ***

If the main program and CGIs compiled without any errors, you
can continue with testing or installing Nagios as follows (type
'make' without any arguments for a list of all possible options):

make test
- This runs the test suite

make install
- This installs the main program, CGIs, and HTML files

make install-init
- This installs the init script in /lib/systemd/system

make install-daemoninit
- This will initialize the init script
in /lib/systemd/system

make install-groups-users
- This adds the users and groups if they do not exist

make install-commandmode
- This installs and configures permissions on the
directory for holding the external command file

make install-config
- This installs *SAMPLE* config files in /usr/local/nagios/etc
You'll have to modify these sample files before you can
use Nagios. Read the HTML documentation for more info
on doing this. Pay particular attention to the docs on
object configuration files, as they determine what/how
things get monitored!

make install-webconf
- This installs the Apache config file for the Nagios
web interface

make install-exfoliation
- This installs the Exfoliation theme for the Nagios
web interface

make install-classicui
- This installs the classic theme for the Nagios
web interface


*** Support Notes *******************************************

If you have questions about configuring or running Nagios,
please make sure that you:

- Look at the sample config files
- Read the documentation on the Nagios Library at:
https://library.nagios.com

before you post a question to one of the mailing lists.
Also make sure to include pertinent information that could
help others help you. This might include:

- What version of Nagios you are using
- What version of the plugins you are using
- Relevant snippets from your config files
- Relevant error messages from the Nagios log file

For more information on obtaining support for Nagios, visit:

https://support.nagios.com

*************************************************************

Enjoy.

执行 make test

1.在执行 make test 命令之前,先将 nagion 解压目录所属主和组都修改为 nagios, 否则执行 make test 会遇到 Error: Unable to write to check_result_path ('/usr/local/src/nagios-4.5.2/t/var/spool/checkresults') - Permission denied 的错误:

1
chown -R nagios:nagios /usr/local/src/nagios-4.5.2

2.在执行 make test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
root@instance-2UkeKOzr:/usr/local/src/nagios-4.5.2# make test
cd ./base && make
make[1]: Entering directory '/usr/local/src/nagios-4.5.2/base'
make -C ../lib
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
prove ./*.t :: /usr/local/src/nagios-4.5.2
./610cgistatus.t .................. ok
./611cgistatus-hosturgencies.t .... ok
./615cgierror.t ................... ok
./616cginotification.t ............ ok
./617statuswml.t .................. ok
./618cgisecurity.t ................ ok
./622extinfo-local.t .............. ok
./623cmd-local.t .................. ok
./660status-downtimes-comments.t .. ok
./705nagiostats.t ................. ok
./900-configparsing.t ............. ok
./910-noservice.t ................. ok
./920-nocontactgroup.t ............ ok
./930-emptygroups.t ............... ok
All tests successful.
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
NOTE: You must run configure with --enable-libtap to run the C tap tests
make[1]: Leaving directory '/usr/local/src/nagios-4.5.2'

执行 make install

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@instance-2UkeKOzr:/usr/local/src/nagios-4.5.2# make install
cd ./base && make install
make[1]: Entering directory '/usr/local/src/nagios-4.5.2/base'
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/bin
/usr/bin/install -c -s -m 774 -o nagios -g nagios nagios /usr/local/nagios/bin
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
*** Main program, CGIs and HTML files installed ***

You can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):

make install-init
- This installs the init script in /lib/systemd/system

make install-commandmode
- This installs and configures permissions on the
directory for holding the external command file

make install-config
- This installs sample config files in /usr/local/nagios/etc

make[1]: Leaving directory '/usr/local/src/nagios-4.5.2'

执行 make install-daemoninit

1
2
3
4
5
6
root@instance-2UkeKOzr:/usr/local/src/nagios-4.5.2# make install-daemoninit 
/usr/bin/install -c -m 755 -d -o root -g root /lib/systemd/system
/usr/bin/install -c -m 755 -o root -g root startup/default-service /lib/systemd/system/nagios.service
Created symlink /etc/systemd/system/multi-user.target.wants/nagios.service → /lib/systemd/system/nagios.service.

*** Init script installed ***

执行 make install-daemoninit

1
2
3
root@instance-2UkeKOzr:/usr/local/src/nagios-4.5.2# make install-init 
/usr/bin/install -c -m 755 -d -o root -g root /lib/systemd/system
/usr/bin/install -c -m 755 -o root -g root startup/default-service /lib/systemd/system/nagios.service

执行 make install-commandmode

1
2
3
4
5
root@instance-2UkeKOzr:/usr/local/src/nagios-4.5.2# make install-commandmode 
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/var/rw
chmod g+s /usr/local/nagios/var/rw

*** External command directory configured ***

执行 make install-config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
root@instance-2UkeKOzr:/usr/local/src/nagios-4.5.2# make install-config 
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc/objects
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/nagios.cfg /usr/local/nagios/etc/nagios.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/cgi.cfg /usr/local/nagios/etc/cgi.cfg
/usr/bin/install -c -b -m 660 -o nagios -g nagios sample-config/resource.cfg /usr/local/nagios/etc/resource.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/templates.cfg /usr/local/nagios/etc/objects/templates.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/commands.cfg /usr/local/nagios/etc/objects/commands.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/contacts.cfg /usr/local/nagios/etc/objects/contacts.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/timeperiods.cfg /usr/local/nagios/etc/objects/timeperiods.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/localhost.cfg /usr/local/nagios/etc/objects/localhost.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/windows.cfg /usr/local/nagios/etc/objects/windows.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/printer.cfg /usr/local/nagios/etc/objects/printer.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/switch.cfg /usr/local/nagios/etc/objects/switch.cfg

*** Config files installed ***

Remember, these are *SAMPLE* config files. You'll need to read
the documentation for more information on how to actually define
services, hosts, etc. to fit your particular needs.

最后,如果你使用的 Apache 作为 web 服务,则还需要执行 make install-webconf

Nagios web 端安装

1.(在没有安装 apache的 htpasswd 命令情况下 )打开网址 https://tool.oschina.net/htpasswd 生成帐号密码,然后将其复制;

2.在 nagios 安装目录 /usr/local/nagios/etc/ 下创建 .htpassword 的文件,将上面生成的账号密码内容复制粘贴到该文件中:

1
leazhi:$apr1$fcvOen3f$u1LdZ/UKrg33MowLbIJWd0

3.打开浏览器,输入服务器IP,然后输入上面的账号密码,访问 nagios 主页,如图:

nagios 配置

1.备份 /usr/local/nagios/etc/cgi.cfg 文件:

1
cp /usr/local/nagios/etc/cgi.cfg{,.bak}

2.授权 nagios web 端用户访问,否则 nagios 认证用户登陆后,无法访问到 nagios 监控内容:

1
sed -i 's/nagiosadmin/nagiosadmin,leazhi/g' /usr/local/nagios/etc/cgi.cfg

3.将 nagios 目录所属主和组都修改为 nagios :

1
chown -R nagios:nagios /usr/local/nagios/

4.重启下 nagios:

1
systemctl restart nagios.service

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/nrpe 
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.启动 nrpe 服务并将其设置为开机启动:

1
systemctl enable --now nrpe.service

7.查看启动状态:

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

8.nrpe 测试:

1
2
root@instance-2UkeKOzr:/usr/local/src/nrpe-4.0.2# /usr/local/nrpe/libexec/check_nrpe -H localhost
NRPE v4.0.2

报错及解决方法

configure 报错:

make 报错:

参考文档:

果果.IT - lnmp 安装nagios
CNBLOGS - Linux下Nagios的安装与配置
CSDN - nagios安装与配置教程(详细版)【入门教程】2020-11-16
CSDN - Nagios的安装和基本配置