本节提供了有关由Zabbix agent和Zabbix agent 2执行的被动检查和主动检查的详细信息。
Zabbix 使用基于 JSON 的通信协议与agents进行通信。
自 Zabbix 7.0 起,Zabbix agent和Zabbix agent 2协议已统一。Zabbix agent和Zabbix agent 2请求/响应之间的区别通过 "variant" 标签值来表达。
被动检查是一个简单的数据请求。Zabbix server 或 proxy 请求某些数据(例如,CPU负载),然后 Zabbix agent 将结果发送回服务器。
被动检查是异步执行的 - 在开始其他检查之前不需要接收一个请求的响应。DNS解析也是异步的。
agent 轮询器将尝试连接到DNS查找返回的所有地址。这确保了如果一个IP地址无法访问,轮询器将尝试下一个可用地址,从而增加成功连接的可能性。此增强功能适用于 Zabbix server 和 proxy。
异步检查的最大并发数是1000(由 maxconcurrentchecksperpoller 定义)。
异步 agent 轮询器的数量由 启动代理轮询器 参数定义。
服务器请求
关于头部和数据长度的定义,请参考 protocol details。
字段 | 类型 | 必填 | 值 | |
---|---|---|---|---|
request | string | yes | "passive checks" |
|
data | array of object | yes | 被动检查 监控项。 | |
key | string | yes | 包含扩展宏的 监控项 键。 | |
timeout | string | yes | 监控项 超时时间。 |
Agent 响应
字段 | 类型 | 必填 | 值 | |
---|---|---|---|---|
version | string | yes | agent version 编号。 | |
variant | number | yes | agent 变体(1 - Zabbix agent,2 - Zabbix agent 2)。 | |
data | array of object | yes | 包含检查结果。 | |
value | string | no | 如果检查成功,则为 监控项 值。 | |
error | string | no | 如果检查不成功,则为错误信息。 |
例如,对于支持的 监控项:
对于不支持的 监控项:
为了确保 Zabbix server 或 proxy 可以与使用明文协议的预 7.0 版本中的 agents 正常工作,实现了向旧协议的故障转移。
被动检查在重启后或接口配置更改后使用 JSON 协议(7.0 及更新版本)执行。
如果响应中未收到有效的 JSON(例如 agent 返回了 "ZBX_NOTSUPPORTED"),Zabbix 将缓存该接口为旧协议,并通过仅发送 监控项 键来重试检查。
请注意,每小时 Zabbix server/proxy 都会尝试使用新协议与所有接口通信,如需要则回退到旧协议。
主动检查需要更复杂的处理。 agent 必须首先从 server/proxy 获取一个 监控项 和/或 remote commands 的列表,以进行独立处理。
服务器/proxies 到 get 主动检查的来源列在 agent configuration file 的 "ServerActive" 参数中。 这些检查的请求频率由同一配置文件中的“RefreshActiveChecks”参数设置。 但是,如果刷新主动检查失败,则会在硬编码的 60 秒后重试。
自 Zabbix 6.4 起,agent(在主动模式下)不再每隔两分钟(默认值)从 server/proxy 接收完整的 copy 配置。 为了减少网络流量和资源使用,现在每隔 5 秒(默认值)执行一次增量配置同步。在此过程中,只有当 agent 尚未接收过完整的配置,或者 主机 配置、全局宏或全局正则表达式发生了更改时,server/proxy 才会提供完整的 copy 配置。
agent会定期将新数值发送至服务器。 若agent接收到任何remote commands以execute,执行结果也将被发送。 请注意,自Zabbix agent 7.0起支持在主动式agent上执行远程命令。
若agent位于防火墙后方,可考虑 仅使用主动式监控,因为此情况下无需 修改防火墙以允许初始入站连接。
Agent 请求
主动检查请求用于获取由agent处理的待执行主动检查项。 此请求由agent在启动时发送,随后以RefreshActiveChecks为间隔发送。
{
"request": "active checks",
"host": "Zabbix server",
"host_metadata": "mysql,nginx",
"interface": "zabbix.server.lan",
"ip": "159.168.1.1",
"port": 12050,
"version": "7.0.0",
"variant": 2,
"config_revision": 1,
"session": "e3dcbd9ace2c9694e1d7bbd030eeef6e"
}
字段 | 类型 | 必填 | 值 |
---|---|---|---|
request | string | yes | active checks |
host | string | yes | 主机名。 |
host_metadata | string | no | 配置参数HostMetadata或HostMetadataItem的指标值。 |
interface | string | no | 配置参数HostInterface或HostInterfaceItem的指标值。 |
ip | string | no | 配置参数ListenIP的首个IP地址(若设置)。 |
port | number | no | 若配置参数ListenPort的值被设置且非默认值,则为agent监听端口。 |
version | string | yes | agent version 编号。 |
variant | number | yes | agent变体(1 - Zabbix agent,2 - Zabbix agent 2)。 |
config_revision | number | no | 用于增量配置同步的配置标识符。 |
session | string | no | 用于增量配置同步的会话标识符。 |
服务器响应
服务器在处理主动检查请求后,将主动检查响应发送回agent。
{
"response": "success",
"config_revision": 2,
"data": [
{
"key": "system.uptime",
"itemid": 1234,
"delay": "10s",
"lastlogsize": 0,
"mtime": 0
},
{
"key": "agent.version",
"itemid": 5678,
"delay": "10m",
"lastlogsize": 0,
"mtime": 0,
"timeout": "30s"
}
],
"commands": [
{
"command": "df -h --output=source,size / | awk 'NR>1 {print $2}'",
"id": 1324,
"wait": 1
}
]
}
字段 | 类型 | 必填 | 值 | |
---|---|---|---|---|
response | string | yes | success \ |
|
info | string | no | 失败时的错误信息。 | |
data | array of objects | no | 主动检查监控项。若主机配置未更改则省略。 | |
键 | string | no | 监控项 带扩展宏的键。 | |
监控项ID | number | no | 监控项标识符 | |
延迟 | string | no | 监控项 update 间隔。 自 Zabbix 7.0 起,Zabbix agent 和 Zabbix agent 2 均支持灵活/调度间隔。 |
|
最后日志大小 | number | no | 监控项 lastlogsize | |
修改时间 | number | no | 监控项 修改时间。 | |
超时 | string | no | 监控项 超时。 | |
refresh_unsupported | number | no | 不支持的 监控项 刷新间隔。 | |
regexp | array of objects | no | 全局正则表达式 | |
名称 | string | no | 全局正则表达式名称。 | |
表达式 | string | no | 全局正则表达式 | |
expression_type | number | no | 全局正则表达式类型。 | |
exp_delimiter | string | no | 全局正则表达式分隔符。 | |
区分大小写 | number | no | 全局正则表达式大小写敏感设置。 | |
commands | array of objects | no | 发送至execute的远程命令。当远程命令执行由动作operation触发或手动script执行时包含该字段。请注意,自Zabbix agent 7.0版本起支持在主动式agent上执行远程命令。较早版本的主动式agents将忽略主动检查服务器响应中包含的任何远程命令。 | |
command | string | no | 远程命令 | |
id | number | no | 远程命令标识符。 | |
等待 | number | no | 远程命令执行模式("0" (nowait) 用于来自operations动作的命令;"1" (wait) 用于来自手动script执行的命令)。 | |
config_revision | number | no | 增量配置同步的配置标识符。若主机配置未更改则省略。若主机配置发生变更则递增。 |
服务器必须返回成功响应。
例如:
请注意,(敏感)配置数据可能 可供拥有访问 Zabbix server trapper 权限的各方使用 在使用主动检查时指定端口。这是可行的,因为任何人都可以 假装是一个活跃的agent并请求监控项配置数据 除非使用,否则不会进行身份验证 encryption 选项。
Agent发送
agent数据请求包含收集的监控项值和执行的远程命令的值(如果有的话)。
{
"request": "agent data",
"data": [
{
"id": 1,
"itemid": 5678,
"value": "7.0.0",
"clock": 1712830783,
"ns": 76808644
},
{
"id": 2,
"itemid": 1234,
"value": "69672",
"clock": 1712830783,
"ns": 77053975
}
],
"commands": [
{
"id": 1324,
"value": "16G"
}
],
"session": "1234456akdsjhfoui",
"host": "Zabbix server",
"version": "7.0.0",
"variant": 2
}
字段 | 类型 | 必填 | 值 | |
---|---|---|---|---|
request | string | yes | agent data |
|
data | array of objects | yes | 监控项值。 | |
id | number | yes | 值标识符(在网络问题情况下用于检查重复值的递增计数器)。 | |
itemid | string | yes | 监控项标识符。 | |
value | string | no | 监控项值。 | |
lastlogsize | number | no | 监控项 lastlogsize。 | |
mtime | number | no | 监控项 mtime。 | |
state | number | no | 监控项状态。 | |
source | string | no | 值事件日志源。 | |
eventid | number | no | 值事件日志eventid。 | |
severity | number | no | 值事件日志严重性。 | |
timestamp | number | no | 值事件日志时间戳。 | |
clock | number | yes | 值时间戳(自Epoch以来的秒数)。 | |
ns | number | yes | 值时间戳的纳秒数。 | |
commands | array of objects | no | 远程命令执行结果。请注意,主动agent上的远程命令执行自Zabbix agent 7.0版本开始支持。旧版主动agents将忽略主动检查服务器响应中包含的任何远程命令。 | |
id | number | no | 远程命令标识符。 | |
value | string | no | 如果执行成功,则为远程命令执行结果。 | |
error | string | no | 如果执行失败,则为远程命令执行错误消息。 | |
session | string | yes | 每次启动agent时生成的唯一会话标识符。 | |
host | string | yes | 主机名。 | |
version | string | yes | agent version编号。 | |
variant | number | yes | agent变体(1 - Zabbix agent, 2 - Zabbix agent 2)。 |
每个值都会分配一个虚拟ID。值ID是一个简单的递增计数器,在一个数据会话内(由会话令牌标识)唯一。此ID用于丢弃在网络连接不良的环境中可能发送的重复值。
服务器响应
在处理完agent数据请求后,服务器将agent数据响应发送回agent。
字段 | 类型 | 必填 | 值 |
---|---|---|---|
response | string | yes | success \ |
info | string | yes | 监控项处理结果。 |
如果某些值在服务器端发送失败(例如,因为主机或监控项已被禁用或删除),agent将不会重试发送这些值。
例如:
请注意,在上面的示例中,vfs.fs.size[/nono]的不支持状态通过"state"值1和"value"属性中的错误消息表示。
服务器端会将错误消息截断为2048个字符。
Agent发送
心跳消息由活跃的agent每隔HeartbeatFrequency秒(在Zabbix agent/agent 2配置file中设置)发送给Zabbix server/proxy。
该消息用于监控主动检查的可用性。
{
"request": "active check heartbeat",
"host": "Zabbix server",
"heartbeat_freq": 60,
"version": "7.0.0",
"variant": 2
}
字段 | 类型 | 必填 | 值 |
---|---|---|---|
request | string | yes | active check heartbeat |
host | string | yes | 主机名称 |
heartbeat_freq | number | yes | agent心跳频率(HeartbeatFrequency配置参数) |
version | string | yes | agentversion编号 |
variant | number | yes | agent变体(1-Zabbix agent,2-Zabbix agent 2) |
重定向响应
当一个主机被重新分配时,服务器可能指示agent将其心跳(及后续主动检查)重定向到另一个proxy或服务器实例。
字段 | 类型 | 必填 | 值 | |
---|---|---|---|---|
response | string | yes | success \ |
|
redirect | object | yes | 重定向指令 | |
revision | number | yes | 配置修订标识符 | |
address | string | yes | 目标server/proxy地址 |
Zabbix 将占用最多 16 MB 的 XML Base64 编码数据,但单个解码后的值不应超过 64 KB,否则在解码时将被截断为 64 KB。