· 5 years ago · Dec 08, 2020, 09:04 PM
1/*
2
3Copyright 2017 Lubos Medovarsky
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
81. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
112. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
153. Neither the name of the copyright holder nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30*/
31
32/*
33
34The application sends increasing counter value to Zabbix in the form of traps.
35To use in a meaningful manner, fire up your Zabbix UI or API, and create a
36hostname "arduino", add an item with key "counter" of type "Zabbix trapper",
37with type "Numeric".
38
39*/
40
41#include <Arduino.h>
42#include <SPI.h>
43#include <Ethernet.h>
44#include "config.hpp"
45
46int counter;
47
48byte mac[] = { MAC1, MAC2, MAC3, MAC4, MAC5, MAC6 };
49IPAddress zabbix(ZABBIX_IP1, ZABBIX_IP2, ZABBIX_IP3, ZABBIX_IP4);
50int numeroDeVariaveis = 2;
51int zabbixPort = 10051;
52
53void zabbix_send_trap(void);
54
55void setup() {
56 Serial.begin(9600);
57 Serial.println("iBoard 1.1");
58 Serial.println("Obtaining IP address using DHCP...");
59 while (Ethernet.begin(mac) != 1) {
60 delay(10);
61 }
62 counter = 1;
63 Serial.println("setup done.");
64}
65
66void loop() {
67 zabbix_send_trap();
68 delay(60000);
69 counter++;
70}
71
72const char PROGMEM content_header[] = "{\"request\":\"sender data\",\"data\":[";
73const char PROGMEM content_item[] = "{\"host\":\"%s\",\"key\":\"%s\",\"value\":\"%d\"}";
74const char PROGMEM content_footer[] = "]}";
75
76char host[] = "ethernet"; // hostname zabbix
77char key[][11] = {"variavel1","variavel2"}; // item zabbix
78
79unsigned int prepare_content(char *dst, char keyAtual[]) {
80 unsigned int len = 0;
81
82 memcpy(&dst[len], content_header, sizeof(content_header));
83 len += sizeof(content_header) - 1;
84 len += sprintf_P(&dst[len], content_item, host, keyAtual, counter);
85 memcpy(&dst[len], content_footer, sizeof(content_footer));
86 len += sizeof(content_footer) - 1;
87 return len;
88}
89
90EthernetClient zbx_client;
91char packet_header[] = "ZBXD\1"; //followed by unsigned long long content_len
92unsigned long long content_len;
93unsigned int payload_len;
94char packet_content[256];
95
96void zabbix_send_trap() {
97 for(int i=0; i<numeroDeVariaveis;i++){
98 content_len = prepare_content(packet_content, key[i]);
99 payload_len = sizeof(content_header) + content_len + sizeof(content_footer);
100 if (zbx_client.connect(zabbix, zabbixPort)) {
101 Serial.println("connected to zabbix");
102 Serial.println("sending data");
103 zbx_client.write(packet_header, sizeof(packet_header) - 1);
104 zbx_client.write((char *)&content_len, sizeof(content_len));
105 zbx_client.write(packet_content, content_len);
106 delay(1);
107 zbx_client.stop();
108 Serial.println("disconnected");
109 }
110 }
111}
112