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
37 #include "utils_complain.h"
39 #include <mosquitto.h>
41 #define MQTT_MAX_TOPIC_SIZE 1024
42 #define MQTT_MAX_MESSAGE_SIZE MQTT_MAX_TOPIC_SIZE + 1024
43 #define MQTT_DEFAULT_HOST "localhost"
44 #define MQTT_DEFAULT_PORT 1883
45 #define MQTT_DEFAULT_TOPIC_PREFIX "collectd"
46 #define MQTT_DEFAULT_TOPIC "collectd/#"
47 #ifndef MQTT_KEEPALIVE
48 # define MQTT_KEEPALIVE 60
50 #ifndef SSL_VERIFY_PEER
51 # define SSL_VERIFY_PEER 1
58 struct mqtt_client_conf
63 struct mosquitto *mosq;
72 char *cacertificatefile;
73 char *certificatefile;
74 char *certificatekeyfile;
89 c_complain_t complaint_cantpublish;
92 typedef struct mqtt_client_conf mqtt_client_conf_t;
94 static mqtt_client_conf_t **subscribers = NULL;
95 static size_t subscribers_num = 0;
100 #if LIBMOSQUITTO_MAJOR == 0
101 static char const *mosquitto_strerror (int code)
105 case MOSQ_ERR_SUCCESS: return "MOSQ_ERR_SUCCESS";
106 case MOSQ_ERR_NOMEM: return "MOSQ_ERR_NOMEM";
107 case MOSQ_ERR_PROTOCOL: return "MOSQ_ERR_PROTOCOL";
108 case MOSQ_ERR_INVAL: return "MOSQ_ERR_INVAL";
109 case MOSQ_ERR_NO_CONN: return "MOSQ_ERR_NO_CONN";
110 case MOSQ_ERR_CONN_REFUSED: return "MOSQ_ERR_CONN_REFUSED";
111 case MOSQ_ERR_NOT_FOUND: return "MOSQ_ERR_NOT_FOUND";
112 case MOSQ_ERR_CONN_LOST: return "MOSQ_ERR_CONN_LOST";
113 case MOSQ_ERR_SSL: return "MOSQ_ERR_SSL";
114 case MOSQ_ERR_PAYLOAD_SIZE: return "MOSQ_ERR_PAYLOAD_SIZE";
115 case MOSQ_ERR_NOT_SUPPORTED: return "MOSQ_ERR_NOT_SUPPORTED";
116 case MOSQ_ERR_AUTH: return "MOSQ_ERR_AUTH";
117 case MOSQ_ERR_ACL_DENIED: return "MOSQ_ERR_ACL_DENIED";
118 case MOSQ_ERR_UNKNOWN: return "MOSQ_ERR_UNKNOWN";
119 case MOSQ_ERR_ERRNO: return "MOSQ_ERR_ERRNO";
122 return "UNKNOWN ERROR CODE";
125 /* provided by libmosquitto */
128 static void mqtt_free (mqtt_client_conf_t *conf)
134 (void) mosquitto_disconnect (conf->mosq);
136 (void) mosquitto_destroy (conf->mosq);
139 sfree (conf->username);
140 sfree (conf->password);
141 sfree (conf->client_id);
142 sfree (conf->topic_prefix);
146 static char *strip_prefix (char *topic)
150 for (size_t i = 0; topic[i] != 0; i++)
159 char *tmp = strchr (topic, '/');
169 static void on_message (
170 #if LIBMOSQUITTO_MAJOR == 0
172 __attribute__((unused)) struct mosquitto *m,
174 __attribute__((unused)) void *arg,
175 const struct mosquitto_message *msg)
177 value_list_t vl = VALUE_LIST_INIT;
178 data_set_t const *ds;
184 if (msg->payloadlen <= 0) {
185 DEBUG ("mqtt plugin: message has empty payload");
189 topic = strdup (msg->topic);
190 name = strip_prefix (topic);
192 status = parse_identifier_vl (name, &vl);
195 ERROR ("mqtt plugin: Unable to parse topic \"%s\".", topic);
201 ds = plugin_get_ds (vl.type);
204 ERROR ("mqtt plugin: Unknown type: \"%s\".", vl.type);
208 vl.values = calloc (ds->ds_num, sizeof (*vl.values));
209 if (vl.values == NULL)
211 ERROR ("mqtt plugin: calloc failed.");
214 vl.values_len = ds->ds_num;
216 payload = malloc (msg->payloadlen+1);
219 ERROR ("mqtt plugin: malloc for payload buffer failed.");
223 memmove (payload, msg->payload, msg->payloadlen);
224 payload[msg->payloadlen] = 0;
226 DEBUG ("mqtt plugin: payload = \"%s\"", payload);
227 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)
249 status = mosquitto_reconnect (conf->mosq);
250 if (status != MOSQ_ERR_SUCCESS)
253 ERROR ("mqtt_connect_broker: mosquitto_connect failed: %s",
254 (status == MOSQ_ERR_ERRNO)
255 ? sstrerror(errno, errbuf, sizeof (errbuf))
256 : mosquitto_strerror (status));
263 &conf->complaint_cantpublish,
264 "mqtt plugin: successfully reconnected to broker \"%s:%d\"",
265 conf->host, conf->port);
268 } /* mqtt_reconnect */
270 /* must hold conf->lock when calling. */
271 static int mqtt_connect (mqtt_client_conf_t *conf)
273 char const *client_id;
276 if (conf->mosq != NULL)
277 return mqtt_reconnect (conf);
280 client_id = conf->client_id;
282 client_id = hostname_g;
284 #if LIBMOSQUITTO_MAJOR == 0
285 conf->mosq = mosquitto_new (client_id, /* user data = */ conf);
287 conf->mosq = mosquitto_new (client_id, conf->clean_session, /* user data = */ conf);
289 if (conf->mosq == NULL)
291 ERROR ("mqtt plugin: mosquitto_new failed");
295 #if LIBMOSQUITTO_MAJOR != 0
296 if (conf->cacertificatefile) {
297 status = mosquitto_tls_set(conf->mosq, conf->cacertificatefile, NULL,
298 conf->certificatefile, conf->certificatekeyfile, /* pw_callback */NULL);
299 if (status != MOSQ_ERR_SUCCESS) {
300 ERROR ("mqtt plugin: cannot mosquitto_tls_set: %s", mosquitto_strerror(status));
301 mosquitto_destroy (conf->mosq);
306 status = mosquitto_tls_opts_set(conf->mosq, SSL_VERIFY_PEER, conf->tlsprotocol, conf->ciphersuite);
307 if (status != MOSQ_ERR_SUCCESS) {
308 ERROR ("mqtt plugin: cannot mosquitto_tls_opts_set: %s", mosquitto_strerror(status));
309 mosquitto_destroy (conf->mosq);
314 status = mosquitto_tls_insecure_set(conf->mosq, false);
315 if (status != MOSQ_ERR_SUCCESS) {
316 ERROR ("mqtt plugin: cannot mosquitto_tls_insecure_set: %s", mosquitto_strerror(status));
317 mosquitto_destroy (conf->mosq);
324 if (conf->username && conf->password)
326 status = mosquitto_username_pw_set (conf->mosq, conf->username, conf->password);
327 if (status != MOSQ_ERR_SUCCESS)
330 ERROR ("mqtt plugin: mosquitto_username_pw_set failed: %s",
331 (status == MOSQ_ERR_ERRNO)
332 ? sstrerror (errno, errbuf, sizeof (errbuf))
333 : mosquitto_strerror (status));
335 mosquitto_destroy (conf->mosq);
341 #if LIBMOSQUITTO_MAJOR == 0
342 status = mosquitto_connect (conf->mosq, conf->host, conf->port,
343 /* keepalive = */ MQTT_KEEPALIVE, /* clean session = */ conf->clean_session);
345 status = mosquitto_connect (conf->mosq, conf->host, conf->port, MQTT_KEEPALIVE);
347 if (status != MOSQ_ERR_SUCCESS)
350 ERROR ("mqtt plugin: mosquitto_connect failed: %s",
351 (status == MOSQ_ERR_ERRNO)
352 ? sstrerror (errno, errbuf, sizeof (errbuf))
353 : mosquitto_strerror (status));
355 mosquitto_destroy (conf->mosq);
362 mosquitto_message_callback_set (conf->mosq, on_message);
364 status = mosquitto_subscribe (conf->mosq,
365 /* message_id = */ NULL,
366 conf->topic, conf->qos);
367 if (status != MOSQ_ERR_SUCCESS)
369 ERROR ("mqtt plugin: Subscribing to \"%s\" failed: %s",
370 conf->topic, mosquitto_strerror (status));
372 mosquitto_disconnect (conf->mosq);
373 mosquitto_destroy (conf->mosq);
383 static void *subscribers_thread (void *arg)
385 mqtt_client_conf_t *conf = arg;
392 status = mqtt_connect (conf);
399 /* The documentation says "0" would map to the default (1000ms), but
400 * that does not work on some versions. */
401 #if LIBMOSQUITTO_MAJOR == 0
402 status = mosquitto_loop (conf->mosq, /* timeout = */ 1000 /* ms */);
404 status = mosquitto_loop (conf->mosq,
405 /* timeout[ms] = */ 1000,
406 /* max_packets = */ 100);
408 if (status == MOSQ_ERR_CONN_LOST)
413 else if (status != MOSQ_ERR_SUCCESS)
415 ERROR ("mqtt plugin: mosquitto_loop failed: %s",
416 mosquitto_strerror (status));
417 mosquitto_destroy (conf->mosq);
423 DEBUG ("mqtt plugin: mosquitto_loop succeeded.");
424 } /* while (conf->loop) */
427 } /* void *subscribers_thread */
429 static int publish (mqtt_client_conf_t *conf, char const *topic,
430 void const *payload, size_t payload_len)
434 pthread_mutex_lock (&conf->lock);
436 status = mqtt_connect (conf);
438 pthread_mutex_unlock (&conf->lock);
439 ERROR ("mqtt plugin: unable to reconnect to broker");
443 status = mosquitto_publish(conf->mosq, /* message_id */ NULL, topic,
444 #if LIBMOSQUITTO_MAJOR == 0
445 (uint32_t) payload_len, payload,
447 (int) payload_len, payload,
449 conf->qos, conf->retain);
450 if (status != MOSQ_ERR_SUCCESS)
454 &conf->complaint_cantpublish,
455 "mqtt plugin: mosquitto_publish failed: %s",
456 (status == MOSQ_ERR_ERRNO)
457 ? sstrerror(errno, errbuf, sizeof (errbuf))
458 : mosquitto_strerror(status));
459 /* Mark our connection "down" regardless of the error as a safety
460 * measure; we will try to reconnect the next time we have to publish a
464 pthread_mutex_unlock (&conf->lock);
468 pthread_mutex_unlock (&conf->lock);
472 static int format_topic (char *buf, size_t buf_len,
473 data_set_t const *ds, value_list_t const *vl,
474 mqtt_client_conf_t *conf)
476 char name[MQTT_MAX_TOPIC_SIZE];
479 if ((conf->topic_prefix == NULL) || (conf->topic_prefix[0] == 0))
480 return (FORMAT_VL (buf, buf_len, vl));
482 status = FORMAT_VL (name, sizeof (name), vl);
486 status = ssnprintf (buf, buf_len, "%s/%s", conf->topic_prefix, name);
487 if ((status < 0) || (((size_t) status) >= buf_len))
491 } /* int format_topic */
493 static int mqtt_write (const data_set_t *ds, const value_list_t *vl,
494 user_data_t *user_data)
496 mqtt_client_conf_t *conf;
497 char topic[MQTT_MAX_TOPIC_SIZE];
498 char payload[MQTT_MAX_MESSAGE_SIZE];
501 if ((user_data == NULL) || (user_data->data == NULL))
503 conf = user_data->data;
505 status = format_topic (topic, sizeof (topic), ds, vl, conf);
508 ERROR ("mqtt plugin: format_topic failed with status %d.", status);
512 status = format_values (payload, sizeof (payload),
513 ds, vl, conf->store_rates);
516 ERROR ("mqtt plugin: format_values failed with status %d.", status);
520 status = publish (conf, topic, payload, strlen (payload) + 1);
523 ERROR ("mqtt plugin: publish failed: %s", mosquitto_strerror (status));
534 * ClientId "collectd"
541 * CACert "ca.pem" Enables TLS if set
542 * CertificateFile "client-cert.pem" optional
543 * CertificateKeyFile "client-key.pem" optional
544 * TLSProtocol "tlsv1.2" optional
547 static int mqtt_config_publisher (oconfig_item_t *ci)
549 mqtt_client_conf_t *conf;
551 user_data_t user_data = { 0 };
554 conf = calloc (1, sizeof (*conf));
557 ERROR ("mqtt plugin: calloc failed.");
563 status = cf_util_get_string (ci, &conf->name);
570 conf->host = strdup (MQTT_DEFAULT_HOST);
571 conf->port = MQTT_DEFAULT_PORT;
572 conf->client_id = NULL;
574 conf->topic_prefix = strdup (MQTT_DEFAULT_TOPIC_PREFIX);
575 conf->store_rates = 1;
577 status = pthread_mutex_init (&conf->lock, NULL);
584 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
586 for (int i = 0; i < ci->children_num; i++)
588 oconfig_item_t *child = ci->children + i;
589 if (strcasecmp ("Host", child->key) == 0)
590 cf_util_get_string (child, &conf->host);
591 else if (strcasecmp ("Port", child->key) == 0)
593 int tmp = cf_util_get_port_number (child);
595 ERROR ("mqtt plugin: Invalid port number.");
599 else if (strcasecmp ("ClientId", child->key) == 0)
600 cf_util_get_string (child, &conf->client_id);
601 else if (strcasecmp ("User", child->key) == 0)
602 cf_util_get_string (child, &conf->username);
603 else if (strcasecmp ("Password", child->key) == 0)
604 cf_util_get_string (child, &conf->password);
605 else if (strcasecmp ("QoS", child->key) == 0)
608 status = cf_util_get_int (child, &tmp);
609 if ((status != 0) || (tmp < 0) || (tmp > 2))
610 ERROR ("mqtt plugin: Not a valid QoS setting.");
614 else if (strcasecmp ("Prefix", child->key) == 0)
615 cf_util_get_string (child, &conf->topic_prefix);
616 else if (strcasecmp ("StoreRates", child->key) == 0)
617 cf_util_get_boolean (child, &conf->store_rates);
618 else if (strcasecmp ("Retain", child->key) == 0)
619 cf_util_get_boolean (child, &conf->retain);
620 else if (strcasecmp ("CACert", child->key) == 0)
621 cf_util_get_string (child, &conf->cacertificatefile);
622 else if (strcasecmp ("CertificateFile", child->key) == 0)
623 cf_util_get_string (child, &conf->certificatefile);
624 else if (strcasecmp ("CertificateKeyFile", child->key) == 0)
625 cf_util_get_string (child, &conf->certificatekeyfile);
626 else if (strcasecmp ("TLSProtocol", child->key) == 0)
627 cf_util_get_string (child, &conf->tlsprotocol);
628 else if (strcasecmp ("CipherSuite", child->key) == 0)
629 cf_util_get_string (child, &conf->ciphersuite);
631 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
634 ssnprintf (cb_name, sizeof (cb_name), "mqtt/%s", conf->name);
635 user_data.data = conf;
637 plugin_register_write (cb_name, mqtt_write, &user_data);
639 } /* mqtt_config_publisher */
645 * ClientId "collectd"
651 static int mqtt_config_subscriber (oconfig_item_t *ci)
653 mqtt_client_conf_t **tmp;
654 mqtt_client_conf_t *conf;
657 conf = calloc (1, sizeof (*conf));
660 ERROR ("mqtt plugin: calloc failed.");
666 status = cf_util_get_string (ci, &conf->name);
673 conf->host = strdup (MQTT_DEFAULT_HOST);
674 conf->port = MQTT_DEFAULT_PORT;
675 conf->client_id = NULL;
677 conf->topic = strdup (MQTT_DEFAULT_TOPIC);
678 conf->clean_session = 1;
680 status = pthread_mutex_init (&conf->lock, NULL);
687 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
689 for (int i = 0; i < ci->children_num; i++)
691 oconfig_item_t *child = ci->children + i;
692 if (strcasecmp ("Host", child->key) == 0)
693 cf_util_get_string (child, &conf->host);
694 else if (strcasecmp ("Port", child->key) == 0)
696 status = cf_util_get_port_number (child);
698 ERROR ("mqtt plugin: Invalid port number.");
702 else if (strcasecmp ("ClientId", child->key) == 0)
703 cf_util_get_string (child, &conf->client_id);
704 else if (strcasecmp ("User", child->key) == 0)
705 cf_util_get_string (child, &conf->username);
706 else if (strcasecmp ("Password", child->key) == 0)
707 cf_util_get_string (child, &conf->password);
708 else if (strcasecmp ("QoS", child->key) == 0)
711 status = cf_util_get_int (child, &qos);
712 if ((status != 0) || (qos < 0) || (qos > 2))
713 ERROR ("mqtt plugin: Not a valid QoS setting.");
717 else if (strcasecmp ("Topic", child->key) == 0)
718 cf_util_get_string (child, &conf->topic);
719 else if (strcasecmp ("CleanSession", child->key) == 0)
720 cf_util_get_boolean (child, &conf->clean_session);
722 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
725 tmp = realloc (subscribers, sizeof (*subscribers) * (subscribers_num + 1) );
728 ERROR ("mqtt plugin: realloc failed.");
733 subscribers[subscribers_num] = conf;
737 } /* mqtt_config_subscriber */
749 static int mqtt_config (oconfig_item_t *ci)
751 for (int i = 0; i < ci->children_num; i++)
753 oconfig_item_t *child = ci->children + i;
755 if (strcasecmp ("Publish", child->key) == 0)
756 mqtt_config_publisher (child);
757 else if (strcasecmp ("Subscribe", child->key) == 0)
758 mqtt_config_subscriber (child);
760 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
764 } /* int mqtt_config */
766 static int mqtt_init (void)
768 mosquitto_lib_init ();
770 for (size_t i = 0; i < subscribers_num; i++)
774 if (subscribers[i]->loop)
777 status = plugin_thread_create (&subscribers[i]->thread,
779 /* func = */ subscribers_thread,
780 /* args = */ subscribers[i]);
784 ERROR ("mqtt plugin: pthread_create failed: %s",
785 sstrerror (errno, errbuf, sizeof (errbuf)));
793 void module_register (void)
795 plugin_register_complex_config ("mqtt", mqtt_config);
796 plugin_register_init ("mqtt", mqtt_init);
797 } /* void module_register */
799 /* vim: set sw=4 sts=4 et fdm=marker : */