-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor.cpp
60 lines (48 loc) · 1.31 KB
/
monitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "config.h"
#if MONITOR_ENABLED
#include "timing.h"
#include "ethernet.h"
static char monitor_packet[1024];
void monitor_flush() {
const char ip[4] = {MONITOR_IP_ADDRESS};
const char mac[6] = {MONITOR_MAC_ADDRESS};
size_t packet_size = strlen(monitor_packet);
if (packet_size == 0)
return;
ethernet_send_udp_packet(ip, mac, MONITOR_PORT, MONITOR_PORT, monitor_packet, packet_size);
monitor_packet[0] = '\0';
}
static void monitor_append(const char *buf) {
if (strlen(buf) + strlen(monitor_packet) >= sizeof(monitor_packet)) {
monitor_flush();
}
strcat(monitor_packet, buf);
}
void monitor_send(const char *metric, int value) {
char buf[512] = MONITOR_PREFIX;
strcat(buf, metric);
strcat(buf, " ");
itoa(value, buf + strlen(buf), 10);
strcat(buf, " ");
itoa(time_get_unix(), buf + strlen(buf), 10);
strcat(buf, "\n");
monitor_append(buf);
}
void monitor_sendf(const char *metric, float value) {
char buf[512] = MONITOR_PREFIX;
strcat(buf, metric);
strcat(buf, " ");
sprintf(buf + strlen(buf), "%f", value);
strcat(buf, " ");
itoa(time_get_unix(), buf + strlen(buf), 10);
strcat(buf, "\n");
monitor_append(buf);
}
#else
void monitor_send(const char *metric, int value) {
/* empty */
}
void monitor_sendf(const char *metric, float value) {
/* empty */
}
#endif