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_cache.h"
37 #include "utils_complain.h"
41 #include <mosquitto.h>
43 #define MQTT_MAX_TOPIC_SIZE 1024
44 #define MQTT_MAX_MESSAGE_SIZE MQTT_MAX_TOPIC_SIZE + 1024
45 #define MQTT_DEFAULT_HOST "localhost"
46 #define MQTT_DEFAULT_PORT 1883
47 #define MQTT_DEFAULT_TOPIC_PREFIX "collectd"
48 #define MQTT_DEFAULT_TOPIC "collectd/#"
49 #ifndef MQTT_KEEPALIVE
50 # define MQTT_KEEPALIVE 60
52 #ifndef SSL_VERIFY_PEER
53 # define SSL_VERIFY_PEER 1
60 struct mqtt_client_conf
65 struct mosquitto *mosq;
74 char *cacertificatefile;
75 char *certificatefile;
76 char *certificatekeyfile;
91 c_complain_t complaint_cantpublish;
94 typedef struct mqtt_client_conf mqtt_client_conf_t;
96 static mqtt_client_conf_t **subscribers = NULL;
97 static size_t subscribers_num = 0;
102 #if LIBMOSQUITTO_MAJOR == 0
103 static char const *mosquitto_strerror (int code)
107 case MOSQ_ERR_SUCCESS: return "MOSQ_ERR_SUCCESS";
108 case MOSQ_ERR_NOMEM: return "MOSQ_ERR_NOMEM";
109 case MOSQ_ERR_PROTOCOL: return "MOSQ_ERR_PROTOCOL";
110 case MOSQ_ERR_INVAL: return "MOSQ_ERR_INVAL";
111 case MOSQ_ERR_NO_CONN: return "MOSQ_ERR_NO_CONN";
112 case MOSQ_ERR_CONN_REFUSED: return "MOSQ_ERR_CONN_REFUSED";
113 case MOSQ_ERR_NOT_FOUND: return "MOSQ_ERR_NOT_FOUND";
114 case MOSQ_ERR_CONN_LOST: return "MOSQ_ERR_CONN_LOST";
115 case MOSQ_ERR_SSL: return "MOSQ_ERR_SSL";
116 case MOSQ_ERR_PAYLOAD_SIZE: return "MOSQ_ERR_PAYLOAD_SIZE";
117 case MOSQ_ERR_NOT_SUPPORTED: return "MOSQ_ERR_NOT_SUPPORTED";
118 case MOSQ_ERR_AUTH: return "MOSQ_ERR_AUTH";
119 case MOSQ_ERR_ACL_DENIED: return "MOSQ_ERR_ACL_DENIED";
120 case MOSQ_ERR_UNKNOWN: return "MOSQ_ERR_UNKNOWN";
121 case MOSQ_ERR_ERRNO: return "MOSQ_ERR_ERRNO";
124 return "UNKNOWN ERROR CODE";
127 /* provided by libmosquitto */
130 static void mqtt_free (mqtt_client_conf_t *conf)
136 (void) mosquitto_disconnect (conf->mosq);
138 (void) mosquitto_destroy (conf->mosq);
141 sfree (conf->username);
142 sfree (conf->password);
143 sfree (conf->client_id);
144 sfree (conf->topic_prefix);
148 static char *strip_prefix (char *topic)
154 for (i = 0; topic[i] != 0; i++)
163 char *tmp = strchr (topic, '/');
173 static void on_message (
174 #if LIBMOSQUITTO_MAJOR == 0
176 __attribute__((unused)) struct mosquitto *m,
178 __attribute__((unused)) void *arg,
179 const struct mosquitto_message *msg)
181 value_list_t vl = VALUE_LIST_INIT;
182 data_set_t const *ds;
188 if (msg->payloadlen <= 0) {
189 DEBUG ("mqtt plugin: message has empty payload");
193 topic = strdup (msg->topic);
194 name = strip_prefix (topic);
196 status = parse_identifier_vl (name, &vl);
199 ERROR ("mqtt plugin: Unable to parse topic \"%s\".", topic);
205 ds = plugin_get_ds (vl.type);
208 ERROR ("mqtt plugin: Unknown type: \"%s\".", vl.type);
212 vl.values = calloc (ds->ds_num, sizeof (*vl.values));
213 if (vl.values == NULL)
215 ERROR ("mqtt plugin: calloc failed.");
218 vl.values_len = ds->ds_num;
220 payload = malloc (msg->payloadlen+1);
223 ERROR ("mqtt plugin: malloc for payload buffer failed.");
226 memmove (payload, msg->payload, msg->payloadlen);
227 payload[msg->payloadlen] = 0;
229 DEBUG ("mqtt plugin: payload = \"%s\"", payload);
230 status = parse_values (payload, &vl, ds);
233 ERROR ("mqtt plugin: Unable to parse payload \"%s\".", payload);
240 plugin_dispatch_values (&vl);
242 } /* void on_message */
244 /* must hold conf->lock when calling. */
245 static int mqtt_reconnect (mqtt_client_conf_t *conf)
252 status = mosquitto_reconnect (conf->mosq);
253 if (status != MOSQ_ERR_SUCCESS)
256 ERROR ("mqtt_connect_broker: mosquitto_connect failed: %s",
257 (status == MOSQ_ERR_ERRNO)
258 ? sstrerror(errno, errbuf, sizeof (errbuf))
259 : mosquitto_strerror (status));
266 &conf->complaint_cantpublish,
267 "mqtt plugin: successfully reconnected to broker \"%s:%d\"",
268 conf->host, conf->port);
271 } /* mqtt_reconnect */
273 /* must hold conf->lock when calling. */
274 static int mqtt_connect (mqtt_client_conf_t *conf)
276 char const *client_id;
279 if (conf->mosq != NULL)
280 return mqtt_reconnect (conf);
283 client_id = conf->client_id;
285 client_id = hostname_g;
287 #if LIBMOSQUITTO_MAJOR == 0
288 conf->mosq = mosquitto_new (client_id, /* user data = */ conf);
290 conf->mosq = mosquitto_new (client_id, conf->clean_session, /* user data = */ conf);
292 if (conf->mosq == NULL)
294 ERROR ("mqtt plugin: mosquitto_new failed");
298 #if LIBMOSQUITTO_MAJOR != 0
299 if (conf->cacertificatefile) {
300 status = mosquitto_tls_set(conf->mosq, conf->cacertificatefile, NULL,
301 conf->certificatefile, conf->certificatekeyfile, /* pw_callback */NULL);
302 if (status != MOSQ_ERR_SUCCESS) {
303 ERROR ("mqtt plugin: cannot mosquitto_tls_set: %s", mosquitto_strerror(status));
304 mosquitto_destroy (conf->mosq);
309 status = mosquitto_tls_opts_set(conf->mosq, SSL_VERIFY_PEER, conf->tlsprotocol, conf->ciphersuite);
310 if (status != MOSQ_ERR_SUCCESS) {
311 ERROR ("mqtt plugin: cannot mosquitto_tls_opts_set: %s", mosquitto_strerror(status));
312 mosquitto_destroy (conf->mosq);
317 status = mosquitto_tls_insecure_set(conf->mosq, false);
318 if (status != MOSQ_ERR_SUCCESS) {
319 ERROR ("mqtt plugin: cannot mosquitto_tls_insecure_set: %s", mosquitto_strerror(status));
320 mosquitto_destroy (conf->mosq);
327 if (conf->username && conf->password)
329 status = mosquitto_username_pw_set (conf->mosq, conf->username, conf->password);
330 if (status != MOSQ_ERR_SUCCESS)
333 ERROR ("mqtt plugin: mosquitto_username_pw_set failed: %s",
334 (status == MOSQ_ERR_ERRNO)
335 ? sstrerror (errno, errbuf, sizeof (errbuf))
336 : mosquitto_strerror (status));
338 mosquitto_destroy (conf->mosq);
344 #if LIBMOSQUITTO_MAJOR == 0
345 status = mosquitto_connect (conf->mosq, conf->host, conf->port,
346 /* keepalive = */ MQTT_KEEPALIVE, /* clean session = */ conf->clean_session);
348 status = mosquitto_connect (conf->mosq, conf->host, conf->port, MQTT_KEEPALIVE);
350 if (status != MOSQ_ERR_SUCCESS)
353 ERROR ("mqtt plugin: mosquitto_connect failed: %s",
354 (status == MOSQ_ERR_ERRNO)
355 ? sstrerror (errno, errbuf, sizeof (errbuf))
356 : mosquitto_strerror (status));
358 mosquitto_destroy (conf->mosq);
365 mosquitto_message_callback_set (conf->mosq, on_message);
367 status = mosquitto_subscribe (conf->mosq,
368 /* message_id = */ NULL,
369 conf->topic, conf->qos);
370 if (status != MOSQ_ERR_SUCCESS)
372 ERROR ("mqtt plugin: Subscribing to \"%s\" failed: %s",
373 conf->topic, mosquitto_strerror (status));
375 mosquitto_disconnect (conf->mosq);
376 mosquitto_destroy (conf->mosq);
386 static void *subscribers_thread (void *arg)
388 mqtt_client_conf_t *conf = arg;
395 status = mqtt_connect (conf);
402 /* The documentation says "0" would map to the default (1000ms), but
403 * that does not work on some versions. */
404 #if LIBMOSQUITTO_MAJOR == 0
405 status = mosquitto_loop (conf->mosq, /* timeout = */ 1000 /* ms */);
407 status = mosquitto_loop (conf->mosq,
408 /* timeout[ms] = */ 1000,
409 /* max_packets = */ 100);
411 if (status == MOSQ_ERR_CONN_LOST)
416 else if (status != MOSQ_ERR_SUCCESS)
418 ERROR ("mqtt plugin: mosquitto_loop failed: %s",
419 mosquitto_strerror (status));
420 mosquitto_destroy (conf->mosq);
426 DEBUG ("mqtt plugin: mosquitto_loop succeeded.");
427 } /* while (conf->loop) */
430 } /* void *subscribers_thread */
432 static int publish (mqtt_client_conf_t *conf, char const *topic,
433 void const *payload, size_t payload_len)
437 pthread_mutex_lock (&conf->lock);
439 status = mqtt_connect (conf);
441 pthread_mutex_unlock (&conf->lock);
442 ERROR ("mqtt plugin: unable to reconnect to broker");
446 status = mosquitto_publish(conf->mosq, /* message_id */ NULL, topic,
447 #if LIBMOSQUITTO_MAJOR == 0
448 (uint32_t) payload_len, payload,
450 (int) payload_len, payload,
452 conf->qos, conf->retain);
453 if (status != MOSQ_ERR_SUCCESS)
457 &conf->complaint_cantpublish,
458 "mqtt plugin: mosquitto_publish failed: %s",
459 (status == MOSQ_ERR_ERRNO)
460 ? sstrerror(errno, errbuf, sizeof (errbuf))
461 : mosquitto_strerror(status));
462 /* Mark our connection "down" regardless of the error as a safety
463 * measure; we will try to reconnect the next time we have to publish a
467 pthread_mutex_unlock (&conf->lock);
471 pthread_mutex_unlock (&conf->lock);
475 static int format_topic (char *buf, size_t buf_len,
476 data_set_t const *ds, value_list_t const *vl,
477 mqtt_client_conf_t *conf)
479 char name[MQTT_MAX_TOPIC_SIZE];
482 if ((conf->topic_prefix == NULL) || (conf->topic_prefix[0] == 0))
483 return (FORMAT_VL (buf, buf_len, vl));
485 status = FORMAT_VL (name, sizeof (name), vl);
489 status = ssnprintf (buf, buf_len, "%s/%s", conf->topic_prefix, name);
490 if ((status < 0) || (((size_t) status) >= buf_len))
494 } /* int format_topic */
496 static int mqtt_write (const data_set_t *ds, const value_list_t *vl,
497 user_data_t *user_data)
499 mqtt_client_conf_t *conf;
500 char topic[MQTT_MAX_TOPIC_SIZE];
501 char payload[MQTT_MAX_MESSAGE_SIZE];
504 if ((user_data == NULL) || (user_data->data == NULL))
506 conf = user_data->data;
508 status = format_topic (topic, sizeof (topic), ds, vl, conf);
511 ERROR ("mqtt plugin: format_topic failed with status %d.", status);
515 status = format_values (payload, sizeof (payload),
516 ds, vl, conf->store_rates);
519 ERROR ("mqtt plugin: format_values failed with status %d.", status);
523 status = publish (conf, topic, payload, strlen (payload) + 1);
526 ERROR ("mqtt plugin: publish failed: %s", mosquitto_strerror (status));
537 * ClientId "collectd"
544 * CACert "ca.pem" Enables TLS if set
545 * CertificateFile "client-cert.pem" optional
546 * CertificateKeyFile "client-key.pem" optional
547 * TLSProtocol "tlsv1.2" optional
550 static int mqtt_config_publisher (oconfig_item_t *ci)
552 mqtt_client_conf_t *conf;
554 user_data_t user_data;
558 conf = calloc (1, sizeof (*conf));
561 ERROR ("mqtt plugin: malloc failed.");
567 status = cf_util_get_string (ci, &conf->name);
574 conf->host = strdup (MQTT_DEFAULT_HOST);
575 conf->port = MQTT_DEFAULT_PORT;
576 conf->client_id = NULL;
578 conf->topic_prefix = strdup (MQTT_DEFAULT_TOPIC_PREFIX);
579 conf->store_rates = 1;
581 status = pthread_mutex_init (&conf->lock, NULL);
588 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
590 for (i = 0; i < ci->children_num; i++)
592 oconfig_item_t *child = ci->children + i;
593 if (strcasecmp ("Host", child->key) == 0)
594 cf_util_get_string (child, &conf->host);
595 else if (strcasecmp ("Port", child->key) == 0)
597 int tmp = cf_util_get_port_number (child);
599 ERROR ("mqtt plugin: Invalid port number.");
603 else if (strcasecmp ("ClientId", child->key) == 0)
604 cf_util_get_string (child, &conf->client_id);
605 else if (strcasecmp ("User", child->key) == 0)
606 cf_util_get_string (child, &conf->username);
607 else if (strcasecmp ("Password", child->key) == 0)
608 cf_util_get_string (child, &conf->password);
609 else if (strcasecmp ("QoS", child->key) == 0)
612 status = cf_util_get_int (child, &tmp);
613 if ((status != 0) || (tmp < 0) || (tmp > 2))
614 ERROR ("mqtt plugin: Not a valid QoS setting.");
618 else if (strcasecmp ("Prefix", child->key) == 0)
619 cf_util_get_string (child, &conf->topic_prefix);
620 else if (strcasecmp ("StoreRates", child->key) == 0)
621 cf_util_get_boolean (child, &conf->store_rates);
622 else if (strcasecmp ("Retain", child->key) == 0)
623 cf_util_get_boolean (child, &conf->retain);
624 else if (strcasecmp ("CACert", child->key) == 0)
625 cf_util_get_string (child, &conf->cacertificatefile);
626 else if (strcasecmp ("CertificateFile", child->key) == 0)
627 cf_util_get_string (child, &conf->certificatefile);
628 else if (strcasecmp ("CertificateKeyFile", child->key) == 0)
629 cf_util_get_string (child, &conf->certificatekeyfile);
630 else if (strcasecmp ("TLSProtocol", child->key) == 0)
631 cf_util_get_string (child, &conf->tlsprotocol);
632 else if (strcasecmp ("CipherSuite", child->key) == 0)
633 cf_util_get_string (child, &conf->ciphersuite);
635 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
638 ssnprintf (cb_name, sizeof (cb_name), "mqtt/%s", conf->name);
639 memset (&user_data, 0, sizeof (user_data));
640 user_data.data = conf;
642 plugin_register_write (cb_name, mqtt_write, &user_data);
644 } /* mqtt_config_publisher */
650 * ClientId "collectd"
656 static int mqtt_config_subscriber (oconfig_item_t *ci)
658 mqtt_client_conf_t **tmp;
659 mqtt_client_conf_t *conf;
663 conf = calloc (1, sizeof (*conf));
666 ERROR ("mqtt plugin: malloc failed.");
672 status = cf_util_get_string (ci, &conf->name);
679 conf->host = strdup (MQTT_DEFAULT_HOST);
680 conf->port = MQTT_DEFAULT_PORT;
681 conf->client_id = NULL;
683 conf->topic = strdup (MQTT_DEFAULT_TOPIC);
684 conf->clean_session = 1;
686 status = pthread_mutex_init (&conf->lock, NULL);
693 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
695 for (i = 0; i < ci->children_num; i++)
697 oconfig_item_t *child = ci->children + i;
698 if (strcasecmp ("Host", child->key) == 0)
699 cf_util_get_string (child, &conf->host);
700 else if (strcasecmp ("Port", child->key) == 0)
702 int tmp = cf_util_get_port_number (child);
704 ERROR ("mqtt plugin: Invalid port number.");
708 else if (strcasecmp ("ClientId", child->key) == 0)
709 cf_util_get_string (child, &conf->client_id);
710 else if (strcasecmp ("User", child->key) == 0)
711 cf_util_get_string (child, &conf->username);
712 else if (strcasecmp ("Password", child->key) == 0)
713 cf_util_get_string (child, &conf->password);
714 else if (strcasecmp ("QoS", child->key) == 0)
717 status = cf_util_get_int (child, &tmp);
718 if ((status != 0) || (tmp < 0) || (tmp > 2))
719 ERROR ("mqtt plugin: Not a valid QoS setting.");
723 else if (strcasecmp ("Topic", child->key) == 0)
724 cf_util_get_string (child, &conf->topic);
725 else if (strcasecmp ("CleanSession", child->key) == 0)
726 cf_util_get_boolean (child, &conf->clean_session);
728 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
731 tmp = realloc (subscribers, sizeof (*subscribers) * (subscribers_num + 1) );
734 ERROR ("mqtt plugin: realloc failed.");
739 subscribers[subscribers_num] = conf;
743 } /* mqtt_config_subscriber */
755 static int mqtt_config (oconfig_item_t *ci)
759 for (i = 0; i < ci->children_num; i++)
761 oconfig_item_t *child = ci->children + i;
763 if (strcasecmp ("Publish", child->key) == 0)
764 mqtt_config_publisher (child);
765 else if (strcasecmp ("Subscribe", child->key) == 0)
766 mqtt_config_subscriber (child);
768 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
772 } /* int mqtt_config */
774 static int mqtt_init (void)
778 mosquitto_lib_init ();
780 for (i = 0; i < subscribers_num; i++)
784 if (subscribers[i]->loop)
787 status = plugin_thread_create (&subscribers[i]->thread,
789 /* func = */ subscribers_thread,
790 /* args = */ subscribers[i]);
794 ERROR ("mqtt plugin: pthread_create failed: %s",
795 sstrerror (errno, errbuf, sizeof (errbuf)));
803 void module_register (void)
805 plugin_register_complex_config ("mqtt", mqtt_config);
806 plugin_register_init ("mqtt", mqtt_init);
807 } /* void module_register */
809 /* vim: set sw=4 sts=4 et fdm=marker : */