Following is the zabbix documentation with the query header format. Document Zabbix
I have difficulty implementing this header in C ++ (Arduino),
I saw an implementation of what I need in this forum here , however it is in Russian and is implemented in C #, I tried to translate it but to no avail.
So I need someone to help me write the function of the header examples in C ++ (Arduino).
Follows full Arduino code after @EdgarBonet change suggestion
#include <ArduinoJson.h>
#include <SPI.h>
#include <Ethernet.h>
// Config IP
IPAddress ip(192, 168, 0, 130);
IPAddress gw(192, 168, 0, 1);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
float contador = 0;
EthernetClient client;
//Config Server address
const char* server = "192.168.0.135";
int porta = 10051;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
}
void loop() {
// Begin Json
const size_t capacity = JSON_ARRAY_SIZE(1)
+ JSON_OBJECT_SIZE(2)
+ JSON_OBJECT_SIZE(3);
DynamicJsonBuffer jsonBuffer(capacity);
JsonObject& root = jsonBuffer.createObject();
root["request"] = "sender data";
JsonArray& data = root.createNestedArray("data");
JsonObject& data_0 = data.createNestedObject();
data_0["host"] = "ethernet";
data_0["key"] = "variavel1";
data_0["value"] = contador;
String json = "";
root.printTo(json);
const uint8_t *dataSend = (const uint8_t *) json.c_str();
uint64_t data_length = json.length();
uint8_t packet[13 + data_length]; // 13-byte header + payload
// Write the 5-byte signature.
memcpy(packet, "ZBXD1円", 5);
// Add the 8-byte packet length, LSB first.
for (int i = 0; i < 8; i++) {
packet[5+i] = data_length >> 8*i;
}
// Add the payload.
memcpy(packet + 13, dataSend, data_length);
if (client.connect(server, porta)) {
Serial.println("Enviando ao servidor");
delay(1000);
// Send the packet.
client.write(packet, sizeof packet);
delay(1000);
contador ++;
}
}
Following server response:
2034:20191003:101411.196 Message size 2066888786 from 192.168.0.130 exceeds the maximum size 1073741824 bytes. Message ignored.
1 Answer 1
The problem is quite explicit in the error message:
expected 808529969 bytes
You told the server that you were going to give it 808529969 bytes, yet you provided much less. Why did you tell it you were going to send so much data? Maybe we can get a clue by rewriting this number in hexadecimal:
808529969 = 0x30313031
Breaking this into individual bytes, LSB first (as this is how the server expects it), you have
0x31 0x30 0x31 0x30
These are the bytes you sent to the server, and they all happen to be
ASCII codes for digits, which make up the string "1010". I don't know
how you came up with this, as the code you posted is not the one you
tested (the posted code does not even compile!). But it seems at one
point you tried to convert something to ASCII binary, and indeed
String(10, BIN)
produces exactly the string above.
The server expects the data length to be provided as binary bytes,
not as an ASCII string. I personally try to avoid the String
class, which can create memory issues. In this situation I would just
work with an array of bytes, typed as uint8_t
(the same as the Arduino
byte
type, only more standard). In the code below, I assume for
simplicity that you keep a String
for building the payload, but then I
switch to a raw byte array:
const uint8_t *data = (const uint8_t *) json.c_str();
uint64_t data_length = json.length();
uint8_t packet[13 + data_length]; // 13-byte header + payload
// Write the 5-byte signature.
memcpy(packet, "ZBXD1円", 5);
// Add the 8-byte packet length, LSB first.
for (int i = 0; i < 8; i++) {
packet[5+i] = data_length >> 8*i;
}
// Add the payload.
memcpy(packet + 13, data, data_length);
// Send the packet.
client.write(packet, sizeof packet);
Note that when working with binary, rather than text data, you have to
use memcpy()
instead of strcpy()
, and client.write()
instead of
client.print()
.
-
1@VeniciusBack: You probably have enough reputation to edit your question. And you wouldn't want to answer anyway... unless you do have an answer!Edgar Bonet– Edgar Bonet2019年10月03日 13:37:34 +00:00Commented Oct 3, 2019 at 13:37
-
1@VeniciusBack: Also, when you provide your code and report an error message, make double sure that the error message your report is generated by the exact same version of the code you are showing. Otherwise you are wasting everyone's time. I am pretty sure the error message and the code your are showing do not match.Edgar Bonet– Edgar Bonet2019年10月03日 14:35:32 +00:00Commented Oct 3, 2019 at 14:35
-
1I edited the post, with the full code of the arduino I am sending and the response of that code provided by the zabbix server. Thanks!Venicius Back– Venicius Back2019年10月03日 14:55:54 +00:00Commented Oct 3, 2019 at 14:55
-
1@VeniciusBack: Do not edit your answer, which was deleted by VE7JRO for a very good reason (it's not really an answer). Edit your question.Edgar Bonet– Edgar Bonet2019年10月03日 15:34:39 +00:00Commented Oct 3, 2019 at 15:34
-
1@VeniciusBack: The way to credit is to "accept" an answer.Edgar Bonet– Edgar Bonet2019年10月04日 13:32:00 +00:00Commented Oct 4, 2019 at 13:32