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>
29 // Reference: http://mosquitto.org/api/files/mosquitto-h.html
35 #include "utils_cache.h"
36 #include "utils_complain.h"
40 #include <mosquitto.h>
42 #define MQTT_MAX_TOPIC_SIZE 1024
43 #define MQTT_MAX_MESSAGE_SIZE MQTT_MAX_TOPIC_SIZE + 1024
44 #define MQTT_DEFAULT_HOST "localhost"
45 #define MQTT_DEFAULT_PORT 1883
46 #define MQTT_DEFAULT_TOPIC_PREFIX "collectd"
47 #define MQTT_DEFAULT_TOPIC "collectd/#"
48 #ifndef MQTT_KEEPALIVE
49 # define MQTT_KEEPALIVE 60
56 struct mqtt_client_conf
61 struct mosquitto *mosq;
82 c_complain_t complaint_cantpublish;
85 typedef struct mqtt_client_conf mqtt_client_conf_t;
87 static mqtt_client_conf_t **subscribers = NULL;
88 static size_t subscribers_num = 0;
93 #if LIBMOSQUITTO_MAJOR == 0
94 static char const *mosquitto_strerror (int code)
98 case MOSQ_ERR_SUCCESS: return "MOSQ_ERR_SUCCESS";
99 case MOSQ_ERR_NOMEM: return "MOSQ_ERR_NOMEM";
100 case MOSQ_ERR_PROTOCOL: return "MOSQ_ERR_PROTOCOL";
101 case MOSQ_ERR_INVAL: return "MOSQ_ERR_INVAL";
102 case MOSQ_ERR_NO_CONN: return "MOSQ_ERR_NO_CONN";
103 case MOSQ_ERR_CONN_REFUSED: return "MOSQ_ERR_CONN_REFUSED";
104 case MOSQ_ERR_NOT_FOUND: return "MOSQ_ERR_NOT_FOUND";
105 case MOSQ_ERR_CONN_LOST: return "MOSQ_ERR_CONN_LOST";
106 case MOSQ_ERR_SSL: return "MOSQ_ERR_SSL";
107 case MOSQ_ERR_PAYLOAD_SIZE: return "MOSQ_ERR_PAYLOAD_SIZE";
108 case MOSQ_ERR_NOT_SUPPORTED: return "MOSQ_ERR_NOT_SUPPORTED";
109 case MOSQ_ERR_AUTH: return "MOSQ_ERR_AUTH";
110 case MOSQ_ERR_ACL_DENIED: return "MOSQ_ERR_ACL_DENIED";
111 case MOSQ_ERR_UNKNOWN: return "MOSQ_ERR_UNKNOWN";
112 case MOSQ_ERR_ERRNO: return "MOSQ_ERR_ERRNO";
115 return "UNKNOWN ERROR CODE";
118 /* provided by libmosquitto */
121 static void mqtt_free (mqtt_client_conf_t *conf)
127 (void) mosquitto_disconnect (conf->mosq);
129 (void) mosquitto_destroy (conf->mosq);
132 sfree (conf->username);
133 sfree (conf->password);
134 sfree (conf->client_id);
135 sfree (conf->topic_prefix);
139 static char *strip_prefix (char *topic)
145 for (i = 0; topic[i] != 0; i++)
154 char *tmp = strchr (topic, '/');
164 static void on_message (
165 #if LIBMOSQUITTO_MAJOR == 0
167 __attribute__((unused)) struct mosquitto *m,
169 __attribute__((unused)) void *arg,
170 const struct mosquitto_message *msg)
172 value_list_t vl = VALUE_LIST_INIT;
173 data_set_t const *ds;
179 if (msg->payloadlen <= 0) {
180 DEBUG ("mqtt plugin: message has empty payload");
184 topic = strdup (msg->topic);
185 name = strip_prefix (topic);
187 status = parse_identifier_vl (name, &vl);
190 ERROR ("mqtt plugin: Unable to parse topic \"%s\".", topic);
196 ds = plugin_get_ds (vl.type);
199 ERROR ("mqtt plugin: Unknown type: \"%s\".", vl.type);
203 vl.values = calloc (ds->ds_num, sizeof (*vl.values));
204 if (vl.values == NULL)
206 ERROR ("mqtt plugin: calloc failed.");
209 vl.values_len = ds->ds_num;
211 payload = malloc (msg->payloadlen+1);
214 ERROR ("mqtt plugin: malloc for payload buffer failed.");
217 memmove (payload, msg->payload, msg->payloadlen);
218 payload[msg->payloadlen] = 0;
220 DEBUG ("mqtt plugin: payload = \"%s\"", payload);
221 status = parse_values (payload, &vl, ds);
224 ERROR ("mqtt plugin: Unable to parse payload \"%s\".", payload);
231 plugin_dispatch_values (&vl);
233 } /* void on_message */
235 /* must hold conf->lock when calling. */
236 static int mqtt_reconnect (mqtt_client_conf_t *conf)
243 status = mosquitto_reconnect (conf->mosq);
244 if (status != MOSQ_ERR_SUCCESS)
247 ERROR ("mqtt_connect_broker: mosquitto_connect failed: %s",
248 (status == MOSQ_ERR_ERRNO)
249 ? sstrerror(errno, errbuf, sizeof (errbuf))
250 : mosquitto_strerror (status));
257 &conf->complaint_cantpublish,
258 "mqtt plugin: successfully reconnected to broker \"%s:%d\"",
259 conf->host, conf->port);
262 } /* mqtt_reconnect */
264 /* must hold conf->lock when calling. */
265 static int mqtt_connect (mqtt_client_conf_t *conf)
267 char const *client_id;
270 if (conf->mosq != NULL)
271 return mqtt_reconnect (conf);
274 client_id = conf->client_id;
276 client_id = hostname_g;
278 #if LIBMOSQUITTO_MAJOR == 0
279 conf->mosq = mosquitto_new (client_id, /* user data = */ conf);
281 conf->mosq = mosquitto_new (client_id, conf->clean_session, /* user data = */ conf);
283 if (conf->mosq == NULL)
285 ERROR ("mqtt plugin: mosquitto_new failed");
289 if (conf->username && conf->password)
291 status = mosquitto_username_pw_set (conf->mosq, conf->username, conf->password);
292 if (status != MOSQ_ERR_SUCCESS)
295 ERROR ("mqtt plugin: mosquitto_username_pw_set failed: %s",
296 (status == MOSQ_ERR_ERRNO)
297 ? sstrerror (errno, errbuf, sizeof (errbuf))
298 : mosquitto_strerror (status));
300 mosquitto_destroy (conf->mosq);
306 #if LIBMOSQUITTO_MAJOR == 0
307 status = mosquitto_connect (conf->mosq, conf->host, conf->port,
308 /* keepalive = */ MQTT_KEEPALIVE, /* clean session = */ conf->clean_session);
310 status = mosquitto_connect (conf->mosq, conf->host, conf->port, MQTT_KEEPALIVE);
312 if (status != MOSQ_ERR_SUCCESS)
315 ERROR ("mqtt plugin: mosquitto_connect failed: %s",
316 (status == MOSQ_ERR_ERRNO)
317 ? sstrerror (errno, errbuf, sizeof (errbuf))
318 : mosquitto_strerror (status));
320 mosquitto_destroy (conf->mosq);
327 mosquitto_message_callback_set (conf->mosq, on_message);
329 status = mosquitto_subscribe (conf->mosq,
330 /* message_id = */ NULL,
331 conf->topic, conf->qos);
332 if (status != MOSQ_ERR_SUCCESS)
334 ERROR ("mqtt plugin: Subscribing to \"%s\" failed: %s",
335 conf->topic, mosquitto_strerror (status));
337 mosquitto_disconnect (conf->mosq);
338 mosquitto_destroy (conf->mosq);
348 static void *subscribers_thread (void *arg)
350 mqtt_client_conf_t *conf = arg;
357 status = mqtt_connect (conf);
364 /* The documentation says "0" would map to the default (1000ms), but
365 * that does not work on some versions. */
366 #if LIBMOSQUITTO_MAJOR == 0
367 status = mosquitto_loop (conf->mosq, /* timeout = */ 1000 /* ms */);
369 status = mosquitto_loop (conf->mosq,
370 /* timeout[ms] = */ 1000,
371 /* max_packets = */ 100);
373 if (status == MOSQ_ERR_CONN_LOST)
378 else if (status != MOSQ_ERR_SUCCESS)
380 ERROR ("mqtt plugin: mosquitto_loop failed: %s",
381 mosquitto_strerror (status));
382 mosquitto_destroy (conf->mosq);
388 DEBUG ("mqtt plugin: mosquitto_loop succeeded.");
389 } /* while (conf->loop) */
392 } /* void *subscribers_thread */
394 static int publish (mqtt_client_conf_t *conf, char const *topic,
395 void const *payload, size_t payload_len)
399 pthread_mutex_lock (&conf->lock);
401 status = mqtt_connect (conf);
403 pthread_mutex_unlock (&conf->lock);
404 ERROR ("mqtt plugin: unable to reconnect to broker");
408 status = mosquitto_publish(conf->mosq, /* message_id */ NULL, topic,
409 #if LIBMOSQUITTO_MAJOR == 0
410 (uint32_t) payload_len, payload,
412 (int) payload_len, payload,
414 conf->qos, conf->retain);
415 if (status != MOSQ_ERR_SUCCESS)
419 &conf->complaint_cantpublish,
420 "mqtt plugin: mosquitto_publish failed: %s",
421 (status == MOSQ_ERR_ERRNO)
422 ? sstrerror(errno, errbuf, sizeof (errbuf))
423 : mosquitto_strerror(status));
424 /* Mark our connection "down" regardless of the error as a safety
425 * measure; we will try to reconnect the next time we have to publish a
429 pthread_mutex_unlock (&conf->lock);
433 pthread_mutex_unlock (&conf->lock);
437 static int format_topic (char *buf, size_t buf_len,
438 data_set_t const *ds, value_list_t const *vl,
439 mqtt_client_conf_t *conf)
441 char name[MQTT_MAX_TOPIC_SIZE];
444 if ((conf->topic_prefix == NULL) || (conf->topic_prefix[0] == 0))
445 return (FORMAT_VL (buf, buf_len, vl));
447 status = FORMAT_VL (name, sizeof (name), vl);
451 status = ssnprintf (buf, buf_len, "%s/%s", conf->topic_prefix, name);
452 if ((status < 0) || (((size_t) status) >= buf_len))
456 } /* int format_topic */
458 static int mqtt_write (const data_set_t *ds, const value_list_t *vl,
459 user_data_t *user_data)
461 mqtt_client_conf_t *conf;
462 char topic[MQTT_MAX_TOPIC_SIZE];
463 char payload[MQTT_MAX_MESSAGE_SIZE];
466 if ((user_data == NULL) || (user_data->data == NULL))
468 conf = user_data->data;
470 status = format_topic (topic, sizeof (topic), ds, vl, conf);
473 ERROR ("mqtt plugin: format_topic failed with status %d.", status);
477 status = format_values (payload, sizeof (payload),
478 ds, vl, conf->store_rates);
481 ERROR ("mqtt plugin: format_values failed with status %d.", status);
485 status = publish (conf, topic, payload, strlen (payload) + 1);
488 ERROR ("mqtt plugin: publish failed: %s", mosquitto_strerror (status));
499 * ClientId "collectd"
508 static int mqtt_config_publisher (oconfig_item_t *ci)
510 mqtt_client_conf_t *conf;
512 user_data_t user_data;
516 conf = calloc (1, sizeof (*conf));
519 ERROR ("mqtt plugin: malloc failed.");
525 status = cf_util_get_string (ci, &conf->name);
532 conf->host = strdup (MQTT_DEFAULT_HOST);
533 conf->port = MQTT_DEFAULT_PORT;
534 conf->client_id = NULL;
536 conf->topic_prefix = strdup (MQTT_DEFAULT_TOPIC_PREFIX);
537 conf->store_rates = 1;
539 status = pthread_mutex_init (&conf->lock, NULL);
546 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
548 for (i = 0; i < ci->children_num; i++)
550 oconfig_item_t *child = ci->children + i;
551 if (strcasecmp ("Host", child->key) == 0)
552 cf_util_get_string (child, &conf->host);
553 else if (strcasecmp ("Port", child->key) == 0)
555 int tmp = cf_util_get_port_number (child);
557 ERROR ("mqtt plugin: Invalid port number.");
561 else if (strcasecmp ("ClientId", child->key) == 0)
562 cf_util_get_string (child, &conf->client_id);
563 else if (strcasecmp ("User", child->key) == 0)
564 cf_util_get_string (child, &conf->username);
565 else if (strcasecmp ("Password", child->key) == 0)
566 cf_util_get_string (child, &conf->password);
567 else if (strcasecmp ("QoS", child->key) == 0)
570 status = cf_util_get_int (child, &tmp);
571 if ((status != 0) || (tmp < 0) || (tmp > 2))
572 ERROR ("mqtt plugin: Not a valid QoS setting.");
576 else if (strcasecmp ("Prefix", child->key) == 0)
577 cf_util_get_string (child, &conf->topic_prefix);
578 else if (strcasecmp ("StoreRates", child->key) == 0)
579 cf_util_get_boolean (child, &conf->store_rates);
580 else if (strcasecmp ("Retain", child->key) == 0)
581 cf_util_get_boolean (child, &conf->retain);
583 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
586 ssnprintf (cb_name, sizeof (cb_name), "mqtt/%s", conf->name);
587 memset (&user_data, 0, sizeof (user_data));
588 user_data.data = conf;
590 plugin_register_write (cb_name, mqtt_write, &user_data);
592 } /* mqtt_config_publisher */
598 * ClientId "collectd"
604 static int mqtt_config_subscriber (oconfig_item_t *ci)
606 mqtt_client_conf_t **tmp;
607 mqtt_client_conf_t *conf;
611 conf = calloc (1, sizeof (*conf));
614 ERROR ("mqtt plugin: malloc failed.");
620 status = cf_util_get_string (ci, &conf->name);
627 conf->host = strdup (MQTT_DEFAULT_HOST);
628 conf->port = MQTT_DEFAULT_PORT;
629 conf->client_id = NULL;
631 conf->topic = strdup (MQTT_DEFAULT_TOPIC);
632 conf->clean_session = 1;
634 status = pthread_mutex_init (&conf->lock, NULL);
641 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
643 for (i = 0; i < ci->children_num; i++)
645 oconfig_item_t *child = ci->children + i;
646 if (strcasecmp ("Host", child->key) == 0)
647 cf_util_get_string (child, &conf->host);
648 else if (strcasecmp ("Port", child->key) == 0)
650 int tmp = cf_util_get_port_number (child);
652 ERROR ("mqtt plugin: Invalid port number.");
656 else if (strcasecmp ("ClientId", child->key) == 0)
657 cf_util_get_string (child, &conf->client_id);
658 else if (strcasecmp ("User", child->key) == 0)
659 cf_util_get_string (child, &conf->username);
660 else if (strcasecmp ("Password", child->key) == 0)
661 cf_util_get_string (child, &conf->password);
662 else if (strcasecmp ("QoS", child->key) == 0)
665 status = cf_util_get_int (child, &tmp);
666 if ((status != 0) || (tmp < 0) || (tmp > 2))
667 ERROR ("mqtt plugin: Not a valid QoS setting.");
671 else if (strcasecmp ("Topic", child->key) == 0)
672 cf_util_get_string (child, &conf->topic);
673 else if (strcasecmp ("CleanSession", child->key) == 0)
674 cf_util_get_boolean (child, &conf->clean_session);
676 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
679 tmp = realloc (subscribers, sizeof (*subscribers) * subscribers_num);
682 ERROR ("mqtt plugin: realloc failed.");
687 subscribers[subscribers_num] = conf;
691 } /* mqtt_config_subscriber */
703 static int mqtt_config (oconfig_item_t *ci)
707 for (i = 0; i < ci->children_num; i++)
709 oconfig_item_t *child = ci->children + i;
711 if (strcasecmp ("Publish", child->key) == 0)
712 mqtt_config_publisher (child);
713 else if (strcasecmp ("Subscribe", child->key) == 0)
714 mqtt_config_subscriber (child);
716 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
720 } /* int mqtt_config */
722 static int mqtt_init (void)
726 mosquitto_lib_init ();
728 for (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]);
742 ERROR ("mqtt plugin: pthread_create failed: %s",
743 sstrerror (errno, errbuf, sizeof (errbuf)));
751 void module_register (void)
753 plugin_register_complex_config ("mqtt", mqtt_config);
754 plugin_register_init ("mqtt", mqtt_init);
755 } /* void module_register */
757 /* vim: set sw=4 sts=4 et fdm=marker : */