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.");
227 memmove (payload, msg->payload, msg->payloadlen);
228 payload[msg->payloadlen] = 0;
230 DEBUG ("mqtt plugin: payload = \"%s\"", payload);
231 status = parse_values (payload, &vl, ds);
234 ERROR ("mqtt plugin: Unable to parse payload \"%s\".", payload);
241 plugin_dispatch_values (&vl);
243 } /* void on_message */
245 /* must hold conf->lock when calling. */
246 static int mqtt_reconnect (mqtt_client_conf_t *conf)
253 status = mosquitto_reconnect (conf->mosq);
254 if (status != MOSQ_ERR_SUCCESS)
257 ERROR ("mqtt_connect_broker: mosquitto_connect failed: %s",
258 (status == MOSQ_ERR_ERRNO)
259 ? sstrerror(errno, errbuf, sizeof (errbuf))
260 : mosquitto_strerror (status));
267 &conf->complaint_cantpublish,
268 "mqtt plugin: successfully reconnected to broker \"%s:%d\"",
269 conf->host, conf->port);
272 } /* mqtt_reconnect */
274 /* must hold conf->lock when calling. */
275 static int mqtt_connect (mqtt_client_conf_t *conf)
277 char const *client_id;
280 if (conf->mosq != NULL)
281 return mqtt_reconnect (conf);
284 client_id = conf->client_id;
286 client_id = hostname_g;
288 #if LIBMOSQUITTO_MAJOR == 0
289 conf->mosq = mosquitto_new (client_id, /* user data = */ conf);
291 conf->mosq = mosquitto_new (client_id, conf->clean_session, /* user data = */ conf);
293 if (conf->mosq == NULL)
295 ERROR ("mqtt plugin: mosquitto_new failed");
299 #if LIBMOSQUITTO_MAJOR != 0
300 if (conf->cacertificatefile) {
301 status = mosquitto_tls_set(conf->mosq, conf->cacertificatefile, NULL,
302 conf->certificatefile, conf->certificatekeyfile, /* pw_callback */NULL);
303 if (status != MOSQ_ERR_SUCCESS) {
304 ERROR ("mqtt plugin: cannot mosquitto_tls_set: %s", mosquitto_strerror(status));
305 mosquitto_destroy (conf->mosq);
310 status = mosquitto_tls_opts_set(conf->mosq, SSL_VERIFY_PEER, conf->tlsprotocol, conf->ciphersuite);
311 if (status != MOSQ_ERR_SUCCESS) {
312 ERROR ("mqtt plugin: cannot mosquitto_tls_opts_set: %s", mosquitto_strerror(status));
313 mosquitto_destroy (conf->mosq);
318 status = mosquitto_tls_insecure_set(conf->mosq, false);
319 if (status != MOSQ_ERR_SUCCESS) {
320 ERROR ("mqtt plugin: cannot mosquitto_tls_insecure_set: %s", mosquitto_strerror(status));
321 mosquitto_destroy (conf->mosq);
328 if (conf->username && conf->password)
330 status = mosquitto_username_pw_set (conf->mosq, conf->username, conf->password);
331 if (status != MOSQ_ERR_SUCCESS)
334 ERROR ("mqtt plugin: mosquitto_username_pw_set failed: %s",
335 (status == MOSQ_ERR_ERRNO)
336 ? sstrerror (errno, errbuf, sizeof (errbuf))
337 : mosquitto_strerror (status));
339 mosquitto_destroy (conf->mosq);
345 #if LIBMOSQUITTO_MAJOR == 0
346 status = mosquitto_connect (conf->mosq, conf->host, conf->port,
347 /* keepalive = */ MQTT_KEEPALIVE, /* clean session = */ conf->clean_session);
349 status = mosquitto_connect (conf->mosq, conf->host, conf->port, MQTT_KEEPALIVE);
351 if (status != MOSQ_ERR_SUCCESS)
354 ERROR ("mqtt plugin: mosquitto_connect failed: %s",
355 (status == MOSQ_ERR_ERRNO)
356 ? sstrerror (errno, errbuf, sizeof (errbuf))
357 : mosquitto_strerror (status));
359 mosquitto_destroy (conf->mosq);
366 mosquitto_message_callback_set (conf->mosq, on_message);
368 status = mosquitto_subscribe (conf->mosq,
369 /* message_id = */ NULL,
370 conf->topic, conf->qos);
371 if (status != MOSQ_ERR_SUCCESS)
373 ERROR ("mqtt plugin: Subscribing to \"%s\" failed: %s",
374 conf->topic, mosquitto_strerror (status));
376 mosquitto_disconnect (conf->mosq);
377 mosquitto_destroy (conf->mosq);
387 static void *subscribers_thread (void *arg)
389 mqtt_client_conf_t *conf = arg;
396 status = mqtt_connect (conf);
403 /* The documentation says "0" would map to the default (1000ms), but
404 * that does not work on some versions. */
405 #if LIBMOSQUITTO_MAJOR == 0
406 status = mosquitto_loop (conf->mosq, /* timeout = */ 1000 /* ms */);
408 status = mosquitto_loop (conf->mosq,
409 /* timeout[ms] = */ 1000,
410 /* max_packets = */ 100);
412 if (status == MOSQ_ERR_CONN_LOST)
417 else if (status != MOSQ_ERR_SUCCESS)
419 ERROR ("mqtt plugin: mosquitto_loop failed: %s",
420 mosquitto_strerror (status));
421 mosquitto_destroy (conf->mosq);
427 DEBUG ("mqtt plugin: mosquitto_loop succeeded.");
428 } /* while (conf->loop) */
431 } /* void *subscribers_thread */
433 static int publish (mqtt_client_conf_t *conf, char const *topic,
434 void const *payload, size_t payload_len)
438 pthread_mutex_lock (&conf->lock);
440 status = mqtt_connect (conf);
442 pthread_mutex_unlock (&conf->lock);
443 ERROR ("mqtt plugin: unable to reconnect to broker");
447 status = mosquitto_publish(conf->mosq, /* message_id */ NULL, topic,
448 #if LIBMOSQUITTO_MAJOR == 0
449 (uint32_t) payload_len, payload,
451 (int) payload_len, payload,
453 conf->qos, conf->retain);
454 if (status != MOSQ_ERR_SUCCESS)
458 &conf->complaint_cantpublish,
459 "mqtt plugin: mosquitto_publish failed: %s",
460 (status == MOSQ_ERR_ERRNO)
461 ? sstrerror(errno, errbuf, sizeof (errbuf))
462 : mosquitto_strerror(status));
463 /* Mark our connection "down" regardless of the error as a safety
464 * measure; we will try to reconnect the next time we have to publish a
468 pthread_mutex_unlock (&conf->lock);
472 pthread_mutex_unlock (&conf->lock);
476 static int format_topic (char *buf, size_t buf_len,
477 data_set_t const *ds, value_list_t const *vl,
478 mqtt_client_conf_t *conf)
480 char name[MQTT_MAX_TOPIC_SIZE];
483 if ((conf->topic_prefix == NULL) || (conf->topic_prefix[0] == 0))
484 return (FORMAT_VL (buf, buf_len, vl));
486 status = FORMAT_VL (name, sizeof (name), vl);
490 status = ssnprintf (buf, buf_len, "%s/%s", conf->topic_prefix, name);
491 if ((status < 0) || (((size_t) status) >= buf_len))
495 } /* int format_topic */
497 static int mqtt_write (const data_set_t *ds, const value_list_t *vl,
498 user_data_t *user_data)
500 mqtt_client_conf_t *conf;
501 char topic[MQTT_MAX_TOPIC_SIZE];
502 char payload[MQTT_MAX_MESSAGE_SIZE];
505 if ((user_data == NULL) || (user_data->data == NULL))
507 conf = user_data->data;
509 status = format_topic (topic, sizeof (topic), ds, vl, conf);
512 ERROR ("mqtt plugin: format_topic failed with status %d.", status);
516 status = format_values (payload, sizeof (payload),
517 ds, vl, conf->store_rates);
520 ERROR ("mqtt plugin: format_values failed with status %d.", status);
524 status = publish (conf, topic, payload, strlen (payload) + 1);
527 ERROR ("mqtt plugin: publish failed: %s", mosquitto_strerror (status));
538 * ClientId "collectd"
545 * CACert "ca.pem" Enables TLS if set
546 * CertificateFile "client-cert.pem" optional
547 * CertificateKeyFile "client-key.pem" optional
548 * TLSProtocol "tlsv1.2" optional
551 static int mqtt_config_publisher (oconfig_item_t *ci)
553 mqtt_client_conf_t *conf;
555 user_data_t user_data;
559 conf = calloc (1, sizeof (*conf));
562 ERROR ("mqtt plugin: calloc failed.");
568 status = cf_util_get_string (ci, &conf->name);
575 conf->host = strdup (MQTT_DEFAULT_HOST);
576 conf->port = MQTT_DEFAULT_PORT;
577 conf->client_id = NULL;
579 conf->topic_prefix = strdup (MQTT_DEFAULT_TOPIC_PREFIX);
580 conf->store_rates = 1;
582 status = pthread_mutex_init (&conf->lock, NULL);
589 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
591 for (i = 0; i < ci->children_num; i++)
593 oconfig_item_t *child = ci->children + i;
594 if (strcasecmp ("Host", child->key) == 0)
595 cf_util_get_string (child, &conf->host);
596 else if (strcasecmp ("Port", child->key) == 0)
598 int tmp = cf_util_get_port_number (child);
600 ERROR ("mqtt plugin: Invalid port number.");
604 else if (strcasecmp ("ClientId", child->key) == 0)
605 cf_util_get_string (child, &conf->client_id);
606 else if (strcasecmp ("User", child->key) == 0)
607 cf_util_get_string (child, &conf->username);
608 else if (strcasecmp ("Password", child->key) == 0)
609 cf_util_get_string (child, &conf->password);
610 else if (strcasecmp ("QoS", child->key) == 0)
613 status = cf_util_get_int (child, &tmp);
614 if ((status != 0) || (tmp < 0) || (tmp > 2))
615 ERROR ("mqtt plugin: Not a valid QoS setting.");
619 else if (strcasecmp ("Prefix", child->key) == 0)
620 cf_util_get_string (child, &conf->topic_prefix);
621 else if (strcasecmp ("StoreRates", child->key) == 0)
622 cf_util_get_boolean (child, &conf->store_rates);
623 else if (strcasecmp ("Retain", child->key) == 0)
624 cf_util_get_boolean (child, &conf->retain);
625 else if (strcasecmp ("CACert", child->key) == 0)
626 cf_util_get_string (child, &conf->cacertificatefile);
627 else if (strcasecmp ("CertificateFile", child->key) == 0)
628 cf_util_get_string (child, &conf->certificatefile);
629 else if (strcasecmp ("CertificateKeyFile", child->key) == 0)
630 cf_util_get_string (child, &conf->certificatekeyfile);
631 else if (strcasecmp ("TLSProtocol", child->key) == 0)
632 cf_util_get_string (child, &conf->tlsprotocol);
633 else if (strcasecmp ("CipherSuite", child->key) == 0)
634 cf_util_get_string (child, &conf->ciphersuite);
636 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
639 ssnprintf (cb_name, sizeof (cb_name), "mqtt/%s", conf->name);
640 memset (&user_data, 0, sizeof (user_data));
641 user_data.data = conf;
643 plugin_register_write (cb_name, mqtt_write, &user_data);
645 } /* mqtt_config_publisher */
651 * ClientId "collectd"
657 static int mqtt_config_subscriber (oconfig_item_t *ci)
659 mqtt_client_conf_t **tmp;
660 mqtt_client_conf_t *conf;
664 conf = calloc (1, sizeof (*conf));
667 ERROR ("mqtt plugin: calloc failed.");
673 status = cf_util_get_string (ci, &conf->name);
680 conf->host = strdup (MQTT_DEFAULT_HOST);
681 conf->port = MQTT_DEFAULT_PORT;
682 conf->client_id = NULL;
684 conf->topic = strdup (MQTT_DEFAULT_TOPIC);
685 conf->clean_session = 1;
687 status = pthread_mutex_init (&conf->lock, NULL);
694 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
696 for (i = 0; i < ci->children_num; i++)
698 oconfig_item_t *child = ci->children + i;
699 if (strcasecmp ("Host", child->key) == 0)
700 cf_util_get_string (child, &conf->host);
701 else if (strcasecmp ("Port", child->key) == 0)
703 status = cf_util_get_port_number (child);
705 ERROR ("mqtt plugin: Invalid port number.");
709 else if (strcasecmp ("ClientId", child->key) == 0)
710 cf_util_get_string (child, &conf->client_id);
711 else if (strcasecmp ("User", child->key) == 0)
712 cf_util_get_string (child, &conf->username);
713 else if (strcasecmp ("Password", child->key) == 0)
714 cf_util_get_string (child, &conf->password);
715 else if (strcasecmp ("QoS", child->key) == 0)
718 status = cf_util_get_int (child, &qos);
719 if ((status != 0) || (qos < 0) || (qos > 2))
720 ERROR ("mqtt plugin: Not a valid QoS setting.");
724 else if (strcasecmp ("Topic", child->key) == 0)
725 cf_util_get_string (child, &conf->topic);
726 else if (strcasecmp ("CleanSession", child->key) == 0)
727 cf_util_get_boolean (child, &conf->clean_session);
729 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
732 tmp = realloc (subscribers, sizeof (*subscribers) * (subscribers_num + 1) );
735 ERROR ("mqtt plugin: realloc failed.");
740 subscribers[subscribers_num] = conf;
744 } /* mqtt_config_subscriber */
756 static int mqtt_config (oconfig_item_t *ci)
760 for (i = 0; i < ci->children_num; i++)
762 oconfig_item_t *child = ci->children + i;
764 if (strcasecmp ("Publish", child->key) == 0)
765 mqtt_config_publisher (child);
766 else if (strcasecmp ("Subscribe", child->key) == 0)
767 mqtt_config_subscriber (child);
769 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
773 } /* int mqtt_config */
775 static int mqtt_init (void)
779 mosquitto_lib_init ();
781 for (i = 0; i < subscribers_num; i++)
785 if (subscribers[i]->loop)
788 status = plugin_thread_create (&subscribers[i]->thread,
790 /* func = */ subscribers_thread,
791 /* args = */ subscribers[i]);
795 ERROR ("mqtt plugin: pthread_create failed: %s",
796 sstrerror (errno, errbuf, sizeof (errbuf)));
804 void module_register (void)
806 plugin_register_complex_config ("mqtt", mqtt_config);
807 plugin_register_init ("mqtt", mqtt_init);
808 } /* void module_register */
810 /* vim: set sw=4 sts=4 et fdm=marker : */