zabbix-7.2 部署


1、环境

1、文件下载

https://www.zabbix.com/download

2、选择平台

7.2 --> Rocky Linux --> 9 --> Server,Frontend,Agent --> Mysql --> Nginx

2、安装和配置

1、禁用 EPEL 提供的 Zabbix 软件包(如果安装了它)

vim /etc/yum.repos.d/epel.repo

[epel]
...
excludepkgs=zabbix*

2、安装 zabbix 仓库

rpm -Uvh https://repo.zabbix.com/zabbix/7.2/release/rocky/9/noarch/zabbix-release-latest-7.2.el9.noarch.rpm
dnf clean all

3、安装 Zabbix 服务器、前端、代理

dnf install -y zabbix-server-mysql zabbix-web-mysql zabbix-nginx-conf zabbix-sql-scripts zabbix-selinux-policy zabbix-agent

4、初始数据库

mysql -uroot -p

mysql> create database zabbix character set utf8mb4 collate utf8mb4_bin;
mysql> create user zabbix@'%' identified by 'password';
mysql> grant all privileges on zabbix.* to zabbix@'%';
mysql> set global log_bin_trust_function_creators = 1;
mysql> quit;

在 Zabbix 服务器主机上导入初始模式和数据。系统将提示您输入新创建的密码
zcat /usr/share/zabbix/sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix

导入数据库架构后禁用 log_bin_trust_function_creators 选项

mysql -uroot -p

mysql> set global log_bin_trust_function_creators = 0;
mysql> quit;

5、为 Zabbix 服务器配置数据库

vim /etc/zabbix/zabbix_server.conf

DBPassword=password

6、编辑文件 vim /etc/nginx/conf.d/zabbix.conf 取消注释并设置 'listen' 和 'server_name' 指令

# listen 8080;
# server_name example.com;

7、启动 Zabbix 服务器和代理进程

systemctl restart zabbix-server zabbix-agent nginx php-fpm
systemctl enable zabbix-server zabbix-agent nginx php-fpm

BUG

1、unix:/run/php-fpm/zabbix.sock failed

2025/01/17 13:51:30 [crit] 3077486#0: *13 connect() to unix:/run/php-fpm/zabbix.sock failed (13: Permission denied) while connecting to upstream, client: 172.16.46.193, server: 22.50.8.4, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/zabbix.sock:", host: "22.50.8.4:8080"

查看 nginx.conf 的用户,比如为 nginx
chown nginx.nginx /run/php-fpm/zabbix.sock 

2、connection to database 'zabbix' failed

3084717:20250117:142044.070 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local server through socket '/var/lib/mysql/mysql.sock' (2)
3084717:20250117:142044.070 database is down: reconnecting in 10 seconds

tail -f /var/log/zabbix/zabbix_server.log
vim /etc/zabbix/zabbix_server.conf
DBSocket=/tmp/mysql.sock
systemctl restart zabbix-server zabbix-agent php-fpm

3、Database host 连接失败

如果是默认值 localhost,可以改为具体的 ip 即可

3、配置企微告警

vim /usr/lib/zabbix/alertscripts/wechat.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
import requests
import json
import sys
 
# Webhook URL
api_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e6c"
 
# HTTP headers
headers = {'Content-Type': 'application/json;charset=utf-8'}
 
# 定义发送消息的函数
def send_message(text):
    # 构建消息体
    json_text = {
        "msgtype": "text",
        "text": {
            "content": text
        }
    }
    
    # 发送 POST 请求
    response = requests.post(api_url, json=json_text, headers=headers)
    
    # 检查响应状态码
    if response.status_code == 200:
        print("Message sent successfully.")
    else:
        print(f"Failed to send message. Status code: {response.status_code}")
 
# 主函数
if __name__ == '__main__':
    # 检查命令行参数是否正确
    if len(sys.argv) < 2:
        print("Usage: python3 wechat.py <message>")
        sys.exit(1)
    
    # 获取命令行参数中的消息内容
    text = sys.argv[1]
    
    # 调用发送消息函数
    send_message(text)

chmod +x /usr/lib/zabbix/alertscripts/wechat.py
脚本参数-值 {ALERT.MESSAGE}

消息类型:问题
​主题:告警通知
消息:
事件名称:{EVENT.NAME}
告警地址:{HOST.NAME}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警严重性:{EVENT.SEVERITY}
当前状态:{EVENT.STATUS}
告警时间:{EVENT.DATE} {EVENT.TIME}
事件ID:{EVENT.ID}

消息类型:问题恢复
主题:恢复通知
消息:
事件名称:{EVENT.NAME}
告警地址:{HOST.NAME}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警严重性:{EVENT.SEVERITY}
当前状态:{EVENT.RECOVERY.STATUS}
告警时间:{EVENT.DATE} {EVENT.TIME}
恢复时间:{EVENT.DATE} {EVENT.RECOVERY.TIME}
持续时间:{EVENT.AGE}
事件ID:{EVENT.RECOVERY.ID}

// 在 host 中的 Macros 去修改默认阈值
// 参考 https://blog.csdn.net/sylzp123/article/details/144810068

4、agent 安装

// Zabbix 官方仓库
https://repo.zabbix.com/zabbix/

1. 添加 Zabbix 官方仓库

dnf install https://repo.zabbix.com/zabbix/7.2/release/rocky/9/noarch/zabbix-release-7.2-1.el9.noarch.rpm 
dnf clean all

2. 安装配置 Zabbix Agent

dnf install -y zabbix-agent

vim /etc/zabbix/zabbix_agentd.conf
#被动模式 Server=Zabbix_Server_IP
主动模式
ServerActive=22.50.8.4
Hostname=testx
#HostnameItem=system.hostname
HostMetadata=Linuxx

还需要创建自动注册动作:
登录 Zabbix Web 界面
导航到 Configuration -> Actions

3. 启动并启用 Zabbix Agent

systemctl status zabbix-agent
systemctl restart zabbix-agent
systemctl enable zabbix-agent
systemctl status zabbix-agent

参考文献

https://www.zabbix.com/documentation/7.2/zh/manual/quickstart/login

             ------------------------------- THE END -------------------------------

posted @ 2025-02-13 11:24  ordinaryRoadX  阅读(19)  评论(0编辑  收藏  举报