使用Zabbix 7.0内建Scheduled Report,如何生成“上周SLA违约Top10”PDF并自动发送到飞书群?
解读
面试官想验证三件事:
- 对Zabbix 7.0原生Scheduled Report(基于TimescaleDB+Grafana PDF引擎)的完整配置链路是否真正落地过;
- 能否把“SLA违约”抽象成可计算的Zabbix指标,而不是简单拉取触发器数量;
- 在中国企业常用的飞书生态里,如何合规、稳定地把PDF送进群聊,而不是停留在“发邮件”层面。
因此,回答必须给出“数据定义→报表模板→调度→飞书推送”四段闭环,任何一段缺失都会扣分。
知识点
- SLA对象:Zabbix 7.0把SLA独立成Service模块,支持SLA、SLO、SLI三级定义,底层表services、service_sla、service_problem。
- 违约判定:SLA<目标值即违约,需用service.sla.value函数按“上周”时间窗口聚合。
- Scheduled Report:7.0新增zabbix-web-service组件,调用无头Chromium生成PDF,依赖zabbix-web-service、nginx、TimescaleDB连续聚合。
- 飞书群机器人:国内版本仅支持“上传文件”接口(https://open.feishu.cn/open-apis/im/v1/files),需先获取tenant_access_token,再把PDF以multipart/form-data POST,消息体type=file。
- 安全合规:飞书 tenant_access_token 有效期2 h,必须在Zabbix Server侧用定时脚本刷新并写入/etc/zabbix/fs_credential,禁止硬编码到前端。
答案
步骤一:定义SLA
- 在“Services”新建业务树,例如“电商支付”、“网关API”等,勾选“Calculate SLA”。
- 设置SLA目标值99.9%,周期Weekly,违约阈值<99.9%。
- 把对应触发器Tag serviceid=<serviceid>绑定到Service,确保problem与service关联。
步骤二:创建“上周SLA违约Top10”Dashboard
- 新建Dashboard → Add → Service SLA widget → 指标选“SLA”→ Time range 选“Previous week”。
- 排序选Ascending,Limit 10,即违约最严重的10条。
- 另加Panel展示“违约时长(秒)”“影响触发器数”,方便领导一眼看懂。
- 保存并记住Dashboard ID。
步骤三:配置Scheduled Report
- Administration → Scheduled reports → Create report:
- Name:上周SLA违约Top10
- Dashboard:选中刚建的Dashboard
- Period:Previous week
- Cycle:Weekly,每周一 08:00
- Export format:PDF
- Subject:{{DATE}}-上周SLA违约Top10
- 在“Recipients”里先留空(因为原生只支持邮件,我们后面用脚本推飞书)。
- 保存后得到reportid=123。
步骤四:飞书推送脚本(Python3,放/etc/zabbix/feishu_report.py)
#!/usr/bin/env python3
import requests, json, os, datetime, mimetypes
reportid = 123
today = datetime.date.today().strftime('%Y%m%d')
pdf_path = f'/var/lib/zabbix/reports/{reportid}/{today}_sla_top10.pdf'
1. 等待zabbix-web-service生成PDF,轮询最多300 s
for i in range(30):
if os.path.exists(pdf_path): break
time.sleep(10)
2. 获取tenant_access_token
cred = json.load(open('/etc/zabbix/fs_credential'))
tenant = cred['tenant']
app_id, app_secret = cred['app_id'], cred['app_secret']
token_resp = requests.post('https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/',
json={'app_id': app_id, 'app_secret': app_secret}).json()
token = token_resp['tenant_access_token']
3. 上传文件
with open(pdf_path, 'rb') as f:
files = {'file': (os.path.basename(pdf_path), f, 'application/pdf')}
upload = requests.post('https://open.feishu.cn/open-apis/im/v1/files',
headers={'Authorization': f'Bearer {token}'},
files=files,
data={'file_type': 'pdf', 'file_name': os.path.basename(pdf_path)}).json()
file_key = upload['data']['file_key']
4. 发送群消息
chat_id = cred['chat_id']
requests.post('https://open.feishu.cn/open-apis/im/v1/messages',
headers={'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'},
json={'chat_id': chat_id, 'msg_type': 'file', 'content': json.dumps({'file_key': file_key})})
步骤五:把脚本挂到Zabbix调度
- 在Scheduled Report里把“Send to”填一个虚拟邮箱,再在/etc/zabbix/zabbix_server.conf里加
ReportScript=/etc/zabbix/feishu_report.py - 重载zabbix-server,测试手动执行脚本,确认飞书群收到带PDF卡片。
- 观察/var/log/zabbix/report.log,确保无Chromium超时或飞书接口429。
至此,每周一08:05领导在飞书群即可收到“上周SLA违约Top10”PDF,全程无人工干预。
拓展思考
- 如果飞书群文件上限100 MB,而PDF超过限制,可先在脚本里用ghostscript压缩,或改用“图片+链接”方式。
- 当Zabbix Server无法直接访问公网时,可在DMZ放一台nginx反向代理,只开放open.feishu.cn域名,走HTTPS 443,满足金融公司“监控网不能出公网”的合规要求。
- 若企业同时用钉钉、企业微信,可把脚本抽象成通用消息网关,通过环境变量切换chat_type,实现一份报表三端推送。
- 7.0的Scheduled Report目前不支持把“变量”传入Dashboard,比如动态选择业务线;如需多租户,可提前克隆Dashboard并分别建report,或在脚本里调用Zabbix API动态生成临时Dashboard再删除,实现“千人千面”。