]> mj.ucw.cz Git - home-hw.git/blob - bsb/logger/burrow-bsb-logger.c
BSB logger: fixup
[home-hw.git] / bsb / logger / burrow-bsb-logger.c
1 /*
2  *      A BSB frame logger
3  *
4  *      (c) 2020 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <ucw/lib.h>
8 #include <ucw/fastbuf.h>
9 #include <ucw/log.h>
10 #include <ucw/opt.h>
11
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <syslog.h>
17 #include <time.h>
18 #include <unistd.h>
19
20 #include <curl/curl.h>
21 #include <mosquitto.h>
22
23 /*** MQTT ***/
24
25 static struct mosquitto *mosq;
26
27 static void mqtt_error(const char *operation, int err, bool teardown)
28 {
29         msg(L_ERROR, "Mosquitto: %s failed: error %d", operation, err);
30 }
31
32 static void mqtt_publish(const char *topic, const char *fmt, ...)
33 {
34         va_list args;
35         va_start(args, fmt);
36
37         char m[256];
38         int l = vsnprintf(m, sizeof(m), fmt, args);
39         int err = mosquitto_publish(mosq, NULL, topic, l, m, 0, true);
40         if (err != MOSQ_ERR_SUCCESS)
41                 mqtt_error("publish", err, false);
42
43         va_end(args);
44 }
45
46 static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message)
47 {
48         // msg(L_INFO, "MQTT(%d): %s", level, message);
49 }
50
51 static void mqtt_conn_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int status)
52 {
53         if (!status) {
54                 msg(L_DEBUG, "MQTT: Connection established, subscribing");
55                 int err = mosquitto_subscribe(mosq, NULL, "bsb/#", 1);
56                 if (err != MOSQ_ERR_SUCCESS) {
57                         mqtt_error("subscribe", err, false);
58                         return;
59                 }
60                 mqtt_publish("status/bsb-logger", "ok");
61         } else {
62                 msg(L_DEBUG, "MQTT: Cannot connect");
63         }
64 }
65
66 static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m);
67
68 static void mqtt_connect(void)
69 {
70         int err;
71
72         mosquitto_lib_init();
73
74         mosq = mosquitto_new("bsb-logger", 1, NULL);
75         if (!mosq)
76                 die("Mosquitto: initialization failed");
77
78         mosquitto_connect_callback_set(mosq, mqtt_conn_callback);
79         mosquitto_log_callback_set(mosq, mqtt_log_callback);
80         mosquitto_message_callback_set(mosq, mqtt_msg_callback);
81
82         if (mosquitto_will_set(mosq, "status/bsb-logger", 4, "dead", 0, true) != MOSQ_ERR_SUCCESS)
83                 die("Mosquitto: unable to set will");
84
85         if (mosquitto_tls_set(mosq, "/etc/burrow-mqtt/ca.crt", NULL, "/etc/burrow-mqtt/client.crt", "/etc/burrow-mqtt/client.key", NULL) != MOSQ_ERR_SUCCESS)
86                 die("Mosquitto: unable to set TLS parameters");
87
88         if (mosquitto_connect_async(mosq, "burrow-mqtt", 8883, 60) != MOSQ_ERR_SUCCESS)
89                 die("Mosquitto: connect failed");
90 }
91
92 static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m)
93 {
94         time_t now = time(NULL);
95         char val[256];
96         if (m->payloadlen >= sizeof(val) - 1) {
97                 msg(L_ERROR, "Invalid value for topic %s", m->topic);
98                 return;
99         }
100         memcpy(val, m->payload, m->payloadlen);
101         val[m->payloadlen] = 0;
102         msg(L_DEBUG, "MQTT < %s %s", m->topic, val);
103
104         static struct fastbuf *logfile;
105         if (!logfile)
106                 logfile = bopen_file("/var/log/bsb-frames", O_WRONLY | O_CREAT | O_APPEND, NULL);
107
108         if (!strcmp(m->topic, "bsb/frame"))
109                 bprintf(logfile, "%jd %s\n", (uintmax_t) time(NULL), val);
110
111         // FIXME: Log statistics
112
113         bflush(logfile);
114 }
115
116 /*** Main ***/
117
118 static int use_daemon;
119 static int use_debug;
120
121 static struct opt_section options = {
122         OPT_ITEMS {
123                 OPT_HELP("A daemon for logging BSB frames received via MQTT"),
124                 OPT_HELP(""),
125                 OPT_HELP("Options:"),
126                 OPT_BOOL('d', "debug", use_debug, 0, "\tLog debugging messages"),
127                 OPT_BOOL(0, "daemon", use_daemon, 0, "\tDaemonize"),
128                 OPT_HELP_OPTION,
129                 OPT_CONF_OPTIONS,
130                 OPT_END
131         }
132 };
133
134 int main(int argc UNUSED, char **argv)
135 {
136         log_init(argv[0]);
137         opt_parse(&options, argv+1);
138
139         if (use_daemon) {
140                 struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
141                 log_set_default_stream(ls);
142         }
143         if (!use_debug)
144                 log_default_stream()->levels &= ~(1U << L_DEBUG);
145
146         mqtt_connect();
147
148         int err = mosquitto_loop_forever(mosq, 10000, 1);
149         if (err != MOSQ_ERR_SUCCESS)
150                 mqtt_error("loop", err, false);
151
152         return 1;
153 }