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"
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)
152 for (i = 0; topic[i] != 0; i++)
161 char *tmp = strchr (topic, '/');
171 static void on_message (
172 #if LIBMOSQUITTO_MAJOR == 0
174 __attribute__((unused)) struct mosquitto *m,
176 __attribute__((unused)) void *arg,
177 const struct mosquitto_message *msg)
179 value_list_t vl = VALUE_LIST_INIT;
180 data_set_t const *ds;
186 if (msg->payloadlen <= 0) {
187 DEBUG ("mqtt plugin: message has empty payload");
191 topic = strdup (msg->topic);
192 name = strip_prefix (topic);
194 status = parse_identifier_vl (name, &vl);
197 ERROR ("mqtt plugin: Unable to parse topic \"%s\".", topic);
203 ds = plugin_get_ds (vl.type);
206 ERROR ("mqtt plugin: Unknown type: \"%s\".", vl.type);
210 vl.values = calloc (ds->ds_num, sizeof (*vl.values));
211 if (vl.values == NULL)
213 ERROR ("mqtt plugin: calloc failed.");
216 vl.values_len = ds->ds_num;
218 payload = malloc (msg->payloadlen+1);
221 ERROR ("mqtt plugin: malloc for payload buffer failed.");
225 memmove (payload, msg->payload, msg->payloadlen);
226 payload[msg->payloadlen] = 0;
228 DEBUG ("mqtt plugin: payload = \"%s\"", payload);
229 status = parse_values (payload, &vl, ds);
232 ERROR ("mqtt plugin: Unable to parse payload \"%s\".", payload);
239 plugin_dispatch_values (&vl);
241 } /* void on_message */
243 /* must hold conf->lock when calling. */
244 static int mqtt_reconnect (mqtt_client_conf_t *conf)
251 status = mosquitto_reconnect (conf->mosq);
252 if (status != MOSQ_ERR_SUCCESS)
255 ERROR ("mqtt_connect_broker: mosquitto_connect failed: %s",
256 (status == MOSQ_ERR_ERRNO)
257 ? sstrerror(errno, errbuf, sizeof (errbuf))
258 : mosquitto_strerror (status));
265 &conf->complaint_cantpublish,
266 "mqtt plugin: successfully reconnected to broker \"%s:%d\"",
267 conf->host, conf->port);
270 } /* mqtt_reconnect */
272 /* must hold conf->lock when calling. */
273 static int mqtt_connect (mqtt_client_conf_t *conf)
275 char const *client_id;
278 if (conf->mosq != NULL)
279 return mqtt_reconnect (conf);
282 client_id = conf->client_id;
284 client_id = hostname_g;
286 #if LIBMOSQUITTO_MAJOR == 0
287 conf->mosq = mosquitto_new (client_id, /* user data = */ conf);
289 conf->mosq = mosquitto_new (client_id, conf->clean_session, /* user data = */ conf);
291 if (conf->mosq == NULL)
293 ERROR ("mqtt plugin: mosquitto_new failed");
297 #if LIBMOSQUITTO_MAJOR != 0
298 if (conf->cacertificatefile) {
299 status = mosquitto_tls_set(conf->mosq, conf->cacertificatefile, NULL,
300 conf->certificatefile, conf->certificatekeyfile, /* pw_callback */NULL);
301 if (status != MOSQ_ERR_SUCCESS) {
302 ERROR ("mqtt plugin: cannot mosquitto_tls_set: %s", mosquitto_strerror(status));
303 mosquitto_destroy (conf->mosq);
308 status = mosquitto_tls_opts_set(conf->mosq, SSL_VERIFY_PEER, conf->tlsprotocol, conf->ciphersuite);
309 if (status != MOSQ_ERR_SUCCESS) {
310 ERROR ("mqtt plugin: cannot mosquitto_tls_opts_set: %s", mosquitto_strerror(status));
311 mosquitto_destroy (conf->mosq);
316 status = mosquitto_tls_insecure_set(conf->mosq, false);
317 if (status != MOSQ_ERR_SUCCESS) {
318 ERROR ("mqtt plugin: cannot mosquitto_tls_insecure_set: %s", mosquitto_strerror(status));
319 mosquitto_destroy (conf->mosq);
326 if (conf->username && conf->password)
328 status = mosquitto_username_pw_set (conf->mosq, conf->username, conf->password);
329 if (status != MOSQ_ERR_SUCCESS)
332 ERROR ("mqtt plugin: mosquitto_username_pw_set failed: %s",
333 (status == MOSQ_ERR_ERRNO)
334 ? sstrerror (errno, errbuf, sizeof (errbuf))
335 : mosquitto_strerror (status));
337 mosquitto_destroy (conf->mosq);
343 #if LIBMOSQUITTO_MAJOR == 0
344 status = mosquitto_connect (conf->mosq, conf->host, conf->port,
345 /* keepalive = */ MQTT_KEEPALIVE, /* clean session = */ conf->clean_session);
347 status = mosquitto_connect (conf->mosq, conf->host, conf->port, MQTT_KEEPALIVE);
349 if (status != MOSQ_ERR_SUCCESS)
352 ERROR ("mqtt plugin: mosquitto_connect failed: %s",
353 (status == MOSQ_ERR_ERRNO)
354 ? sstrerror (errno, errbuf, sizeof (errbuf))
355 : mosquitto_strerror (status));
357 mosquitto_destroy (conf->mosq);
364 mosquitto_message_callback_set (conf->mosq, on_message);
366 status = mosquitto_subscribe (conf->mosq,
367 /* message_id = */ NULL,
368 conf->topic, conf->qos);
369 if (status != MOSQ_ERR_SUCCESS)
371 ERROR ("mqtt plugin: Subscribing to \"%s\" failed: %s",
372 conf->topic, mosquitto_strerror (status));
374 mosquitto_disconnect (conf->mosq);
375 mosquitto_destroy (conf->mosq);
385 static void *subscribers_thread (void *arg)
387 mqtt_client_conf_t *conf = arg;
394 status = mqtt_connect (conf);
401 /* The documentation says "0" would map to the default (1000ms), but
402 * that does not work on some versions. */
403 #if LIBMOSQUITTO_MAJOR == 0
404 status = mosquitto_loop (conf->mosq, /* timeout = */ 1000 /* ms */);
406 status = mosquitto_loop (conf->mosq,
407 /* timeout[ms] = */ 1000,
408 /* max_packets = */ 100);
410 if (status == MOSQ_ERR_CONN_LOST)
415 else if (status != MOSQ_ERR_SUCCESS)
417 ERROR ("mqtt plugin: mosquitto_loop failed: %s",
418 mosquitto_strerror (status));
419 mosquitto_destroy (conf->mosq);
425 DEBUG ("mqtt plugin: mosquitto_loop succeeded.");
426 } /* while (conf->loop) */
429 } /* void *subscribers_thread */
431 static int publish (mqtt_client_conf_t *conf, char const *topic,
432 void const *payload, size_t payload_len)
436 pthread_mutex_lock (&conf->lock);
438 status = mqtt_connect (conf);
440 pthread_mutex_unlock (&conf->lock);
441 ERROR ("mqtt plugin: unable to reconnect to broker");
445 status = mosquitto_publish(conf->mosq, /* message_id */ NULL, topic,
446 #if LIBMOSQUITTO_MAJOR == 0
447 (uint32_t) payload_len, payload,
449 (int) payload_len, payload,
451 conf->qos, conf->retain);
452 if (status != MOSQ_ERR_SUCCESS)
456 &conf->complaint_cantpublish,
457 "mqtt plugin: mosquitto_publish failed: %s",
458 (status == MOSQ_ERR_ERRNO)
459 ? sstrerror(errno, errbuf, sizeof (errbuf))
460 : mosquitto_strerror(status));
461 /* Mark our connection "down" regardless of the error as a safety
462 * measure; we will try to reconnect the next time we have to publish a
466 pthread_mutex_unlock (&conf->lock);
470 pthread_mutex_unlock (&conf->lock);
474 static int format_topic (char *buf, size_t buf_len,
475 data_set_t const *ds, value_list_t const *vl,
476 mqtt_client_conf_t *conf)
478 char name[MQTT_MAX_TOPIC_SIZE];
481 if ((conf->topic_prefix == NULL) || (conf->topic_prefix[0] == 0))
482 return (FORMAT_VL (buf, buf_len, vl));
484 status = FORMAT_VL (name, sizeof (name), vl);
488 status = ssnprintf (buf, buf_len, "%s/%s", conf->topic_prefix, name);
489 if ((status < 0) || (((size_t) status) >= buf_len))
493 } /* int format_topic */
495 static int mqtt_write (const data_set_t *ds, const value_list_t *vl,
496 user_data_t *user_data)
498 mqtt_client_conf_t *conf;
499 char topic[MQTT_MAX_TOPIC_SIZE];
500 char payload[MQTT_MAX_MESSAGE_SIZE];
503 if ((user_data == NULL) || (user_data->data == NULL))
505 conf = user_data->data;
507 status = format_topic (topic, sizeof (topic), ds, vl, conf);
510 ERROR ("mqtt plugin: format_topic failed with status %d.", status);
514 status = format_values (payload, sizeof (payload),
515 ds, vl, conf->store_rates);
518 ERROR ("mqtt plugin: format_values failed with status %d.", status);
522 status = publish (conf, topic, payload, strlen (payload) + 1);
525 ERROR ("mqtt plugin: publish failed: %s", mosquitto_strerror (status));
536 * ClientId "collectd"
543 * CACert "ca.pem" Enables TLS if set
544 * CertificateFile "client-cert.pem" optional
545 * CertificateKeyFile "client-key.pem" optional
546 * TLSProtocol "tlsv1.2" optional
549 static int mqtt_config_publisher (oconfig_item_t *ci)
551 mqtt_client_conf_t *conf;
553 user_data_t user_data;
557 conf = calloc (1, sizeof (*conf));
560 ERROR ("mqtt plugin: calloc failed.");
566 status = cf_util_get_string (ci, &conf->name);
573 conf->host = strdup (MQTT_DEFAULT_HOST);
574 conf->port = MQTT_DEFAULT_PORT;
575 conf->client_id = NULL;
577 conf->topic_prefix = strdup (MQTT_DEFAULT_TOPIC_PREFIX);
578 conf->store_rates = 1;
580 status = pthread_mutex_init (&conf->lock, NULL);
587 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
589 for (i = 0; i < ci->children_num; i++)
591 oconfig_item_t *child = ci->children + i;
592 if (strcasecmp ("Host", child->key) == 0)
593 cf_util_get_string (child, &conf->host);
594 else if (strcasecmp ("Port", child->key) == 0)
596 int tmp = cf_util_get_port_number (child);
598 ERROR ("mqtt plugin: Invalid port number.");
602 else if (strcasecmp ("ClientId", child->key) == 0)
603 cf_util_get_string (child, &conf->client_id);
604 else if (strcasecmp ("User", child->key) == 0)
605 cf_util_get_string (child, &conf->username);
606 else if (strcasecmp ("Password", child->key) == 0)
607 cf_util_get_string (child, &conf->password);
608 else if (strcasecmp ("QoS", child->key) == 0)
611 status = cf_util_get_int (child, &tmp);
612 if ((status != 0) || (tmp < 0) || (tmp > 2))
613 ERROR ("mqtt plugin: Not a valid QoS setting.");
617 else if (strcasecmp ("Prefix", child->key) == 0)
618 cf_util_get_string (child, &conf->topic_prefix);
619 else if (strcasecmp ("StoreRates", child->key) == 0)
620 cf_util_get_boolean (child, &conf->store_rates);
621 else if (strcasecmp ("Retain", child->key) == 0)
622 cf_util_get_boolean (child, &conf->retain);
623 else if (strcasecmp ("CACert", child->key) == 0)
624 cf_util_get_string (child, &conf->cacertificatefile);
625 else if (strcasecmp ("CertificateFile", child->key) == 0)
626 cf_util_get_string (child, &conf->certificatefile);
627 else if (strcasecmp ("CertificateKeyFile", child->key) == 0)
628 cf_util_get_string (child, &conf->certificatekeyfile);
629 else if (strcasecmp ("TLSProtocol", child->key) == 0)
630 cf_util_get_string (child, &conf->tlsprotocol);
631 else if (strcasecmp ("CipherSuite", child->key) == 0)
632 cf_util_get_string (child, &conf->ciphersuite);
634 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
637 ssnprintf (cb_name, sizeof (cb_name), "mqtt/%s", conf->name);
638 memset (&user_data, 0, sizeof (user_data));
639 user_data.data = conf;
641 plugin_register_write (cb_name, mqtt_write, &user_data);
643 } /* mqtt_config_publisher */
649 * ClientId "collectd"
655 static int mqtt_config_subscriber (oconfig_item_t *ci)
657 mqtt_client_conf_t **tmp;
658 mqtt_client_conf_t *conf;
662 conf = calloc (1, sizeof (*conf));
665 ERROR ("mqtt plugin: calloc failed.");
671 status = cf_util_get_string (ci, &conf->name);
678 conf->host = strdup (MQTT_DEFAULT_HOST);
679 conf->port = MQTT_DEFAULT_PORT;
680 conf->client_id = NULL;
682 conf->topic = strdup (MQTT_DEFAULT_TOPIC);
683 conf->clean_session = 1;
685 status = pthread_mutex_init (&conf->lock, NULL);
692 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
694 for (i = 0; i < ci->children_num; i++)
696 oconfig_item_t *child = ci->children + i;
697 if (strcasecmp ("Host", child->key) == 0)
698 cf_util_get_string (child, &conf->host);
699 else if (strcasecmp ("Port", child->key) == 0)
701 status = cf_util_get_port_number (child);
703 ERROR ("mqtt plugin: Invalid port number.");
707 else if (strcasecmp ("ClientId", child->key) == 0)
708 cf_util_get_string (child, &conf->client_id);
709 else if (strcasecmp ("User", child->key) == 0)
710 cf_util_get_string (child, &conf->username);
711 else if (strcasecmp ("Password", child->key) == 0)
712 cf_util_get_string (child, &conf->password);
713 else if (strcasecmp ("QoS", child->key) == 0)
716 status = cf_util_get_int (child, &qos);
717 if ((status != 0) || (qos < 0) || (qos > 2))
718 ERROR ("mqtt plugin: Not a valid QoS setting.");
722 else if (strcasecmp ("Topic", child->key) == 0)
723 cf_util_get_string (child, &conf->topic);
724 else if (strcasecmp ("CleanSession", child->key) == 0)
725 cf_util_get_boolean (child, &conf->clean_session);
727 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
730 tmp = realloc (subscribers, sizeof (*subscribers) * (subscribers_num + 1) );
733 ERROR ("mqtt plugin: realloc failed.");
738 subscribers[subscribers_num] = conf;
742 } /* mqtt_config_subscriber */
754 static int mqtt_config (oconfig_item_t *ci)
758 for (i = 0; i < ci->children_num; i++)
760 oconfig_item_t *child = ci->children + i;
762 if (strcasecmp ("Publish", child->key) == 0)
763 mqtt_config_publisher (child);
764 else if (strcasecmp ("Subscribe", child->key) == 0)
765 mqtt_config_subscriber (child);
767 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
771 } /* int mqtt_config */
773 static int mqtt_init (void)
777 mosquitto_lib_init ();
779 for (i = 0; i < subscribers_num; i++)
783 if (subscribers[i]->loop)
786 status = plugin_thread_create (&subscribers[i]->thread,
788 /* func = */ subscribers_thread,
789 /* args = */ subscribers[i]);
793 ERROR ("mqtt plugin: pthread_create failed: %s",
794 sstrerror (errno, errbuf, sizeof (errbuf)));
802 void module_register (void)
804 plugin_register_complex_config ("mqtt", mqtt_config);
805 plugin_register_init ("mqtt", mqtt_init);
806 } /* void module_register */
808 /* vim: set sw=4 sts=4 et fdm=marker : */