4 Header标头
概述
标头存在于 Zabbix 组件之间的响应和请求消息中。需要确定消息的长度,是否压缩以及消息长度、字段的格式。标头包含:
<PROTOCOL> - "ZBXD" (4 bytes).
<FLAGS> - the protocol flags, (1 byte). 0x01 - Zabbix communications protocol, 0x02 - compression, 0x04 - large packet).
<DATALEN> - data length (4 bytes or 8 bytes for large packet). 1 will be formatted as 01/00/00/00 (four bytes, 32 bit number in little-endian format) or 01/00/00/00/00/00/00/00 (eight bytes, 64 bit number in little-endian format) for large packet.
<RESERVED> - uncompressed data length (4 bytes or 8 bytes for large packet). 1 will be formatted as 01/00/00/00 (four bytes, 32 bit number in little-endian format) or 01/00/00/00/00/00/00/00 (eight bytes, 64 bit number in little-endian format) for large packet.
当启用压缩 (0x02 flag) 时,<RESERVED> 字节包含未压缩的数据大小
Zabbix协议每个连接有1GB的数据包大小限制。1GB的限制适用于接收的数据包数据长度和未压缩的数据长度,但是,当启用大数据包 (0x04 flag) 时,Zabbix proxy 可以接收大小高达16GB的配置;注意大的数据包只能用于Zabbix proxy 配置,当压缩前的数据长度超过4GB时,Zabbix服务器将自动设置长度 (0x04 flag) 并发送字段,每个字段为8字节。
实现
以下代码片段显示了如何将Zabbix协议头添加到要发送的数据中,以便获得应该发送给Zabbix的数据包,从而正确解释它。
Language | Code |
---|---|
bash | printf -v LENGTH ‘%016x’ “${#DATA}”PACK=””for (( i=14; i>=0; i-=2 )); do PACK=”$PACK\x${LENGTH:$i:2}”; doneprintf “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; or$packet = “ZBXD\1” . pack(‘V’, strlen($data)) . “\0\0\0\0” . $data; |
Perl | my $packet = “ZBXD\1” . pack(‘<Q’, length($data)) . $data; ormy $packet = “ZBXD\1” . pack(‘V’, length($data)) . “\0\0\0\0” . $data; |
Python | packet = “ZBXD\1” + struct.pack(‘<Q’, len(data)) + data |