4 Header and data length

概述

Zabbix组件之间的响应和请求消息中存在标头和数据长度。 需要确定消息的长度。

  1. <HEADER> - "ZBXD\x01" (5 bytes)
  2. <DATALEN> - data length (8 bytes). 1 will be formatted as 01/00/00/00/00/00/00/00 (eight bytes, 64 bit number in little-endian format)

为了不耗尽内存(可能),Zabbix协议仅限于在一个连接中仅接受128MB。

实施

以下是显示如何将Zabbix协议标头添加到的代码段 data想要 发送以获取 packet必须 发送到Zabbix以便正确解释。

LanguageCode
bash
  1. printf -v LENGTH ‘%016x ${#DATA}”
  2. PACK=””
  3. for i in {14..0..-2}; do PACK=”$PACK\x${LENGTH:$i:2}”; done
  4. printf ZBXD\1$PACK%s $DATA
Java
  1. byte[] header = new byte[] {
  2. Z’, B’, X’, D’, \1’,
  3. (byte)(data.length & 0xFF),
  4. (byte)((data.length >> 8) & 0xFF),
  5. (byte)((data.length >> 16) & 0xFF),
  6. (byte)((data.length >> 24) & 0xFF),
  7. \0’, \0’, \0’, \0’};
  8.  
  9. byte[] packet = new byte[header.length + data.length];
  10. System.arraycopy(header, 0, packet, 0, header.length);
  11. System.arraycopy(data, 0, packet, header.length, data.length);
PHP
  1. $packet = ZBXD\1 . pack(‘P’, strlen($data)) . $data;
or
  1. $packet = ZBXD\1 . pack(‘V’, strlen($data)) . \0\0\0\0 . $data;
Perl
  1. my $packet = ZBXD\1 . pack(‘<Q’, length($data)) . $data;
or
  1. my $packet = ZBXD\1 . pack(‘V’, length($data)) . \0\0\0\0 . $data;
Python
  1. packet = ZBXD\1 + struct.pack(‘<Q’, len(data)) + data

4 Header and data length

Overview

Header and data length are present in response and request messages between Zabbix components. It is required to determine the length of message.

  1. <HEADER> - "ZBXD\x01" (5 bytes)
  2. <DATALEN> - data length (8 bytes). 1 will be formatted as 01/00/00/00/00/00/00/00 (eight bytes, 64 bit number in little-endian format)

To not exhaust memory (potentially) Zabbix protocol is limited to accept only 128MB in one connection.

Implementation

Here are code snippets showing how to add Zabbix protocol header to the data you want to send in order to obtain packet you should send to Zabbix so it is interpreted correctly.

LanguageCode
bash
  1. printf -v LENGTH ‘%016x ${#DATA}”
  2. PACK=””
  3. for i in {14..0..-2}; do PACK=”$PACK\x${LENGTH:$i:2}”; done
  4. printf ZBXD\1$PACK%s $DATA
Java
  1. byte[] header = new byte[] {
  2. Z’, B’, X’, D’, \1’,
  3. (byte)(data.length & 0xFF),
  4. (byte)((data.length >> 8) & 0xFF),
  5. (byte)((data.length >> 16) & 0xFF),
  6. (byte)((data.length >> 24) & 0xFF),
  7. \0’, \0’, \0’, \0’};
  8.  
  9. byte[] packet = new byte[header.length + data.length];
  10. System.arraycopy(header, 0, packet, 0, header.length);
  11. System.arraycopy(data, 0, packet, header.length, data.length);
PHP
  1. $packet = ZBXD\1 . pack(‘P’, strlen($data)) . $data;
or
  1. $packet = ZBXD\1 . pack(‘V’, strlen($data)) . \0\0\0\0 . $data;
Perl
  1. my $packet = ZBXD\1 . pack(‘<Q’, length($data)) . $data;
or
  1. my $packet = ZBXD\1 . pack(‘V’, length($data)) . \0\0\0\0 . $data;
Python
  1. packet = ZBXD\1 + struct.pack(‘<Q’, len(data)) + data