2 * collectd - src/mqtt.c
3 * Copyright (C) 2014 Marc Falzon
4 * Copyright (C) 2014,2015 Florian octo Forster
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Marc Falzon <marc at baha dot mu>
26 * Florian octo Forster <octo at collectd.org>
27 * Jan-Piet Mens <jpmens at gmail.com>
30 // Reference: http://mosquitto.org/api/files/mosquitto-h.html
36 #include "utils_complain.h"
38 #include <mosquitto.h>
40 #define MQTT_MAX_TOPIC_SIZE 1024
41 #define MQTT_MAX_MESSAGE_SIZE MQTT_MAX_TOPIC_SIZE + 1024
42 #define MQTT_DEFAULT_HOST "localhost"
43 #define MQTT_DEFAULT_PORT 1883
44 #define MQTT_DEFAULT_TOPIC_PREFIX "collectd"
45 #define MQTT_DEFAULT_TOPIC "collectd/#"
46 #ifndef MQTT_KEEPALIVE
47 #define MQTT_KEEPALIVE 60
49 #ifndef SSL_VERIFY_PEER
50 #define SSL_VERIFY_PEER 1
56 struct mqtt_client_conf {
60 struct mosquitto *mosq;
69 char *cacertificatefile;
70 char *certificatefile;
71 char *certificatekeyfile;
86 c_complain_t complaint_cantpublish;
89 typedef struct mqtt_client_conf mqtt_client_conf_t;
91 static mqtt_client_conf_t **subscribers = NULL;
92 static size_t subscribers_num = 0;
97 #if LIBMOSQUITTO_MAJOR == 0
98 static char const *mosquitto_strerror(int code) {
100 case MOSQ_ERR_SUCCESS:
101 return "MOSQ_ERR_SUCCESS";
103 return "MOSQ_ERR_NOMEM";
104 case MOSQ_ERR_PROTOCOL:
105 return "MOSQ_ERR_PROTOCOL";
107 return "MOSQ_ERR_INVAL";
108 case MOSQ_ERR_NO_CONN:
109 return "MOSQ_ERR_NO_CONN";
110 case MOSQ_ERR_CONN_REFUSED:
111 return "MOSQ_ERR_CONN_REFUSED";
112 case MOSQ_ERR_NOT_FOUND:
113 return "MOSQ_ERR_NOT_FOUND";
114 case MOSQ_ERR_CONN_LOST:
115 return "MOSQ_ERR_CONN_LOST";
117 return "MOSQ_ERR_SSL";
118 case MOSQ_ERR_PAYLOAD_SIZE:
119 return "MOSQ_ERR_PAYLOAD_SIZE";
120 case MOSQ_ERR_NOT_SUPPORTED:
121 return "MOSQ_ERR_NOT_SUPPORTED";
123 return "MOSQ_ERR_AUTH";
124 case MOSQ_ERR_ACL_DENIED:
125 return "MOSQ_ERR_ACL_DENIED";
126 case MOSQ_ERR_UNKNOWN:
127 return "MOSQ_ERR_UNKNOWN";
129 return "MOSQ_ERR_ERRNO";
132 return "UNKNOWN ERROR CODE";
135 /* provided by libmosquitto */
138 static void mqtt_free(mqtt_client_conf_t *conf) {
143 (void)mosquitto_disconnect(conf->mosq);
145 (void)mosquitto_destroy(conf->mosq);
148 sfree(conf->username);
149 sfree(conf->password);
150 sfree(conf->client_id);
151 sfree(conf->topic_prefix);
155 static char *strip_prefix(char *topic) {
158 for (size_t i = 0; topic[i] != 0; i++)
166 char *tmp = strchr(topic, '/');
176 static void on_message(
177 #if LIBMOSQUITTO_MAJOR == 0
179 __attribute__((unused)) struct mosquitto *m,
181 __attribute__((unused)) void *arg, const struct mosquitto_message *msg) {
182 value_list_t vl = VALUE_LIST_INIT;
183 data_set_t const *ds;
189 if (msg->payloadlen <= 0) {
190 DEBUG("mqtt plugin: message has empty payload");
194 topic = strdup(msg->topic);
195 name = strip_prefix(topic);
197 status = parse_identifier_vl(name, &vl);
199 ERROR("mqtt plugin: Unable to parse topic \"%s\".", topic);
205 ds = plugin_get_ds(vl.type);
207 ERROR("mqtt plugin: Unknown type: \"%s\".", vl.type);
211 vl.values = calloc(ds->ds_num, sizeof(*vl.values));
212 if (vl.values == NULL) {
213 ERROR("mqtt plugin: calloc failed.");
216 vl.values_len = ds->ds_num;
218 payload = malloc(msg->payloadlen + 1);
219 if (payload == NULL) {
220 ERROR("mqtt plugin: malloc for payload buffer failed.");
224 memmove(payload, msg->payload, msg->payloadlen);
225 payload[msg->payloadlen] = 0;
227 DEBUG("mqtt plugin: payload = \"%s\"", payload);
228 status = parse_values(payload, &vl, ds);
230 ERROR("mqtt plugin: Unable to parse payload \"%s\".", payload);
237 plugin_dispatch_values(&vl);
239 } /* void on_message */
241 /* must hold conf->lock when calling. */
242 static int mqtt_reconnect(mqtt_client_conf_t *conf) {
248 status = mosquitto_reconnect(conf->mosq);
249 if (status != MOSQ_ERR_SUCCESS) {
251 ERROR("mqtt_connect_broker: mosquitto_connect failed: %s",
252 (status == MOSQ_ERR_ERRNO) ? sstrerror(errno, errbuf, sizeof(errbuf))
253 : mosquitto_strerror(status));
259 c_release(LOG_INFO, &conf->complaint_cantpublish,
260 "mqtt plugin: successfully reconnected to broker \"%s:%d\"",
261 conf->host, conf->port);
264 } /* mqtt_reconnect */
266 /* must hold conf->lock when calling. */
267 static int mqtt_connect(mqtt_client_conf_t *conf) {
268 char const *client_id;
271 if (conf->mosq != NULL)
272 return mqtt_reconnect(conf);
275 client_id = conf->client_id;
277 client_id = hostname_g;
279 #if LIBMOSQUITTO_MAJOR == 0
280 conf->mosq = mosquitto_new(client_id, /* user data = */ conf);
283 mosquitto_new(client_id, conf->clean_session, /* user data = */ conf);
285 if (conf->mosq == NULL) {
286 ERROR("mqtt plugin: mosquitto_new failed");
290 #if LIBMOSQUITTO_MAJOR != 0
291 if (conf->cacertificatefile) {
292 status = mosquitto_tls_set(conf->mosq, conf->cacertificatefile, NULL,
293 conf->certificatefile, conf->certificatekeyfile,
294 /* pw_callback */ NULL);
295 if (status != MOSQ_ERR_SUCCESS) {
296 ERROR("mqtt plugin: cannot mosquitto_tls_set: %s",
297 mosquitto_strerror(status));
298 mosquitto_destroy(conf->mosq);
303 status = mosquitto_tls_opts_set(conf->mosq, SSL_VERIFY_PEER,
304 conf->tlsprotocol, conf->ciphersuite);
305 if (status != MOSQ_ERR_SUCCESS) {
306 ERROR("mqtt plugin: cannot mosquitto_tls_opts_set: %s",
307 mosquitto_strerror(status));
308 mosquitto_destroy(conf->mosq);
313 status = mosquitto_tls_insecure_set(conf->mosq, false);
314 if (status != MOSQ_ERR_SUCCESS) {
315 ERROR("mqtt plugin: cannot mosquitto_tls_insecure_set: %s",
316 mosquitto_strerror(status));
317 mosquitto_destroy(conf->mosq);
324 if (conf->username && conf->password) {
326 mosquitto_username_pw_set(conf->mosq, conf->username, conf->password);
327 if (status != MOSQ_ERR_SUCCESS) {
329 ERROR("mqtt plugin: mosquitto_username_pw_set failed: %s",
330 (status == MOSQ_ERR_ERRNO)
331 ? sstrerror(errno, errbuf, sizeof(errbuf))
332 : mosquitto_strerror(status));
334 mosquitto_destroy(conf->mosq);
340 #if LIBMOSQUITTO_MAJOR == 0
341 status = mosquitto_connect(conf->mosq, conf->host, conf->port,
342 /* keepalive = */ MQTT_KEEPALIVE,
343 /* clean session = */ conf->clean_session);
346 mosquitto_connect(conf->mosq, conf->host, conf->port, MQTT_KEEPALIVE);
348 if (status != MOSQ_ERR_SUCCESS) {
350 ERROR("mqtt plugin: mosquitto_connect failed: %s",
351 (status == MOSQ_ERR_ERRNO) ? sstrerror(errno, errbuf, sizeof(errbuf))
352 : mosquitto_strerror(status));
354 mosquitto_destroy(conf->mosq);
359 if (!conf->publish) {
360 mosquitto_message_callback_set(conf->mosq, on_message);
363 mosquitto_subscribe(conf->mosq,
364 /* message_id = */ NULL, conf->topic, conf->qos);
365 if (status != MOSQ_ERR_SUCCESS) {
366 ERROR("mqtt plugin: Subscribing to \"%s\" failed: %s", conf->topic,
367 mosquitto_strerror(status));
369 mosquitto_disconnect(conf->mosq);
370 mosquitto_destroy(conf->mosq);
380 static void *subscribers_thread(void *arg) {
381 mqtt_client_conf_t *conf = arg;
387 status = mqtt_connect(conf);
393 /* The documentation says "0" would map to the default (1000ms), but
394 * that does not work on some versions. */
395 #if LIBMOSQUITTO_MAJOR == 0
396 status = mosquitto_loop(conf->mosq, /* timeout = */ 1000 /* ms */);
398 status = mosquitto_loop(conf->mosq,
399 /* timeout[ms] = */ 1000,
400 /* max_packets = */ 100);
402 if (status == MOSQ_ERR_CONN_LOST) {
405 } else if (status != MOSQ_ERR_SUCCESS) {
406 ERROR("mqtt plugin: mosquitto_loop failed: %s",
407 mosquitto_strerror(status));
408 mosquitto_destroy(conf->mosq);
414 DEBUG("mqtt plugin: mosquitto_loop succeeded.");
415 } /* while (conf->loop) */
418 } /* void *subscribers_thread */
420 static int publish(mqtt_client_conf_t *conf, char const *topic,
421 void const *payload, size_t payload_len) {
424 pthread_mutex_lock(&conf->lock);
426 status = mqtt_connect(conf);
428 pthread_mutex_unlock(&conf->lock);
429 ERROR("mqtt plugin: unable to reconnect to broker");
433 status = mosquitto_publish(conf->mosq, /* message_id */ NULL, topic,
434 #if LIBMOSQUITTO_MAJOR == 0
435 (uint32_t)payload_len, payload,
437 (int)payload_len, payload,
439 conf->qos, conf->retain);
440 if (status != MOSQ_ERR_SUCCESS) {
442 c_complain(LOG_ERR, &conf->complaint_cantpublish,
443 "mqtt plugin: mosquitto_publish failed: %s",
444 (status == MOSQ_ERR_ERRNO)
445 ? sstrerror(errno, errbuf, sizeof(errbuf))
446 : mosquitto_strerror(status));
447 /* Mark our connection "down" regardless of the error as a safety
448 * measure; we will try to reconnect the next time we have to publish a
451 mosquitto_disconnect(conf->mosq);
453 pthread_mutex_unlock(&conf->lock);
457 pthread_mutex_unlock(&conf->lock);
461 static int format_topic(char *buf, size_t buf_len, data_set_t const *ds,
462 value_list_t const *vl, mqtt_client_conf_t *conf) {
463 char name[MQTT_MAX_TOPIC_SIZE];
466 if ((conf->topic_prefix == NULL) || (conf->topic_prefix[0] == 0))
467 return (FORMAT_VL(buf, buf_len, vl));
469 status = FORMAT_VL(name, sizeof(name), vl);
473 status = ssnprintf(buf, buf_len, "%s/%s", conf->topic_prefix, name);
474 if ((status < 0) || (((size_t)status) >= buf_len))
478 } /* int format_topic */
480 static int mqtt_write(const data_set_t *ds, const value_list_t *vl,
481 user_data_t *user_data) {
482 mqtt_client_conf_t *conf;
483 char topic[MQTT_MAX_TOPIC_SIZE];
484 char payload[MQTT_MAX_MESSAGE_SIZE];
487 if ((user_data == NULL) || (user_data->data == NULL))
489 conf = user_data->data;
491 status = format_topic(topic, sizeof(topic), ds, vl, conf);
493 ERROR("mqtt plugin: format_topic failed with status %d.", status);
497 status = format_values(payload, sizeof(payload), ds, vl, conf->store_rates);
499 ERROR("mqtt plugin: format_values failed with status %d.", status);
503 status = publish(conf, topic, payload, strlen(payload) + 1);
505 ERROR("mqtt plugin: publish failed: %s", mosquitto_strerror(status));
516 * ClientId "collectd"
523 * CACert "ca.pem" Enables TLS if set
524 * CertificateFile "client-cert.pem" optional
525 * CertificateKeyFile "client-key.pem" optional
526 * TLSProtocol "tlsv1.2" optional
529 static int mqtt_config_publisher(oconfig_item_t *ci) {
530 mqtt_client_conf_t *conf;
534 conf = calloc(1, sizeof(*conf));
536 ERROR("mqtt plugin: calloc failed.");
542 status = cf_util_get_string(ci, &conf->name);
548 conf->host = strdup(MQTT_DEFAULT_HOST);
549 conf->port = MQTT_DEFAULT_PORT;
550 conf->client_id = NULL;
552 conf->topic_prefix = strdup(MQTT_DEFAULT_TOPIC_PREFIX);
553 conf->store_rates = 1;
555 status = pthread_mutex_init(&conf->lock, NULL);
561 C_COMPLAIN_INIT(&conf->complaint_cantpublish);
563 for (int i = 0; i < ci->children_num; i++) {
564 oconfig_item_t *child = ci->children + i;
565 if (strcasecmp("Host", child->key) == 0)
566 cf_util_get_string(child, &conf->host);
567 else if (strcasecmp("Port", child->key) == 0) {
568 int tmp = cf_util_get_port_number(child);
570 ERROR("mqtt plugin: Invalid port number.");
573 } else if (strcasecmp("ClientId", child->key) == 0)
574 cf_util_get_string(child, &conf->client_id);
575 else if (strcasecmp("User", child->key) == 0)
576 cf_util_get_string(child, &conf->username);
577 else if (strcasecmp("Password", child->key) == 0)
578 cf_util_get_string(child, &conf->password);
579 else if (strcasecmp("QoS", child->key) == 0) {
581 status = cf_util_get_int(child, &tmp);
582 if ((status != 0) || (tmp < 0) || (tmp > 2))
583 ERROR("mqtt plugin: Not a valid QoS setting.");
586 } else if (strcasecmp("Prefix", child->key) == 0)
587 cf_util_get_string(child, &conf->topic_prefix);
588 else if (strcasecmp("StoreRates", child->key) == 0)
589 cf_util_get_boolean(child, &conf->store_rates);
590 else if (strcasecmp("Retain", child->key) == 0)
591 cf_util_get_boolean(child, &conf->retain);
592 else if (strcasecmp("CACert", child->key) == 0)
593 cf_util_get_string(child, &conf->cacertificatefile);
594 else if (strcasecmp("CertificateFile", child->key) == 0)
595 cf_util_get_string(child, &conf->certificatefile);
596 else if (strcasecmp("CertificateKeyFile", child->key) == 0)
597 cf_util_get_string(child, &conf->certificatekeyfile);
598 else if (strcasecmp("TLSProtocol", child->key) == 0)
599 cf_util_get_string(child, &conf->tlsprotocol);
600 else if (strcasecmp("CipherSuite", child->key) == 0)
601 cf_util_get_string(child, &conf->ciphersuite);
603 ERROR("mqtt plugin: Unknown config option: %s", child->key);
606 ssnprintf(cb_name, sizeof(cb_name), "mqtt/%s", conf->name);
607 user_data_t user_data = {.data = conf};
609 plugin_register_write(cb_name, mqtt_write, &user_data);
611 } /* mqtt_config_publisher */
617 * ClientId "collectd"
623 static int mqtt_config_subscriber(oconfig_item_t *ci) {
624 mqtt_client_conf_t **tmp;
625 mqtt_client_conf_t *conf;
628 conf = calloc(1, sizeof(*conf));
630 ERROR("mqtt plugin: calloc failed.");
636 status = cf_util_get_string(ci, &conf->name);
642 conf->host = strdup(MQTT_DEFAULT_HOST);
643 conf->port = MQTT_DEFAULT_PORT;
644 conf->client_id = NULL;
646 conf->topic = strdup(MQTT_DEFAULT_TOPIC);
647 conf->clean_session = 1;
649 status = pthread_mutex_init(&conf->lock, NULL);
655 C_COMPLAIN_INIT(&conf->complaint_cantpublish);
657 for (int i = 0; i < ci->children_num; i++) {
658 oconfig_item_t *child = ci->children + i;
659 if (strcasecmp("Host", child->key) == 0)
660 cf_util_get_string(child, &conf->host);
661 else if (strcasecmp("Port", child->key) == 0) {
662 status = cf_util_get_port_number(child);
664 ERROR("mqtt plugin: Invalid port number.");
667 } else if (strcasecmp("ClientId", child->key) == 0)
668 cf_util_get_string(child, &conf->client_id);
669 else if (strcasecmp("User", child->key) == 0)
670 cf_util_get_string(child, &conf->username);
671 else if (strcasecmp("Password", child->key) == 0)
672 cf_util_get_string(child, &conf->password);
673 else if (strcasecmp("QoS", child->key) == 0) {
675 status = cf_util_get_int(child, &qos);
676 if ((status != 0) || (qos < 0) || (qos > 2))
677 ERROR("mqtt plugin: Not a valid QoS setting.");
680 } else if (strcasecmp("Topic", child->key) == 0)
681 cf_util_get_string(child, &conf->topic);
682 else if (strcasecmp("CleanSession", child->key) == 0)
683 cf_util_get_boolean(child, &conf->clean_session);
685 ERROR("mqtt plugin: Unknown config option: %s", child->key);
688 tmp = realloc(subscribers, sizeof(*subscribers) * (subscribers_num + 1));
690 ERROR("mqtt plugin: realloc failed.");
695 subscribers[subscribers_num] = conf;
699 } /* mqtt_config_subscriber */
711 static int mqtt_config(oconfig_item_t *ci) {
712 for (int i = 0; i < ci->children_num; i++) {
713 oconfig_item_t *child = ci->children + i;
715 if (strcasecmp("Publish", child->key) == 0)
716 mqtt_config_publisher(child);
717 else if (strcasecmp("Subscribe", child->key) == 0)
718 mqtt_config_subscriber(child);
720 ERROR("mqtt plugin: Unknown config option: %s", child->key);
724 } /* int mqtt_config */
726 static int mqtt_init(void) {
727 mosquitto_lib_init();
729 for (size_t i = 0; i < subscribers_num; i++) {
732 if (subscribers[i]->loop)
735 status = plugin_thread_create(&subscribers[i]->thread,
737 /* func = */ subscribers_thread,
738 /* args = */ subscribers[i]);
741 ERROR("mqtt plugin: pthread_create failed: %s",
742 sstrerror(errno, errbuf, sizeof(errbuf)));
750 void module_register(void) {
751 plugin_register_complex_config("mqtt", mqtt_config);
752 plugin_register_init("mqtt", mqtt_init);
753 } /* void module_register */
755 /* vim: set sw=4 sts=4 et fdm=marker : */