Sidebar

zh:manual:appendix:protocols:header_datalen

Table of Contents

4 标头

概述

Zabbix组件之间的响应和请求消息中包含标头。标头用来确定消息的长度。标头包括:

<PROTOCOL> - "ZBXD" (4 bytes).
<FLAGS> -the protocol flags, (1 byte). 0x01 - Zabbix communications protocol, 0x02 - compression).
<DATALEN> - data length (4 bytes). 1 will be formatted as 01/00/00/00 (four bytes, 32 bit number in little-endian format).
<RESERVED> - reserved for protocol extensions (4 bytes).

当启用压缩(0x02标志)时,<RESERVED>字节包含未压缩的数据大小,即32 bit小字节序。

Zabbix协议每个连接的数据包大小限制为1GB。

Implementation

这些代码片段展示了如何将Zabbix协议标头添加到你想要发送的数据中,从而使zabbix正确地解析数据包

语言代码
bash
printf -v LENGTH '%016x' "${#DATA}"
PACK=""
for (( i=14; i>=0; i-=2 )); do PACK="$PACK\\x${LENGTH:$i:2}"; done
printf "ZBXD\1$PACK%s" "$DATA"
Java
byte[] header = new byte[] {
	'Z', 'B', 'X', 'D', '\1',
	(byte)(data.length & 0xFF),
	(byte)((data.length >> 8) & 0xFF),
	(byte)((data.length >> 16) & 0xFF),
	(byte)((data.length >> 24) & 0xFF),
	'\0', '\0', '\0', '\0'};
 
byte[] packet = new byte[header.length + data.length];
System.arraycopy(header, 0, packet, 0, header.length);
System.arraycopy(data, 0, packet, header.length, data.length);
PHP
$packet = "ZBXD\1" . pack('P', strlen($data)) . $data;
$packet = "ZBXD\1" . pack('V', strlen($data)) . "\0\0\0\0" . $data;
Perl
my $packet = "ZBXD\1" . pack('<Q', length($data)) . $data;
my $packet = "ZBXD\1" . pack('V', length($data)) . "\0\0\0\0" . $data;
Python
packet = "ZBXD\1" + struct.pack('<Q', len(data)) + data