Zabbix proxy

详细介绍了zabbix-proxy的原理以及配置

4、Zabbix proxy

官方手册

https://www.zabbix.com/documentation/4.0/zh/manual/distributed_monitoring/proxies

zabbix 作为一个分布式监控系统(分布式监控解决方案),支持通过代理(proxy)收集zabbix agent的监控数据然后由zabbix proxy再把数据发送给zabbix server,也就是zabbix proxy 可以代替 zabbix server 收集监控数据,然后把数据汇报给 zabbix server,所以zabbix proxy可以在一定程度上分担了zabbix server 的数据收集压力,从而降低了数据的采集时间、也相应的增加了zabbix server的监控能力。

另外zabbix proxy也区分主动模式和被动模式,通信方式与zabbix server主动模式和被动模式一样,区别是zabbix proxy由于没有zabbix agent的配置,所以zabbix proxy在主动模式下要向zabbix server周期性的向zabbix server申请获取zabbix agent的监控项信息,但是zabbix proxy在被动模式下也是等待zabbix server的连接并接受zabbix server发送的监控项指令,然后再由zabbix proxy向zabbix agent发起请求获取数据。

4.1zabbix proxy架构

img

4.2zabbix proxy对比zabbix server

4.3zabbix proxy部署与使用

4.3.1zabbix proxy版本选择

zabbix proxy的大版本必须要和zabbix server版本一致,否则会导致出现zabbix server与zabbix proxy不兼容问
题。小版本最好也保持一致。

4.3.2zabbix proxy安装

与zabbix server一样可以二进制安装也可以通过yum/apt安装

4.3.2.1apt/yum安装zabbix proxy

[root@zabbix-proxy-active ~]#rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/8/x86_64/zabbix-release-4.0-2.el8.noarch.rpm

[root@zabbix-proxy-active ~]#dnf clean all

[root@zabbix-proxy-slave ~]#yum install -y zabbix-proxy-mysql.x86_64

4.3.2.2编译安装zabbix proxy

# yum install gcc libxml2-devel  net-snmp net-snmp-devel  curl curl-devel php php-bcmath php-mbstring mariadb mariadb-devel java-1.8.0-openjdk-devel -y

# apt install libmysqld-dev libmysqlclient-dev libxml2-dev libxml2 snmp libsnmp-dev libevent-dev curl libcurl4-openssl-dev
 
# useradd zabbix -s /usr/sbin/nologin

# pwd
/usr/local/src/zabbix-4.0.15

# ./configure --prefix=/apps/zabbix_proxy --enable-proxy --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2
# make 
# make instal

4.3.3配置被动zabbix proxy

配置并使用被动模式(Passive Proxy Mode)的zabbix proxy收集zabbix agent监控数据。

4.3.3.1配置数据库

[root@zabbix-mysql-master ~]# mysql -uroot -psunxiang

#创建数据库
mysql> create database zabbix_proxy_slave character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

#创建用户
mysql> create user 'zabbix_proxy_slave'@'10.0.0.%' identified by 'sunxiang';
Query OK, 0 rows affected (0.00 sec)

#授权
mysql> grant all on zabbix_proxy_slave.* to zabbix_proxy_slave@'10.0.0.%';
Query OK, 0 rows affected (0.00 sec)

4.3.3.2连接测试

[root@zabbix-proxy-slave ~]#mysql -u'zabbix_proxy_slave' -p'sunxiang' -h10.0.0.10 zabbix_proxy_slave
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4911
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show tables;
Empty set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| zabbix_proxy_slave |
+--------------------+
2 rows in set (0.00 sec)

4.3.3.3导入数据库

[root@zabbix-proxy-slave ~]#zcat /usr/share/doc/zabbix-proxy-mysql/schema.sql.gz | mysql -u'zabbix_proxy_slave' -p'sunxiang' -h10.0.0.10 zabbix_proxy_slave
[root@zabbix-proxy-slave ~]#mysql -u'zabbix_proxy_slave' -p'sunxiang' -h10.0.0.10 zabbix_proxy_slave

mysql> show tables;
+------------------------------+
| Tables_in_zabbix_proxy_slave |
+------------------------------+
| acknowledges                 |
| actions                      |
| alerts                       |
| application_discovery        |
| application_prototype        |
| application_template         |
| applications                 |
| auditlog                     |
| auditlog_details             |
| autoreg_host                 |
| conditions                   |
| config                       |
| corr_condition               |

4.3.3.4修改配置文件

[root@zabbix-proxy-slave ~]#vim /etc/zabbix/zabbix_proxy.conf 
[root@zabbix-proxy-slave ~]#cat /etc/zabbix/zabbix_proxy.conf | grep -Ev '^$|^#'
ProxyMode=1             #0为主动,1为被动
Server=10.0.0.11        #zabbix server服务器的地址或主机名
Hostname=proxy-slave    #代理服务器名称,需要与zabbix server添加代理时候的proxyname是一致的!
ListenPort=10051        #zabbix proxy监听端口
LogFile=/var/log/zabbix/zabbix_proxy.log
EnableRemoteCommands=1  #允许zabbix server执行远程命令
DBHost=10.0.0.10        #数据库服务器地址
DBName=zabbix_proxy_slave #使用的数据库名称
DBUser=zabbix_proxy_slave #连接数据库的用户名称
DBPassword=sunxaing     #数据库用户密码
DBPort=3306             #数据库端口
ProxyLocalBuffer=720    #已经提交到zabbix server的数据保留时间
ProxyOfflineBuffer=720  #未提交到zabbix server的时间保留时间
HeartbeatFrequency=60   #心跳间隔检测时间,默认60秒,范围0-3600秒,被动模式不使用
ConfigFrequency=5       #间隔多少秒从zabbix server获取监控项信息
DataSenderFrequency=5   #数据发送时间间隔,默认为1秒,范围为1-3600秒,被动模式不使用
StartPollers=20         #启动的数据采集器数量
JavaGateway=10.0.0.30   #java gateway服务器地址,当需要监控java的时候必须配置否则监控不到数据
JavaGatewayPort=10052   #Javagatewa服务端口
StartJavaPollers=20     #启动多少个线程采集数据
CacheSize=2G            #保存监控项而占用的最大内存
HistoryCacheSize=2G     #保存监控历史数据占用的最大内存
HistoryIndexCacheSize=128M  #历史索引缓存的大小
Timeout=30              #监控项超时时间,单位为秒
LogSlowQueries=3000     #毫秒,多久的数据库查询会被记录到日志

4.3.3.5重启zabbix proxy服务

[root@zabbix-proxy-slave ~]#systemctl enable --now zabbix-proxy.service 
[root@zabbix-proxy-slave ~]#tail /var/log/zabbix/zabbix_proxy.log 
 36230:20211217:205753.686 proxy #45 started [poller #11]
 36235:20211217:205753.708 proxy #50 started [poller #16]
 36233:20211217:205753.709 proxy #48 started [poller #14]
 36234:20211217:205753.716 proxy #49 started [poller #15]
 36236:20211217:205753.744 proxy #51 started [poller #17]
 36237:20211217:205753.765 proxy #52 started [poller #18]
 36239:20211217:205753.773 proxy #54 started [poller #20]
 36238:20211217:205753.774 proxy #53 started [poller #19]
 36240:20211217:205753.789 proxy #55 started [unreachable poller #1]
 36241:20211217:205753.792 proxy #56 started [icmp pinger #1]


[root@zabbix-proxy-slave ~]#ss -ntl | grep 10051
LISTEN 0      128          0.0.0.0:10051      0.0.0.0:*          
LISTEN 0      128             [::]:10051         [::]:*   

4.3.3.6zabbix添加被动代理


4.3.3.7配置zabbix agent使用被动代理

4.3.3.7关联被动模式模版

注意:不能重复使用监控项,之前使用的主动模版现在又用被动模版,需要先删除之前的主动模版

4.3.3.8zabbix server配置

[root@zabbix-server ~]#vim /apps/zabbix_server/etc/zabbix_server.conf
### Option: StartProxyPollers
#   Number of pre-forked instances of pollers for passive proxies.
#
# Mandatory: no
# Range: 0-250
# Default:
StartProxyPollers=20    #启用多少子进程与代理端通信,若代理较多可考虑加大此数值,范围是0-250

### Option: ProxyConfigFrequency
#   How often Zabbix Server sends configuration data to a Zabbix Proxy in seconds.
#   This parameter is used only for proxies in the passive mode.
#
# Mandatory: no
# Range: 1-3600*24*7
# Default:
ProxyConfigFrequency=60 #proxy被动模式下,server多少秒同步配置文件至proxy,该参数仅用于被动模式下的代理,范围是1-3600*24*7

### Option: ProxyDataFrequency
#   How often Zabbix Server requests history data from a Zabbix Proxy in seconds.
#   This parameter is used only for proxies in the passive mode.
#
# Mandatory: no
# Range: 1-3600
# Default:
ProxyDataFrequency=60   #被动模式下,zabbix server间隔多少秒向proxy请求历史数据


[root@zabbix-server ~]#systemctl restart zabbix-server.service 

4.3.3.9zabbix agent配置

[root@zabbix-web-server2 ~]# vim /etc/zabbix/zabbix_agentd.conf 
[root@zabbix-web-server2 ~]# grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.0.11,10.0.0.31              #添加proxy地址
ListenPort=10050
StartAgents=3
ServerActive=10.0.0.11
Hostname=10.0.0.40
Timeout=30                              #监控项超时时间,单位为秒
Include=/etc/zabbix/zabbix_agentd.d/*.conf

[root@zabbix-web-server2 ~]# systemctl restart zabbix-agent.service

4.3.3.10验证当前主机状态

监控了一晚上,早上才变绿,之前都是没有绿但是有数据

4.3.3.11验证主机监控数据及图形

4.3.4配置主动模式zabbix proxy

4.3.4.1配置数据库

[root@zabbix-mysql-master ~]# mysql -uroot -psunxiang

#创建数据库
mysql> create database zabbix_proxy_active character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

#创建用户
mysql> create user 'zabbix_proxy_active'@'10.0.0.%' identified by 'sunxiang';
Query OK, 0 rows affected (0.03 sec)

#授权
mysql> grant all on zabbix_proxy_active.* to zabbix_proxy_active@'10.0.0.%';
Query OK, 0 rows affected (0.00 sec)


4.3.4.2连接测试

[root@zabbix-proxy-active ~]#mysql -u'zabbix_proxy_active' -p'sunxiang' -h10.0.0.10 zabbix_proxy_active
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4821
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show tables;
Empty set (0.00 sec)

mysql> show databases;
+---------------------+
| Database            |
+---------------------+
| information_schema  |
| zabbix_proxy_active |
+---------------------+
2 rows in set (0.01 sec)


4.3.4.3导入数据库

[root@zabbix-proxy-active ~]#zcat /usr/share/doc/zabbix-proxy-mysql/schema.sql.gz | mysql -u'zabbix_proxy_active' -p'sunxiang' -h10.0.0.10 zabbix_proxy_active

[root@zabbix-proxy-active ~]#mysql -u'zabbix_proxy_active' -p'sunxiang' -h10.0.0.10 zabbix_proxy_active
mysql> show tables;
+-------------------------------+
| Tables_in_zabbix_proxy_active |
+-------------------------------+
| acknowledges                  |
| actions                       |
| alerts                        |
| application_discovery         |
| application_prototype         |
| application_template          |


4.3.4.4修改配置文件

[root@zabbix-proxy-active ~]#vim /etc/zabbix/zabbix_proxy.conf 
[root@zabbix-proxy-active ~]#grep -E '^[A-Z]' /etc/zabbix/zabbix_proxy.conf 
ProxyMode=0             #0为主动,1为被动
Server=10.0.0.11        #zabbix server服务器的地址或主机名
Hostname=proxy_active   #代理服务器名称,需要与zabbix server添加代理时候的proxyname是一致的!
ListenPort=10051        #zabbix proxy监听端口
LogFile=/var/run/zabbix/zabbix_proxy.pid
EnableRemoteCommands=1  #允许zabbix server执行远程命令
DBHost=10.0.0.10        #数据库服务器地址
DBName=zabbix_proxy_active #使用的数据库名称
DBUser=zabbix_proxy_active #连接数据库的用户名称
DBPassword=sunxiang     #数据库用户密码
DBPort=3306             #数据库端口
ProxyLocalBuffer=720    #已经提交到zabbix server的数据保留时间
ProxyOfflineBuffer=720  #未提交到zabbix server的时间保留时间
HeartbeatFrequency=60   #心跳间隔检测时间,默认60秒,范围0-3600秒,被动模式不使用
ConfigFrequency=5       #间隔多少秒从zabbix server获取监控项信息
DataSenderFrequency=5   #数据发送时间间隔,默认为1秒,范围为1-3600秒,被动模式不使用
StartPollers=20         #启动的数据采集器数量
JavaGateway=10.0.0.30   #java gateway服务器地址,当需要监控java的时候必须配置否则监控不到数据
JavaGatewayPort=10052   #Javagatewa服务端口
StartJavaPollers=20     #启动多少个线程采集数据
CacheSize=2G            #保存监控项而占用的最大内存
HistoryCacheSize=2G     #保存监控历史数据占用的最大内存
HistoryIndexCacheSize=128M  #历史索引缓存的大小
Timeout=30              #监控项超时时间,单位为秒
LogSlowQueries=3000     #毫秒,多久的数据库查询会被记录到日志

4.3.4.5重启zabbix proxy服务

[root@zabbix-proxy-active ~]#systemctl enable --now zabbix-proxy.service

4.3.4.6zabbix web添加主动代理

4.3.4.7zabbix agent使用主动代理

4.3.4.8zabbix agent配置文件

[root@zabbix-web-server1 ~]# grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.0.11,10.0.0.21      #zabbix server与zabbix proxy代理的地址
ListenPort=10050
StartAgents=3
ServerActive=10.0.0.21          #zabbix proxy的地址
Hostname=10.0.0.30
Include=/etc/zabbix/zabbix_agentd.d/*.conf

[root@zabbix-web-server1 ~]# systemctl restart zabbix-agent.service

4.3.4.9zabbix web验证当前主机状态

4.3.4.10验证主机监控数据及图像

4.4交互过程

# vim /var/log/zabbix/zabbix_proxy.log
zabbix agent向zabbix proxy申请监控项:
 2219:20211220:185229.406 sending [{"request":"active checks","host":"10.0.0.30"}]
 2219:20211220:185229.406 before read
 2219:20211220:185229.416 got [{"response":"failed","info":"host [10.0.0.30] not found"}]
 2219:20211220:185229.416 In parse_list_of_checks(

#zabbix proxy向zabbix server发起请求获取主机监控项
 4697:20211220:185229.426 In is_ip4() ip:'10.0.0.30'
 4697:20211220:185229.426 In get_hostid_by_host() host:'10.0.0.30' metadata:''
 4697:20211220:185229.426 query [txnlev:0] [select h.hostid,h.status,h.tls_accept,h.tls_issuer,h.tls_subject,h.tls_psk_identity,a.host_met adata from hosts h left join autoreg_host a on a.proxy_hostid is null and a.host=h.host where h.host='10.0.0.30' and h.status in (0,1) and h.flags<>2 and h.proxy_hostid is null]
 4697:20211220:185229.431 query [txnlev:1] [insert into proxy_autoreg_host (clock,host,listen_ip,listen_dns,listen_port,host_metadata) values (1576407149,'10.0.0.30','10.0.0.30','',10050,'')]


#收到zabbix server返回的监控项 
 361:20211220:191141.647 Received [{"globalmacro":{"fields":["globalmacroid","macro","value"],"data":[[2,"{$SNMP_COMMUNITY}","public"]]},"hosts":{"fields":["hostid","host","status","available","ipmi_authtype","ipmi_privilege","ipmi_username","ipmi_password","ipmi_available","snmp_available","jmx_available","name","tls_connect","tls_accept","tls_issuer","tls_subject","tls_psk_identity","tls_psk"],"data":[[10050,"Template App Zabbix Agent",3,0,-1,2,"","",0,0,0,"Template App Zabbix Agent",1,1,"","","",""],[10270,"Template OS Linux-active",3,0,-1,2,"","",0,0,0,"Template OS Linux-active",1,1,"","","",""],[10276,"107-web2",0,0,-1,2,"","",0,0,0,"172.31.0.107-web2",1,1,"","","",""]]},"interface":{"fields":["interfaceid","hostid","main","type","useip","ip","dns","port","bulk"],"data":[[7,10276,1,1,1,"172.31.0.107","","10050",1]]},"hosts_templates":{"fields":["hosttemplateid","hostid","templateid"],"data":[[251,10270,10050],[255,10276,10270]]},"hostmacro":{"fields":["hostmacroid","hostid","macro","value"],"data":[]}


#收到zabbix agent返回的监控数据
1425:20211220:191315.963 trapper got '{"request":"agent data","session":"a62af26d487fd475d3f94d36c58880e5","data":[{"host":"10.0.0.30","key":"proc.num[]","value":"180","id":42,"clock":1576408390,"ns":885729681},{"host":"10.0.0.30","key":"system.cpu.intr","value":"164526","id":43,"clock":1576408390,"ns":886469760},{"host":"10.0.0.30","key":"system.cpu.load[percpu,avg15]","value":"0.000000","id":44,"clock":1576408390,"ns":887003663},{"host":"10.0.0.30","key":"system.cpu.load[percpu,avg1]","value":"0.010000","id":45,"clock":1576408390,"ns":887473903},{"host":"10.0.0.30","key":"system.cpu.load[percpu,avg5]","value":"0.005000","id":46,"clock":1576408390,"ns":887917992},{"host":"10.0.0.30","key":"system.cpu.switches","value":"218790","id":47,"clock":1576408390,"ns":889276519},

4.5创建用户

单位中不可能每一个要看监控的职工都用管理员帐号进行查看,所以我们需要对不同用户进行分权

4.5.1创建用户群组,并且进行授权

4.5.2创建用户,加入群组

4.5.3登录测试