
本文共 2619 字,大约阅读时间需要 8 分钟。
���������������Zabbix API ������������
���������������
������������������������������������������������������������������������������������������������������
pip3 install urllib_kerberospip3 install requestspip3 install pyyaml
���������������������������������������������������
Python-Zabbix���������������������
������������
������������������������ Zabbix ��� Python ������
pip3 install pyzabbix
������������������
��������� Zabbix API ������������������������������ Zabbix ������������������������ config.yml
���������������������������������
user: adminpassword: your_passwordserver: http://your_zabbix_server
������������
������ Common
��������� Zabbix���������
from pyzabbix import commonclass Common(object): def __init__(self): self.url = f"{config['server']}/zabbix/api_jsonrpc.php" # ��������������������� self.user ��� self.password def get_token(self): request_body = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": self.user, "password": self.password }, "id": 1 } token = requests.post(self.url, json=request_body, verify=False).json()['result'] return token
Zabbix ������������
������ host.get
���������������������������
def get_host_info(host_ids): zbx = Common() params = { "host": host_ids, "expand": True } result = zbx.zabbix_get('host.get', params) return result.json()['result']
������������������
Host ������
������������������������������������������������������
from pyzabbix import commonfrom pyzabbix import modelsclass HostManager: def __init__(self): self.common = common.Common() self.host_info = self.get_host_info("ceph-mgr") def get_host_info(self, host_id): params = { "filter": { "host": [host_id] } } return self.common.zabbix_get("host.get", params)
���������������������
������������������������������������������������������
def main(): host_manager = HostManager() # ��������������������������������� host_details = host_manager.get_host_info("ceph-mgr") print(json.dumps(host_details, indent=4))
������������������
config.yml
���������������������
user: Adminpassword: your_passwordserver: http://localhost:8080
���������������������������������������������������������
��������������������������������������������������� Zabbix API ��������������������������������������������� Zabbix API ���������������������������������������������������������
发表评论
最新留言
关于作者
