核心接口文件 zabbix_api_custom 是Python3环境所写,RHEL 7自带的Python2无法使用,需自行编译部署Python3(Python 3.9.0)运行环境,再结合Supervisor进程管理程序进行管理。
zabbix_api_custom.py 接口文件
[root@sh2-svvl-q910 zabbix_api_custom]# cat zabbix_api_custom.py
import requests
import json
from fastapi import FastAPI
from pydantic import BaseModel, constr
import uvicorn
import traceback
class zabbix_api(object):
def __init__(self, group, ip):
self.url = 'http://192.168.9.10:8999/api_jsonrpc.php'
self.group = group
...
部署zabbix_api_custom.py运行环境
[root@sh2-svvl-q910 ~]# pip3 install fastapi
[root@sh2-svvl-q910 ~]# pip3 install requests
[root@sh2-svvl-q910 ~]# pip3 install uvicorn
通过Supervisor托管Python进程
[root@sh2-svvl-q910 ~]# cat /etc/supervisord.d/zabbix_api_custom.ini
[program:zabbix_api_custom]
command=python3 /root/Python/zabbix_api_custom/zabbix_api_custom.py
autorstart=true
autorestart=true
startsecs=5
startretries=100
stdout_logfile=/root/Python/zabbix_api_custom/access.log
stdout_logfile_maxbytes=100MB
[root@sh2-svvl-q910 ~]# systemctl start supervisord.service && systemctl enable supervisord.service
通过Supervisor托管Python进程(更新)
[root@sh2-svvl-q910 ~]# cat /etc/supervisord.d/zabbix_api_custom.ini
[program:zabbix_api_custom]
command = /root/python-app/venv/bin/python /root/Python/zabbix_api_custom/zabbix_api_custom.py # Supervisor支持配置激活python虚拟环境,需要注意:command 启动命令不直接使用python,而是使用虚拟环境里的python,也可以directory=/.../ 指定环境所在目录
environment=PATH= "/root/python-app/venv/bin"
autorstart=true
autorestart=true
startsecs=5
startretries=100
stdout_logfile=/root/Python/zabbix_api_custom/access.log
stdout_logfile_maxbytes=100MB
[root@sh2-svvl-q910 ~]# systemctl start supervisord.service && systemctl enable supervisord.service
接口访问
URL : http://x.x.x.x:8001/docs # 具体监控大屏所需展示内容由研发部进一步调用接口,通过http调用zabbix的api接口资料参考:https://www.zabbix.com/documentation/5.0/zh/manual/api
{
"group": "string", # 监控分组
"ip": "stringst" # 主机IP
}