Ad Widget

Collapse

zabbix 4, end of query from zabbix server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • katigoroshek
    Junior Member
    • Jun 2016
    • 3

    #1

    zabbix 4, end of query from zabbix server

    Добрый день.

    На Ардуине был такой код (взято тут):
    Code:
     #include <OneWire.h> #include <UIPEthernet.h>  byte mac[] = { 0xDE, 0x05, 0xB6, 0x27, 0x39, 0x19 }; // random MAC byte ip[] = { 192, 168, 4, 5 }; // IP address in local network String readString = String(20);  byte addr[8];  OneWire ds(4); // DS18B20 at pin 4 EthernetServer server(10050); // Zabbix port  void setup() {   Ethernet.begin(mac, ip);   server.begin();   ds.search(addr); }  void loop() {   byte data[2];   float celsius;    readString = "";   if (EthernetClient client = server.available())   {     while (client.connected()) {       if (client.available()) {         char c = client.read();         if (c == '\n') // end of query from zabbix server          {           client.print("ZBXD\x01"); // response header           if (readString == "agent.ping") {             byte responseBytes [] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, '1'};              client.write(responseBytes, 9);           } else            if (readString == "env.temp") {             ds.reset();             ds.select(addr);             ds.write(0x44);  // start conversion with regular (non-parasite!) power             delay(1000);               ds.reset();             ds.select(addr);             ds.write(0xBE);  // read Scratchpad              data[0] = ds.read();             data[1] = ds.read();              int16_t raw = (data[1] << 8) | data[0];             celsius = (float)raw / 16.0;              byte responseBytes [] = {(byte) String(celsius).length(), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};             client.write(responseBytes, 8);             client.print(celsius);           }           else {             byte responseBytes [] = {0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};             client.write(responseBytes, 8);             client.print("ZBX_NOTSUPPORTED");           }           break;         }         else if (readString.length() < 20) {           readString = readString + c;         }       }     }     delay(10);     client.stop();   } }
    (на местном форуме странные теги, куда подевались переносы строк в коде - не знаю, самое важное в картинке)
    Click image for larger version

Name:	code.png
Views:	318
Size:	87.1 KB
ID:	379421

    Там самая для меня интересная строка вот эта:
    if (c == '\n') // end of query from zabbix server

    Она позволяла обрабатывать запросы от сервера Zabbix версии 3.2. Т.е. как только встречался символ переноса строки, агент понимался что это конец запроса и нужно давать ответ.
    Тут описано что Server request для zabbix 3.2 имеет вид:
    <item key>\n А вот тут написано что для zabbix 4.0 Server request имеет другой вид:
    <item key> т.е. потерялся перенос строки. И как теперь переписать код для агента не могу понять. Подскажите, пожалуйста.
    Last edited by katigoroshek; 24-05-2019, 10:35.
  • Kos
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Aug 2015
    • 3404

    #2
    Начиная с версии 4.0, перестал поддерживаться старый протокол - по сути, просто текст с указанием имени метрики и переводом строки. Теперь используется только новый, где в каждом запросе и ответе обязан присутствовать заголовок (обратите внимание - не только в ответе, но и в запросе тоже!). А уже в заголовке в явном виде указана длина сообщения, поэтому надобность в переводе строки отпала.

    Comment

    • revomix
      Junior Member
      • Jun 2019
      • 1

      #3
      добрый день, можете поделиться скетчем для zabbix4, или подсказать что и где изменить
      Last edited by revomix; 04-06-2019, 12:59.

      Comment

      • Hamardaban
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • May 2019
        • 2713

        #4
        есть описание протокола обмена https://www.zabbix.com/documentation.../activepassive .

        Comment

        • stensilart
          Junior Member
          • Oct 2019
          • 5

          #5
          Originally posted by revomix
          добрый день, можете поделиться скетчем для zabbix4, или подсказать что и где изменить
          Получилось что нибудь сделать?

          Comment


          • revomix
            revomix commented
            Editing a comment
            да, получилось

            #include <OneWire.h>
            #include <UIPEthernet.h>

            byte mac[] = { 0xDE, 0x05, 0xB6, 0x27, 0x39, 0x19 }; // random MAC
            byte ip[] = { 192, 168, 1, 10 }; // IP address in local network
            String readString = String(20);
            byte addr[8];

            OneWire ds(4); // DS18B20 at pin 4
            EthernetServer server(10050); // Zabbix port

            void setup() {
            Ethernet.begin(mac, ip);
            server.begin();
            ds.search(addr);
            }

            void loop() {
            byte data[2];
            float celsius;
            readString = "";
            if (EthernetClient client = server.available())
            {
            while (client.connected()) {
            if (client.available()) {
            char c = client.read();
            if (c == '\n') // end of query from zabbix server
            {
            if (readString == "ZBXD")
            {
            readString = "";
            continue;
            }
            Label:
            if (readString == "agent.ping" || readString == "ZBXDagent.ping") {
            client.print("ZBXD\x01"); // response header
            byte responseBytes [] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, '1'};
            client.write(responseBytes, 9);
            } else
            if (readString == "env.temp" || readString == "ZBXDenv.temp") {
            client.print("ZBXD\x01"); // response header
            ds.reset();
            ds.select(addr);
            ds.write(0x44); // start conversion with regular (non-parasite!) power
            delay(1000);
            ds.reset();
            ds.select(addr);
            ds.write(0xBE); // read Scratchpad
            data[0] = ds.read();
            data[1] = ds.read();
            int16_t raw = (data[1] << 8) | data[0];
            celsius = (float)raw / 16.0;
            byte responseBytes [] = {(byte) String(celsius).length(), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
            client.write(responseBytes, 8);
            client.print(celsius);
            } else {
            client.print("ZBXD\x01"); // response header
            byte responseBytes [] = {0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
            client.write(responseBytes, 8);
            client.print("ZBX_NOTSUPPORTED");
            }
            break;
            }
            else if ((c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c == '.') && readString.length() < 40) {
            readString = readString + c;
            continue;
            }
            else if (readString.length() < 23 ) {
            continue;
            }
            }
            else
            goto Label;
            }
            delay(10);
            client.stop();
            }
            }
        Working...