当前位置 : 主页 > 操作系统 > centos >

实践案例:Zabbix通过Proxy被动模式代理跨网段监控Linux主机及应用

来源:互联网 收集:自由互联 发布时间:2022-06-20
在上篇博文《实战案例: Zabbix通过Proxy代理主动模式跨网段监控Linux主机及应用》(https://blog.51cto.com/shone/5338962)中,已经配置完成了架构图中的主动模式的监控部分,本文继续完成被

在上篇博文《实战案例: Zabbix通过Proxy代理主动模式跨网段监控Linux主机及应用》(https://blog.51cto.com/shone/5338962)中,已经配置完成了架构图中的主动模式的监控部分,本文继续完成被动模式的监控的实践,这样结合前面几篇针对本地的应用的监控,就完成了Zabbix6.0.4在多种应用场景下的多种应用的监控演示,全部文章力求思路清晰、简单易懂,让初学者也能轻松阅读和照例实验。


1. 实验架构图

实践案例:Zabbix通过Proxy被动模式代理跨网段监控Linux主机及应用_java

Zabbix主动代理主机名定义为:proxy_active Zabbix代理主机IP地址:172.16.0.118
Zabbix被动代理主机名定义为:proxy_passive Zabbix代理主机IP地址:172.16.0.128

2. Zabbix-Agent 被动代理模式Linux主机的安装及配置

[root@CentOS84-IP172-28 ]#hostname -I
172.16.0.28

[root@CentOS84-IP172-28 ]#cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.0.128 proxy_passive
[root@CentOS84-IP172-28 ]#

[root@CentOS84-IP172-28 ]#rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm
[root@CentOS84-IP172-28 ]#dnf clean all
[root@CentOS84-IP172-28 ]#yum -y install zabbix-agent
[root@CentOS84-IP172-28 ]#systemctl enable --now zabbix-agent

## 修改agent配置 其中:ServerActive=127.0.0.1 最好注释掉,这样在日志看不到报错
[root@CentOS84-IP172-28 ]#vim "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
[root@CentOS84-IP172-28 ]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.250.18,172.16.0.128
Hostname=172.16.0.28
Timeout=30
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@CentOS84-IP172-28 ]#

[root@CentOS84-IP172-28 ]#systemctl restart zabbix-agent

#### 下面这个日志是在Proxy和Server全部配置好后查看到的正常状态信息
[root@CentOS84-IP172-28 ]#tail /var/log/zabbix/zabbix_agentd.log
679384:20220528:154639.484 ** Enabled features 679384:20220528:154639.484 IPv6 support: YES
679384:20220528:154639.484 TLS support: YES
679384:20220528:154639.484 ************************
679384:20220528:154639.484 using configuration file: /etc/zabbix/zabbix_agentd.conf
679384:20220528:154639.484 agent #0 started [main process]
679385:20220528:154639.485 agent #1 started [collector]
679386:20220528:154639.485 agent #2 started [listener #1]
679388:20220528:154639.486 agent #4 started [listener #3]
679387:20220528:154639.487 agent #3 started [listener #2]

3. 被动模式 Zabbix-Proxy 的安装及配置

#### DNS通过hosts定义来完成
[root@CentOS84-IP172-128 ]#hostname -I
172.16.0.128
[root@CentOS84-IP172-128 ]#cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

172.16.0.128 proxy_passive

[root@CentOS84-IP172-128 ]#


############################################################################
#### 特别提示:Zabbix对MariaDB数据库版本有具体要求,在后来的日志提示看到是要求10.5,不符合要求服务启动不了,在日志内有相关的信息。
#### Zabbix-Proxy 数据库安装和准备
[root@CentOS84-IP172-128 ]#cat /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.7 CentOS repository list - created 2022-05-08 05:20 UTC
# https://mariadb.org/download/
[mariadb]
name = MariaDB
baseurl = https://tw1.mirror.blendbyte.net/mariadb/yum/10.7/centos8-amd64
module_hotfixes=1
gpgkey=https://tw1.mirror.blendbyte.net/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
[root@CentOS84-IP172-128 ]#

[root@CentOS84-IP172-128 ]#dnf install -y MariaDB-server
[root@CentOS84-IP172-128 ]#systemctl enable --now mariadb

[root@CentOS84-IP172-128 ]#ss -tln
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 80 [::]:3306 [::]:*

#### 配置数据库
[root@CentOS84-IP172-128 ]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.7.4-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
## 创建数据库并定义好字符集
MariaDB [(none)]> create database zabbix_proxy_passive character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.001 sec)
## 授权
MariaDB [(none)]> grant all privileges on zabbix_proxy_passive.* to proxy@'172.16.0.%' identified by '123456';
Query OK, 0 rows affected (0.017 sec)

MariaDB [(none)]> grant all privileges on zabbix_proxy_passive.* to proxy@'localhost' identified by '123456';
Query OK, 0 rows affected (0.001 sec)
## 查看用户
MariaDB [(none)]> select user,host from mysql.user;
+-------------+--------------------+
| User | Host |
+-------------+--------------------+
| proxy | 172.16.0.% |
| | centos84-ip172-128 |
| | localhost |
| mariadb.sys | localhost |
| mysql | localhost |
| proxy | localhost |
| root | localhost |
+-------------+--------------------+
7 rows in set (0.002 sec)
## 删除空账户
MariaDB [(none)]> drop user ''@'centos84-ip172-128';
Query OK, 0 rows affected (0.003 sec)

MariaDB [(none)]> drop user ''@'localhost';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> select user,host from mysql.user;
+-------------+------------+
| User | Host |
+-------------+------------+
| proxy | 172.16.0.% |
| mariadb.sys | localhost |
| mysql | localhost |
| proxy | localhost |
| root | localhost |
+-------------+------------+
5 rows in set (0.002 sec)

MariaDB [(none)]>

############################################################################
#### 安装zabbix-proxy及依赖包、工具包等
[root@CentOS84-IP172-128 ]#rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm
Retrieving https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm
warning: /var/tmp/rpm-tmp.B25PUs: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:zabbix-release-6.0-1.el8 ################################# [100%]
[root@CentOS84-IP172-128 ]#dnf clean all
82 files removed
[root@CentOS84-IP172-128 ]#yum -y install zabbix-proxy zabbix-proxy-mysql zabbix-sql-scripts zabbix-get

## 安装后zabbix_proxy的默认配置文件,需要修改
[root@CentOS84-IP172-128 ]#grep "^[a-Z]" /etc/zabbix/zabbix_proxy.conf
Server=127.0.0.1
Hostname=Zabbix proxy
LogFile=/var/log/zabbix/zabbix_proxy.log
LogFileSize=0
PidFile=/run/zabbix/zabbix_proxy.pid
SocketDir=/run/zabbix
DBName=zabbix_proxy
DBUser=zabbix
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1
[root@CentOS84-IP172-128 ]#

## 修改zabbix_proxy的配置文件
[root@CentOS84-IP172-128 ]#vim /etc/zabbix/zabbix_proxy.conf
[root@CentOS84-IP172-128 ]#grep "^[a-Z]" /etc/zabbix/zabbix_proxy.conf
ProxyMode=1 #被动模式为1,主动为0
Server=192.168.250.18 #zabbix server服务器的地址或主机名
Hostname=proxy_passive #代理服务器名,需与zabbix-server添加代理时的proxy name完全一致!!
ListenPort=10051 #zabbix proxy监听端口
LogFile=/var/log/zabbix/zabbix_proxy.log
LogFileSize=0
EnableRemoteCommands=1 #允许zabbix server执行远程命令,自愈配置需要用到
PidFile=/run/zabbix/zabbix_proxy.pid
SocketDir=/run/zabbix
DBHost=172.16.0.128 #数据库服务器地址,本机也可以注释掉此行
DBName=zabbix_proxy_passive #被动模式数据库名称
DBUser=proxy #数据库账号,需要和前面数据库定义时候一致
DBPassword=123456 #数据库密码,需要和前面数据库定义时候一致
DBPort=3306 #数据库端口,需要和前面数据库定义时候一致
ProxyLocalBuffer=720 #已提交到zabbix server的数据保留时间
ProxyOfflineBuffer=720 #未提交到zabbix server的时间保留时间
HeartbeatFrequency=60 #心跳间隔检测时间,默认60秒,范围0-3600秒,被动模式不使用
ConfigFrequency=30 #间隔多少秒从zabbix server获取监控项信息
DataSenderFrequency=5 #数据发送时间间隔,默认为1秒,范围为1-3600秒,被动模式不使用
StartPollers=15 #启动的数据采集器数量
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
JavaGateway=192.168.250.118 #java gateway服务器地址,当需监控java时须配置否则无不到数据
JavaGatewayPort=10052 #Javagatewa服务端口
StartJavaPollers=20 #启动多少个线程采集数据
CacheSize=1024M #保存监控项而占用的最大内存
HistoryCacheSize=4096M #保存监控历史数据占用的最大内存
HistoryIndexCacheSize=512M #历史索引缓存的大小
Timeout=10 #监控项超时时间,单位为秒
LogSlowQueries=3000 #毫秒,数据库查询记录到日志的时长
StatsAllowedIP=127.0.0.1
[root@CentOS84-IP172-128 ]#

## 导入数据库
[root@CentOS84-IP172-128 ]#ll /usr/share/doc/zabbix-sql-scripts/mysql/proxy.sql
-rw-r--r-- 1 root root 163854 May 3 15:31 /usr/share/doc/zabbix-sql-scripts/mysql/proxy.sql
[root@CentOS84-IP172-128 ]#cat /usr/share/doc/zabbix-sql-scripts/mysql/proxy.sql | mysql -uproxy -p123456 -h172.16.0.128 zabbix_proxy_passive
[root@CentOS84-IP172-128 ]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.7.4-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+----------------------+
| Database |
+----------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| zabbix_proxy_passive |
+----------------------+
6 rows in set (0.001 sec)

MariaDB [(none)]> quit
Bye

## 启动并开机自启zabbix-proxy
[root@CentOS84-IP172-128 ]#systemctl enable --now zabbix-proxy
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-proxy.service → /usr/lib/systemd/system/zabbix-proxy.service.

## 查看日志
[root@CentOS84-IP172-128 ]#tail /var/log/zabbix/zabbix_proxy.log
655225:20220528:151444.920 proxy #33 started [poller #14]
655224:20220528:151444.921 proxy #32 started [poller #13]
655226:20220528:151444.921 proxy #34 started [poller #15]
655228:20220528:151444.923 proxy #36 started [icmp pinger #1]
655227:20220528:151444.924 proxy #35 started [unreachable poller #1]
655229:20220528:151444.924 proxy #37 started [history poller #1]
655230:20220528:151444.925 proxy #38 started [availability manager #1]
655231:20220528:151444.926 proxy #39 started [odbc poller #1]
655193:20220528:151444.994 proxy #9 started [preprocessing worker #2]
655194:20220528:151444.994 proxy #10 started [preprocessing worker #3]
[root@CentOS84-IP172-128 ]#

4. zabbix-server配置添加被动模式Proxy必须项

#### 修改zabbix_serve的配置,有注释的三行是新增加的配置,不配置看不到数据,我们修改的参数仅仅适合实验环境,实际生产环境略有不同,需要按照需要进行修改。
[root@CentOS84-IP18 ]#vim /etc/zabbix/zabbix_server.conf
[root@CentOS84-IP18 ]#grep "^[a-Z]" /etc/zabbix/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/run/zabbix/zabbix_server.pid
SocketDir=/run/zabbix
DBHost=192.168.250.38
DBName=zabbix
DBUser=zabbix
DBPassword=shone123456
DBPort=3306
JavaGateway=192.168.250.118
JavaGatewayPort=10052
StartJavaPollers=20
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
LogSlowQueries=3000
StartProxyPollers=20 #启用多少子进程与代理端通信,若代理较多可考虑加大此数值,范围是0-250
ProxyConfigFrequency=60 #proxy被动模式下,server多少秒同步配置文件至proxy,该参数仅用于被动模式下的代理,范围是1-3600*24*7
ProxyDataFrequency=60 #被动模式下,zabbix server间隔多少秒向proxy请求历史数据
StatsAllowedIP=127.0.0.1
[root@CentOS84-IP18 ]#
[root@CentOS84-IP18 ]#systemctl restart zabbix-server.service

#### 下面的日志是在全系统配置完成后查询的正常状态下看到的日志
[root@CentOS84-IP18 ]#tail -n50 /var/log/zabbix/zabbix_server.log
..............................
633104:20220528:155436.863 server #40 started [java poller #18]
633105:20220528:155436.864 server #41 started [java poller #19]
633106:20220528:155436.864 server #42 started [java poller #20]
633107:20220528:155436.865 server #43 started [proxy poller #1]
633108:20220528:155436.866 server #44 started [proxy poller #2]
633085:20220528:155436.867 server #21 started [history syncer #4]
633103:20220528:155436.868 server #39 started [java poller #17]
633110:20220528:155436.871 server #46 started [proxy poller #4]
633111:20220528:155436.872 server #47 started [proxy poller #5]
633112:20220528:155436.873 server #48 started [proxy poller #6]
633113:20220528:155436.874 server #49 started [proxy poller #7]
633115:20220528:155436.876 server #51 started [proxy poller #9]
633114:20220528:155436.877 server #50 started [proxy poller #8]
633116:20220528:155436.877 server #52 started [proxy poller #10]
633117:20220528:155436.879 server #53 started [proxy poller #11]
633118:20220528:155436.880 server #54 started [proxy poller #12]
633119:20220528:155436.881 server #55 started [proxy poller #13]
633109:20220528:155436.881 server #45 started [proxy poller #3]
633120:20220528:155436.884 server #56 started [proxy poller #14]
633121:20220528:155436.884 server #57 started [proxy poller #15]
633122:20220528:155436.885 server #58 started [proxy poller #16]
633123:20220528:155436.886 server #59 started [proxy poller #17]
633124:20220528:155436.887 server #60 started [proxy poller #18]
633125:20220528:155436.888 server #61 started [proxy poller #19]
633126:20220528:155436.889 server #62 started [proxy poller #20]
633128:20220528:155436.891 server #64 started [task manager #1]
633127:20220528:155436.892 server #63 started [self-monitoring #1]
633129:20220528:155436.892 server #65 started [poller #1]
633131:20220528:155436.895 server #67 started [poller #3]
633130:20220528:155436.895 server #66 started [poller #2]
633132:20220528:155436.896 server #68 started [poller #4]
633133:20220528:155436.898 server #69 started [poller #5]
633135:20220528:155436.899 server #71 started [trapper #1]
633134:20220528:155436.900 server #70 started [unreachable poller #1]
633136:20220528:155436.900 server #72 started [trapper #2]
633137:20220528:155436.902 server #73 started [trapper #3]
633138:20220528:155436.903 server #74 started [trapper #4]
633139:20220528:155436.905 server #75 started [trapper #5]
633140:20220528:155436.905 server #76 started [icmp pinger #1]
633141:20220528:155436.906 server #77 started [alert syncer #1]
633142:20220528:155436.907 server #78 started [history poller #1]
633143:20220528:155436.908 server #79 started [history poller #2]
633144:20220528:155436.910 server #80 started [history poller #3]
633146:20220528:155436.911 server #82 started [history poller #5]
633145:20220528:155436.912 server #81 started [history poller #4]
633148:20220528:155436.913 server #84 started [trigger housekeeper #1]
633149:20220528:155436.914 server #85 started [odbc poller #1]
633147:20220528:155436.915 server #83 started [availability manager #1]
633107:20220528:155443.972 sending configuration data to proxy "proxy_passive" at "172.16.0.128", datalen 13622, bytes 3066 with compression ratio 4.4
633135:20220528:155451.781 sending configuration data to proxy "proxy_active" at "172.16.0.118", datalen 18962, bytes 3808 with compression ratio 5.0
[root@CentOS84-IP18 ]#

5. zabbix-server界面下配置zabbix-agent使用被动代理

zabbix 添加被动代理: 管理-->agent代理程序-->创建代理:

实践案例:Zabbix通过Proxy被动模式代理跨网段监控Linux主机及应用_Zabbix通过Proxy_02


实践案例:Zabbix通过Proxy被动模式代理跨网段监控Linux主机及应用_java_03


6. Zabbix-Server界面下添加被动模式主机并关联模板

配置 --> 主机 --> 创建主机

添加被动模式代理主机及关联被动模式的模板

实践案例:Zabbix通过Proxy被动模式代理跨网段监控Linux主机及应用_mysql_04


7. Zabbix-Server web界面下验证主机状态

实践案例:Zabbix通过Proxy被动模式代理跨网段监控Linux主机及应用_Zabbix通过Proxy_05


8. Zabbix-Server web界面下验证监控数据及图形

实践案例:Zabbix通过Proxy被动模式代理跨网段监控Linux主机及应用_Zabbix6.0.4_06


实践案例:Zabbix通过Proxy被动模式代理跨网段监控Linux主机及应用_mysql_07



实践案例:Zabbix通过Proxy被动模式代理跨网段监控Linux主机及应用_java_08


网友评论