On this page

调试日志

在使用 Zabbix API、从 Zabbix agent 收集数据,或向 Zabbix 服务器或 proxy 发送数据时,如需排查问题,可以启用调试日志记录。

zabbix_utils 库使用 Python 标准的 logging 模块。

要查看该库输出的详细调试消息:

  1. 在脚本中导入 Python 的 logging 模块。
  2. 配置日志系统,以可读格式输出消息,并将日志级别设置为 DEBUG。
import logging
from zabbix_utils import Getter

logging.basicConfig(
    format=u'[%(asctime)s] %(levelname)s %(message)s',
    level=logging.DEBUG
)

agent = Getter(host='127.0.0.1', port=10050)
resp = agent.get('system.uname')

print(resp.value)

启用调试日志记录后,将打印有关请求和响应的详细信息,例如:

[2023-10-01 12:00:01,587] DEBUG Content of the packet: b'ZBXD\x01\x0c\x00\x00\x00\x00\x00\x00\x00system.uname'
[2023-10-01 12:00:01,722] DEBUG Zabbix response header: b'ZBXD\x01C\x00\x00\x00C\x00\x00\x00'
[2023-10-01 12:00:01,723] DEBUG Zabbix response body: Linux test_server 5.15.0-3.60.5.1.el9uek.x86_64
[2023-10-01 12:00:01,724] DEBUG Response from [127.0.0.1:10050]: Linux test_server 5.15.0-3.60.5.1.el9uek.x86_64
Linux test_server 5.15.0-3.60.5.1.el9uek.x86_64