nagios客户端(被监控主机)安装部署

1. 被监控主机环境准备

1.1 主机信息+IP地址

#一台虚拟机
hostname:nagios-client
ip:ip_address

1.2 查看主机的系统版本信息

[root@nagios-client ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 

1.3 暂时关掉Selinux和防火墙

[root@nagios-client ~]# sed -i "s#enforcing#disabled#g" /etc/sysconfig/selinux 
[root@nagios-client ~]# cat /etc/sysconfig/selinux 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     disabled - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of disabled.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 
[root@nagios-client ~]# setenforce 0  &&    getenforce
Permissive
---------------------------------------------------------------------------------------------------------------------------------------
[root@nagios-client ~]# firewall-cmd --state
running
[root@nagios-client ~]# systemctl stop firewalld.service
[root@nagios-client ~]# firewall-cmd --state               
not running

1.4 安装需要的依赖以及更新系统时间软件

[root@nagios-client ~]#  yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel ntpdate
[root@nagios-client ~]#  ntpdate pool.ntp.org 

1.5 创建nagios用户和用户组

[root@nagios-client ~]# useradd    nagios&& echo "Aa123456" | passwd --stdin nagios
[root@nagios-client ~]# id nagios
uid=1004(nagios) gid=1004(nagios) 组=1004(nagios)

2. nagios被监控端环境搭建

说明:被监控端需要安装nrpe软件,但因为nrpe又依赖于nagios-plugins,所以需要先下载安装nagios-plugins,nagios-plugins最好和服务端的版本保持一致。

2.1 下载nrpe和nagios-plugins安装包

2.1.1 nagios插件
[root@nagios-client ~]# cd /home/tools
[root@nagios-client tools]# wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
[root@nagios-client tools]# ll
-rw-r--r--.  1 root root   2782610 3月  12 00:58 nagios-plugins-2.3.3.tar.gz

2.1.2 nagios客户端代理检测程序
[root@nagios-client ~]# cd /home/tools
[root@nagios-client tools]# wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.0/nrpe-3.2.0.tar.gz
[root@nagios-client tools]# ll
总用量 14072
-rw-r--r--.  1 root root  2782610 3月  12 00:58 nagios-plugins-2.3.3.tar.gz
-rw-r--r--.  1 root root   517743 6月  28 2017 nrpe-3.2.0.tar.gz

2.2 安装部署nagios插件

2.2.1 解压nagios插件安装包
[root@nagios-client tools]# tar xf nagios-plugins-2.3.3.tar.gz
[root@nagios-client tools]# ll
总用量 10076
drwxr-xr-x. 15 root root     4096 3月  12 00:58 nagios-plugins-2.3.3
-rw-r--r--.  1 root root  2782610 3月  12 00:58 nagios-plugins-2.3.3.tar.gz
-rw-r--r--.  1 root root   517743 6月  28 2017 nrpe-3.2.0.tar.gz 
2.2.2 配置编译
[root@nagios-client tools]# cd nagios-plugins-2.3.3
[root@nagios-client nagios-plugins-2.3.3]# ll
总用量 3452
-rw-r--r--. 1 root root   80541 1月  17 2017 ABOUT-NLS
-rw-r--r--. 1 root root    2332 1月  17 2017 acinclude.m4
-rw-r--r--. 1 root root    1275 1月  17 2017 ACKNOWLEDGEMENTS
-rw-r--r--. 1 root root   42202 3月  12 00:57 aclocal.m4
-rw-r--r--. 1 root root     417 12月  5 05:53 AUTHORS
drwxr-xr-x. 3 root root    4096 3月  12 00:58 build-aux
-rw-r--r--. 1 root root  890609 3月  12 00:58 ChangeLog
-rw-r--r--. 1 root root    4918 12月  5 05:53 CODING
-rw-r--r--. 1 root root   78119 3月  12 00:58 config.h.in
-rwxr-xr-x. 1 root root   18163 1月  17 2017 config.rpath
drwxr-xr-x. 2 root root      56 3月  12 00:58 config_test
-rwxr-xr-x. 1 root root 2022791 3月  12 00:58 configure  #编译安装脚本
-rw-r--r--. 1 root root   69121 3月  12 00:57 configure.ac
-rw-r--r--. 1 root root   35147 1月  17 2017 COPYING
-rw-r--r--. 1 root root    3716 1月  17 2017 FAQ
drwxr-xr-x. 4 root root    8192 3月  12 00:58 gl
-rw-r--r--. 1 root root   15578 3月  12 00:58 INSTALL
-rw-r--r--. 1 root root     443 1月  17 2017 LEGAL
drwxr-xr-x. 3 root root    4096 3月  12 00:58 lib
drwxr-xr-x. 2 root root      54 3月  12 00:58 m4
-rw-r--r--. 1 root root    2144 12月  5 05:53 Makefile.am
-rw-r--r--. 1 root root   67135 3月  12 00:58 Makefile.in
-rwxr-xr-x. 1 root root    3474 1月  17 2017 mkinstalldirs
-rw-r--r--. 1 root root    6471 3月  12 00:58 nagios-plugins.spec
-rw-r--r--. 1 root root    6491 12月  5 05:53 nagios-plugins.spec.in
-rw-r--r--. 1 root root   46403 3月  12 00:57 NEWS
-rw-r--r--. 1 root root   17504 12月  5 05:53 NPTest.pm
-rwxr-xr-x. 1 root root    1161 3月  12 00:57 NP-VERSION-GEN
drwxr-xr-x. 2 root root    4096 3月  12 00:58 perlmods
drwxr-xr-x. 5 root root      46 1月  17 2017 pkg
drwxr-xr-x. 4 root root    4096 3月  12 00:58 plugins
drwxr-xr-x. 3 root root     101 3月  12 00:58 plugins-root
drwxr-xr-x. 3 root root    4096 3月  12 00:58 plugins-scripts
drwxr-xr-x. 2 root root    4096 3月  12 00:58 po
-rw-r--r--. 1 root root    5440 12月  5 05:53 README
-rw-r--r--. 1 root root       6 3月  12 00:58 release
-rw-r--r--. 1 root root    3304 1月  17 2017 REQUIREMENTS
-rw-r--r--. 1 root root    3871 1月  17 2017 SUPPORT
drwxr-xr-x. 2 root root      79 3月  12 00:58 tap
-rwxr-xr-x. 1 root root     831 1月  17 2017 test.pl.in
-rw-r--r--. 1 root root    6265 12月  5 05:54 THANKS
drwxr-xr-x. 2 root root      81 3月  12 00:58 tools

指定安装路径以及其他需要的参数

[root@nagios-client nagios-plugins-2.3.3]# ./configure  --prefix=/usr/local/nagios \
--with-mysql=/usr/local/mysql    #如果需要监控客户端的mysql的运行情况,必须在编译安装插件时加此选项
2.2.3 编译安装
[root@nagios-server nagios-plugins-2.3.3]#  make &&  make install  && echo $?
2.2.4 验证nagios插件程序是否被正确安装

切换目录到安装路径(这里是/usr/local/nagios/libexec) ,下看是否存在很多 check文件,若有则安装插件成功,若无就需要重新安装。

root@nagios-client nagios-plugins-2.3.3]#  chown -R nagios.nagios /usr/local/nagios
[root@nagios-client nagios-plugins-2.3.3]# ll /usr/local/nagios/
总用量 4
drwxr-xr-x. 2 nagios nagios    6 5月   9 14:59 include
drwxr-xr-x. 2 nagios nagios 4096 5月   9 14:59 libexec  ######
drwxr-xr-x. 3 nagios nagios   19 5月   9 14:59 share
[root@nagios-client nagios-plugins-2.3.3]# ll /usr/local/nagios/libexec/

2.3 安装部署nrpe软件

2.3.1 解压安装包
[root@nagios-client nagios-plugins-2.3.3]# cd /home/tools
[root@nagios-client tools]# tar xf nrpe-3.2.0.tar.gz
[root@nagios-client tools]# ll
总用量 10076
drwxr-xr-x. 15 root root     4096 3月  12 00:58 nagios-plugins-2.3.3
-rw-r--r--.  1 root root  2782610 3月  12 00:58 nagios-plugins-2.3.3.tar.gz
drwxr-xr-x. 15 root root     4096 3月  12 00:58  nrpe-3.2.0
-rw-r--r--.  1 root root   517743 6月  28 2017 nrpe-3.2.0.tar.gz 
2.3.2 配置编译

说明:nrpe3.0之前版本将nrpe作为xinetd下独立服务运行,nrpe3.0以后的版本不需要了,所以和以前版本的安装步骤有一些不同。

[root@nagios-client tools]# cd nrpe-3.2.0
[root@nagios-client nrpe-3.2.0]# ll
总用量 392
-rwxrwxr-x. 1 root root    283 6月  28 2017 aclocal.m4
drwxrwxr-x. 2 root root     80 6月  28 2017 build-aux
-rw-rw-r--. 1 root root  18788 6月  28 2017 CHANGELOG.md
-rwxrwxr-x. 1 root root 249031 6月  28 2017 configure  #编译安装脚本
-rw-rw-r--. 1 root root  11389 6月  28 2017 configure.ac
drwxrwxr-x. 2 root root     65 6月  28 2017 contrib
drwxrwxr-x. 2 root root     36 6月  28 2017 docs
drwxrwxr-x. 2 root root     81 6月  28 2017 include
-rw-rw-r--. 1 root root    441 6月  28 2017 LEGAL
-rw-rw-r--. 1 root root  15173 6月  28 2017 LICENSE.md
drwxrwxr-x. 2 root root   4096 6月  28 2017 macros
-rw-rw-r--. 1 root root   6915 6月  28 2017 Makefile.in
-rw-rw-r--. 1 root root   5879 6月  28 2017 nrpe.spec.in
-rwxrwxr-x. 1 root root   3248 6月  28 2017 paths.in
-rw-rw-r--. 1 root root   9807 6月  28 2017 README.md
-rw-rw-r--. 1 root root  11692 6月  28 2017 README.SSL.md
drwxrwxr-x. 2 root root     24 6月  28 2017 sample-config
-rw-rw-r--. 1 root root   3647 6月  28 2017 SECURITY.md
drwxrwxr-x. 2 root root     99 6月  28 2017 src
drwxrwxr-x. 2 root root   4096 6月  28 2017 startup
-rwxrwxr-x. 1 root root     74 6月  28 2017 test-wrapper
-rw-rw-r--. 1 root root    782 6月  28 2017 THANKS
-rwxrwxr-x. 1 root root    981 6月  28 2017 travis-test-1
-rwxrwxr-x. 1 root root  10637 6月  28 2017 uninstall.in
-rwxrwxr-x. 1 root root   4326 6月  28 2017 update-cfg.pl
-rwxrwxr-x. 1 root root   3067 6月  28 2017 update-version

不需要指定安装路径以及其他需要的参数

[root@nagios-client nrpe-3.2.0]# ./configure && echo $?
2.3.3 编译安装

make all 编译所有

[root@nagios-client nrpe-3.2.0]# make all && echo $?

make install 编译安装

[root@nagios-client nrpe-3.2.0]# make install && echo $?

安装check_nrpe 这个插件

[root@nagios-client nrpe-3.2.0]# make install-plugin && echo $?

安装deamon

[root@nagios-client nrpe-3.2.0]# make install-daemon && echo $?

安装配置文件

[root@nagios-client nrpe-3.2.0]# make install-daemon-config  && echo $?
make: *** 没有规则可以创建目标“install-xinetd”。 停止。

# nrpe3.0之前版本需要执行 make install-daemon-config
# nrpe3.0以上版本执行此命令时会报上面这个错误提示没有这个参数。

# 现在应该执行以下命令:
[root@nagios-client nrpe-3.2.0]# make install-config && echo $?
这时可以看到安装时修改了etc目录的权限,把配置文件复制到了etc目录下。

安装启动文件

[root@nagios-client nrpe-3.2.0]# make install-xinetd  && echo $?
make: *** 没有规则可以创建目标“install-xinetd”。 停止。

# nrpe3.0之前版本将nrpe作为xinetd下独立服务运行
# nrpe3.0之前版本需要执行  make install-xinetd
# nrpe3.0以上版本执行此命令时会报上面这个错误提示没有这个参数。

[root@nagios-client nrpe-3.2.0]# ll Makefile
-rw-r--r--. 1 root root 6953 5月   9 15:10 Makefile
[root@nagios-client nrpe-3.2.0]# grep install-inetd Makefile
        echo "     install-inetd        install the startup files for inetd, launchd, etc."; install-inetd:
[root@nagios-client nrpe-3.2.0]# grep install-init Makefile
        echo "     install-init         install the startup files for init, systemd, etc."; install-init:

# 现在应该执行以下命令:
[root@nagios-client nrpe-3.2.0]# make install-inetd  && echo $?
[root@nagios-client nrpe-3.2.0]# make install-init 
[root@nagios-client nrpe-3.2.0]# echo $?

2.4 对服务端主机监控进行授权

将 /usr/local/nagios/etc/nrpe.cfg第106行
#allowed_hosts=127.0.0.1,::1
修改为:
allowed_hosts=127.0.0.1,192.168.2.236  #添加nagios服务端的ip地址

[root@nagios-client nrpe-3.2.0]# sed -i  "106s/::1/192.168.2.236/g"  /usr/local/nagios/etc/nrpe.cfg
[root@nagios-client ~]# grep allowed_hosts /usr/local/nagios/etc/nrpe.cfg 
allowed_hosts=127.0.0.1,192.168.2.236

2.5 设置nrpe服务开机自启动

[root@nagios-client ~]# systemctl enable nrpe.service  && echo $?

2.6 启动重启停止服务

启动重启服务

[root@nagios-client nrpe-3.2.0]#  /bin/systemctl start  nrpe.service && echo $?  
[root@nagios-client nrpe-3.2.0]#  /bin/systemctl restart  nrpe.service && echo $?  
[root@nagios-client nrpe-3.2.0]#  /bin/systemctl stop nrpe.service && echo $?  

验证服务是否启动成功

[root@nagios-client nrpe-3.2.0]#  /bin/systemctl start  nrpe.service && echo $?  
[root@nagios-client nrpe-3.2.0]# ps -ef |grep nrpe
nagios    68784      1  0 15:32 ?        00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -f
root      68790    308  0 15:33 pts/0    00:00:00 grep --color=auto nrpe

[root@nagios-client nrpe-3.2.0]# lsof -i:5666
COMMAND   PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
nrpe    68784 nagios    4u  IPv4 2908070      0t0  TCP *:nrpe (LISTEN)
nrpe    68784 nagios    5u  IPv6 2908071      0t0  TCP *:nrpe (LISTEN)

[root@nagios-client nrpe-3.2.0]#  /bin/systemctl status  nrpe.service && echo $?  
● nrpe.service - Nagios Remote Plugin Executor
   Loaded: loaded (/usr/lib/systemd/system/nrpe.service; disabled; vendor preset: disabled)
   Active: active (running) since 六 2020-05-09 15:32:18 CST; 2s ago
     Docs: http://www.nagios.org/documentation
 Main PID: 68784 (nrpe)
   CGroup: /system.slice/nrpe.service
           └─68784 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -f

5月 09 15:32:18 nagios-client systemd[1]: Started Nagios Remote Plugin Executor.
5月 09 15:32:18 nagios-client nrpe[68784]: Starting up daemon
5月 09 15:32:18 nagios-client nrpe[68784]: Server listening on 0.0.0.0 port 5666.
5月 09 15:32:18 nagios-client nrpe[68784]: Server listening on :: port 5666.
5月 09 15:32:18 nagios-client nrpe[68784]: Listening for connections on port 5666
5月 09 15:32:18 nagios-client nrpe[68784]: Allowing connections from: 127.0.0.1,::1
0

3. 配置监控项以及告警指标

3.1 先在本地被监控主机上测试一下监控命令

监控主机上的登录用户

[root@nagios-client ~]# /usr/local/nagios/libexec/check_users -w 5 -c 10
USERS OK - 2 users currently logged in |users=2;5;10;0

监控主机负载

[root@nagios-client ~]# /usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
OK - load average: 0.00, 0.01, 0.05|load1=0.000;15.000;30.000;0; load5=0.010;10.000;25.000;0; load15=0.050;5.000;20.000;0; 

监控主机磁盘使用情况

[root@nagios-client ~]# /usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
DISK OK - free space: / 45597 MiB (89.10% inode=100%);| /=5577MiB;40940;46057;0;51175

监控主机上运行的进程

[root@nagios-client ~]# /usr/local/nagios/libexec/check_procs -w 250 -c 400
PROCS WARNING: 373 processes | procs=373;250;400;0;

监控主机交换分区使用情况

[root@nagios-client ~]# /usr/local/nagios/libexec/check_swap -w 20% -c 10%
SWAP OK - 100% free (7551 MB out of 7551 MB) |swap=7551MB;1510;755;0;7551

其他监控命令使用教程请参考手册:http://nagios-plugins.org/doc/man/

3.2 将测试无误监控命令写入配置文件


[root@nagios-client ~]# cd /usr/local/nagios/etc/
[root@nagios-client etc]# ll
总用量 16
-rw-r--r--. 1 nagios nagios 13121 5月   9 15:49 nrpe.cfg
[root@nagios-client etc]#  vi /usr/local/nagios/etc/nrpe.cfg +333
[root@nagios-client etc]# cat /usr/local/nagios/etc/nrpe.cfg |grep -v "^#"|grep -v "^$"
log_facility=daemon
debug=0
pid_file=/usr/local/nagios/var/nrpe.pid
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=127.0.0.1,192.168.2.236
dont_blame_nrpe=0
allow_bash_command_substitution=0
command_timeout=60
connection_timeout=300
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[check_root_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

#再次重启nrpe服务
[root@nagios-client etc]#  /bin/systemctl restart  nrpe.service && echo $?  

3.3 针对nrpe服务监听端口进行配置

[root@nagios-client etc]#  systemctl start firewalld.service 
[root@nagios-client etc]#  firewall-cmd --zone=public --permanent --add-port=5666/tcp && firewall-cmd --reload     &&    firewall-cmd --list-ports    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章