Zabbix agent主动模式的实现
主动模式是由zabbix agent主动向zabbix server的10051端口发起TCP连接请求。因此主动模式下必须在zabbix agent配置文件中指定zabbix server的IP或者主机名(必须可以被解析为IP地址),在连接到zabbix server之前,zabbix agent是不知道自己要采集哪些数据以及间隔多久采集一次数据的,然后在连接到zabbix server以后获取到zabbix agent的监控项和数据采集间隔时间周期,然后再根据监控项采集数据并返回给zabbix server。在主动模式下不再需要zabbix server向zabbix agent发起连接请求,因此,主动模式在一定程度上可以减轻zabbix server打开的本地随机端口和进程数,在一定程度上可以减轻zabbix server的负载压力。
环境准备
四台机器:
zabbix_agent 5.0 被动模式(已安装) Zabbix_java_gateway 5.0 (已安装) 10.0.0.7
zabbix_server 5.0(已安装) 10.0.0.17
Tomcat-8.5(已安装) 10.0.0.27
zabbix_agent 5.0 主动模式 10.0.0.37
关闭防火墙和selinux
#关闭selinux
[root@centos7 ~]#sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
#关闭防火墙
[root@centos7 ~]#systemctl disable --now firewalld
#重启生效
[root@centos7 ~]#reboot
1. 安装zabbix rpm源
[root@centos7 ~]#rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
[root@centos7 ~]#sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo
[root@centos7 ~]#yum clean all
2. 安装zabbix agent
[root@centos7 ~]#yum install -y zabbix-agent
#编辑配置文件 /etc/yum.repos.d/zabbix.repo
[root@centos7 ~]#vim /etc/yum.repos.d/zabbix.repo
[zabbix-frontend]
...
enabled=1
...
3. 修改zabbix agent配置文件
[root@centos7 ~]#vim /etc/zabbix/zabbix_agentd.conf
Server=10.0.0.17
ListenPort=10050
ListenIP=0.0.0.0
StartAgents=5
ServerActive=10.0.0.17 #主动模式的zabbix server地址
Hostname=10.0.0.37
Timeout=30
[root@centos7 ~]#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.17
ListenPort=10050
ListenIP=0.0.0.0
StartAgents=5
ServerActive=10.0.0.17
Hostname=10.0.0.37
Timeout=30
Include=/etc/zabbix/zabbix_agentd.d/*.conf
4. 启动Zabbix agent进程
#启动Zabbix agent进程,并设置开机自启
[root@centos7 ~]#systemctl restart zabbix-agent.service
[root@centos7 ~]#systemctl enable zabbix-agent.service
[root@centos7 ~]#systemctl status zabbix-agent.service
● zabbix-agent.service - Zabbix Agent
Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-07-02 19:12:04 CST; 18s ago
Main PID: 1755 (zabbix_agentd)
CGroup: /system.slice/zabbix-agent.service
├─1755 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
├─1756 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
├─1757 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
├─1758 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
├─1759 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
├─1760 /usr/sbin/zabbix_agentd: listener #4 [waiting for connection]
├─1761 /usr/sbin/zabbix_agentd: listener #5 [waiting for connection]
└─1762 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
Jul 02 19:12:04 centos7 systemd[1]: Starting Zabbix Agent...
Jul 02 19:12:04 centos7 systemd[1]: Started Zabbix Agent.
#验证Zabbix agent进程是否存在
[root@centos7 ~]#ss -ntlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:10050 *:*users:(("zabbix_agentd",pid=1762,fd=4),("zabbix_agentd",pid=1761,fd=4),("zabbix_agentd",pid=1760,fd=4),("zabbix_agentd",pid=1759,fd=4),("zabbix_agentd",pid=1758,fd=4),("zabbix_agentd",pid=1757,fd=4),("zabbix_agentd",pid=1756,fd=4),("zabbix_agentd",pid=1755,fd=4))
LISTEN 0 128 *:22 *:*users:(("sshd",pid=1298,fd=3))
LISTEN 0 100 127.0.0.1:25 *:*users:(("master",pid=1461,fd=13))
LISTEN 0 128 [::]:22 [::]:*users:(("sshd",pid=1298,fd=4))
LISTEN 0 100 [::1]:25 [::]:*users:(("master",pid=1461,fd=14))
[root@centos7 ~]#ps -ef | grep zabbix_agentd
zabbix 1755 1 0 19:12 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 1756 1755 0 19:12 ? 00:00:00 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix 1757 1755 0 19:12 ? 00:00:00 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix 1758 1755 0 19:12 ? 00:00:00 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix 1759 1755 0 19:12 ? 00:00:00 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix 1760 1755 0 19:12 ? 00:00:00 /usr/sbin/zabbix_agentd: listener #4 [waiting for connection]
zabbix 1761 1755 0 19:12 ? 00:00:00 /usr/sbin/zabbix_agentd: listener #5 [waiting for connection]
zabbix 1762 1755 0 19:12 ? 00:00:00 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
root 1788 1565 0 19:14 pts/0 00:00:00 grep --color=auto zabbix_agentd
5. 生成主动模式模板
点击【配置-主机-10.0.0.7-web1-克隆】
6. 验证主动模式主机状态
#【ZBX】绿色,表示主机可用,监控指标检查已成功
7. 验证主动模式主机端口
[root@zabbix_server ~]#netstat -tanlp | grep 10.0.0.37
tcp 0 0 10.0.0.17:10051 10.0.0.37:43072 TIME_WAIT -
tcp 0 0 10.0.0.17:10051 10.0.0.37:43062 TIME_WAIT -
tcp 0 0 10.0.0.17:10051 10.0.0.37:43070 TIME_WAIT -
tcp 0 0 10.0.0.17:10051 10.0.0.37:43068 TIME_WAIT -
tcp 0 0 10.0.0.17:10051 10.0.0.37:43066 TIME_WAIT -
tcp 0 0 10.0.0.17:10051 10.0.0.37:43064 TIME_WAIT -
8. 验证主动模式主机数据
9.把【自动发现规则】的时间间隔改为60s
点击【配置-主机-10.0.0.37-web2-自动发现规则】