1 /**
2 * collectd - src/mqtt.c
3 * Copyright (C) 2014 Marc Falzon
4 * Copyright (C) 2014,2015 Florian octo Forster
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 *
24 * Authors:
25 * Marc Falzon <marc at baha dot mu>
26 * Florian octo Forster <octo at collectd.org>
27 * Jan-Piet Mens <jpmens at gmail.com>
28 **/
30 // Reference: http://mosquitto.org/api/files/mosquitto-h.html
33 #include "collectd.h"
35 #include "common.h"
36 #include "plugin.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
49 #endif
50 #ifndef SSL_VERIFY_PEER
51 # define SSL_VERIFY_PEER 1
52 #endif
55 /*
56 * Data types
57 */
58 struct mqtt_client_conf
59 {
60 _Bool publish;
61 char *name;
63 struct mosquitto *mosq;
64 _Bool connected;
66 char *host;
67 int port;
68 char *client_id;
69 char *username;
70 char *password;
71 int qos;
72 char *cacertificatefile;
73 char *certificatefile;
74 char *certificatekeyfile;
75 char *tlsprotocol;
76 char *ciphersuite;
78 /* For publishing */
79 char *topic_prefix;
80 _Bool store_rates;
81 _Bool retain;
83 /* For subscribing */
84 pthread_t thread;
85 _Bool loop;
86 char *topic;
87 _Bool clean_session;
89 c_complain_t complaint_cantpublish;
90 pthread_mutex_t lock;
91 };
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;
97 /*
98 * Functions
99 */
100 #if LIBMOSQUITTO_MAJOR == 0
101 static char const *mosquitto_strerror (int code)
102 {
103 switch (code)
104 {
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";
120 }
122 return "UNKNOWN ERROR CODE";
123 }
124 #else
125 /* provided by libmosquitto */
126 #endif
128 static void mqtt_free (mqtt_client_conf_t *conf)
129 {
130 if (conf == NULL)
131 return;
133 if (conf->connected)
134 (void) mosquitto_disconnect (conf->mosq);
135 conf->connected = 0;
136 (void) mosquitto_destroy (conf->mosq);
138 sfree (conf->host);
139 sfree (conf->username);
140 sfree (conf->password);
141 sfree (conf->client_id);
142 sfree (conf->topic_prefix);
143 sfree (conf);
144 }
146 static char *strip_prefix (char *topic)
147 {
148 size_t num = 0;
150 for (size_t i = 0; topic[i] != 0; i++)
151 if (topic[i] == '/')
152 num++;
154 if (num < 2)
155 return (NULL);
157 while (num > 2)
158 {
159 char *tmp = strchr (topic, '/');
160 if (tmp == NULL)
161 return (NULL);
162 topic = tmp + 1;
163 num--;
164 }
166 return (topic);
167 }
169 static void on_message (
170 #if LIBMOSQUITTO_MAJOR == 0
171 #else
172 __attribute__((unused)) struct mosquitto *m,
173 #endif
174 __attribute__((unused)) void *arg,
175 const struct mosquitto_message *msg)
176 {
177 value_list_t vl = VALUE_LIST_INIT;
178 data_set_t const *ds;
179 char *topic;
180 char *name;
181 char *payload;
182 int status;
184 if (msg->payloadlen <= 0) {
185 DEBUG ("mqtt plugin: message has empty payload");
186 return;
187 }
189 topic = strdup (msg->topic);
190 name = strip_prefix (topic);
192 status = parse_identifier_vl (name, &vl);
193 if (status != 0)
194 {
195 ERROR ("mqtt plugin: Unable to parse topic \"%s\".", topic);
196 sfree (topic);
197 return;
198 }
199 sfree (topic);
201 ds = plugin_get_ds (vl.type);
202 if (ds == NULL)
203 {
204 ERROR ("mqtt plugin: Unknown type: \"%s\".", vl.type);
205 return;
206 }
208 vl.values = calloc (ds->ds_num, sizeof (*vl.values));
209 if (vl.values == NULL)
210 {
211 ERROR ("mqtt plugin: calloc failed.");
212 return;
213 }
214 vl.values_len = ds->ds_num;
216 payload = malloc (msg->payloadlen+1);
217 if (payload == NULL)
218 {
219 ERROR ("mqtt plugin: malloc for payload buffer failed.");
220 sfree (vl.values);
221 return;
222 }
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);
228 if (status != 0)
229 {
230 ERROR ("mqtt plugin: Unable to parse payload \"%s\".", payload);
231 sfree (payload);
232 sfree (vl.values);
233 return;
234 }
235 sfree (payload);
237 plugin_dispatch_values (&vl);
238 sfree (vl.values);
239 } /* void on_message */
241 /* must hold conf->lock when calling. */
242 static int mqtt_reconnect (mqtt_client_conf_t *conf)
243 {
244 int status;
246 if (conf->connected)
247 return (0);
249 status = mosquitto_reconnect (conf->mosq);
250 if (status != MOSQ_ERR_SUCCESS)
251 {
252 char errbuf[1024];
253 ERROR ("mqtt_connect_broker: mosquitto_connect failed: %s",
254 (status == MOSQ_ERR_ERRNO)
255 ? sstrerror(errno, errbuf, sizeof (errbuf))
256 : mosquitto_strerror (status));
257 return (-1);
258 }
260 conf->connected = 1;
262 c_release (LOG_INFO,
263 &conf->complaint_cantpublish,
264 "mqtt plugin: successfully reconnected to broker \"%s:%d\"",
265 conf->host, conf->port);
267 return (0);
268 } /* mqtt_reconnect */
270 /* must hold conf->lock when calling. */
271 static int mqtt_connect (mqtt_client_conf_t *conf)
272 {
273 char const *client_id;
274 int status;
276 if (conf->mosq != NULL)
277 return mqtt_reconnect (conf);
279 if (conf->client_id)
280 client_id = conf->client_id;
281 else
282 client_id = hostname_g;
284 #if LIBMOSQUITTO_MAJOR == 0
285 conf->mosq = mosquitto_new (client_id, /* user data = */ conf);
286 #else
287 conf->mosq = mosquitto_new (client_id, conf->clean_session, /* user data = */ conf);
288 #endif
289 if (conf->mosq == NULL)
290 {
291 ERROR ("mqtt plugin: mosquitto_new failed");
292 return (-1);
293 }
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);
302 conf->mosq = NULL;
303 return (-1);
304 }
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);
310 conf->mosq = NULL;
311 return (-1);
312 }
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);
318 conf->mosq = NULL;
319 return (-1);
320 }
321 }
322 #endif
324 if (conf->username && conf->password)
325 {
326 status = mosquitto_username_pw_set (conf->mosq, conf->username, conf->password);
327 if (status != MOSQ_ERR_SUCCESS)
328 {
329 char errbuf[1024];
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);
336 conf->mosq = NULL;
337 return (-1);
338 }
339 }
341 #if LIBMOSQUITTO_MAJOR == 0
342 status = mosquitto_connect (conf->mosq, conf->host, conf->port,
343 /* keepalive = */ MQTT_KEEPALIVE, /* clean session = */ conf->clean_session);
344 #else
345 status = mosquitto_connect (conf->mosq, conf->host, conf->port, MQTT_KEEPALIVE);
346 #endif
347 if (status != MOSQ_ERR_SUCCESS)
348 {
349 char errbuf[1024];
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);
356 conf->mosq = NULL;
357 return (-1);
358 }
360 if (!conf->publish)
361 {
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)
368 {
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);
374 conf->mosq = NULL;
375 return (-1);
376 }
377 }
379 conf->connected = 1;
380 return (0);
381 } /* mqtt_connect */
383 static void *subscribers_thread (void *arg)
384 {
385 mqtt_client_conf_t *conf = arg;
386 int status;
388 conf->loop = 1;
390 while (conf->loop)
391 {
392 status = mqtt_connect (conf);
393 if (status != 0)
394 {
395 sleep (1);
396 continue;
397 }
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 */);
403 #else
404 status = mosquitto_loop (conf->mosq,
405 /* timeout[ms] = */ 1000,
406 /* max_packets = */ 100);
407 #endif
408 if (status == MOSQ_ERR_CONN_LOST)
409 {
410 conf->connected = 0;
411 continue;
412 }
413 else if (status != MOSQ_ERR_SUCCESS)
414 {
415 ERROR ("mqtt plugin: mosquitto_loop failed: %s",
416 mosquitto_strerror (status));
417 mosquitto_destroy (conf->mosq);
418 conf->mosq = NULL;
419 conf->connected = 0;
420 continue;
421 }
423 DEBUG ("mqtt plugin: mosquitto_loop succeeded.");
424 } /* while (conf->loop) */
426 pthread_exit (0);
427 } /* void *subscribers_thread */
429 static int publish (mqtt_client_conf_t *conf, char const *topic,
430 void const *payload, size_t payload_len)
431 {
432 int status;
434 pthread_mutex_lock (&conf->lock);
436 status = mqtt_connect (conf);
437 if (status != 0) {
438 pthread_mutex_unlock (&conf->lock);
439 ERROR ("mqtt plugin: unable to reconnect to broker");
440 return (status);
441 }
443 status = mosquitto_publish(conf->mosq, /* message_id */ NULL, topic,
444 #if LIBMOSQUITTO_MAJOR == 0
445 (uint32_t) payload_len, payload,
446 #else
447 (int) payload_len, payload,
448 #endif
449 conf->qos, conf->retain);
450 if (status != MOSQ_ERR_SUCCESS)
451 {
452 char errbuf[1024];
453 c_complain (LOG_ERR,
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
461 * message */
462 conf->connected = 0;
464 pthread_mutex_unlock (&conf->lock);
465 return (-1);
466 }
468 pthread_mutex_unlock (&conf->lock);
469 return (0);
470 } /* int publish */
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)
475 {
476 char name[MQTT_MAX_TOPIC_SIZE];
477 int status;
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);
483 if (status != 0)
484 return (status);
486 status = ssnprintf (buf, buf_len, "%s/%s", conf->topic_prefix, name);
487 if ((status < 0) || (((size_t) status) >= buf_len))
488 return (ENOMEM);
490 return (0);
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)
495 {
496 mqtt_client_conf_t *conf;
497 char topic[MQTT_MAX_TOPIC_SIZE];
498 char payload[MQTT_MAX_MESSAGE_SIZE];
499 int status = 0;
501 if ((user_data == NULL) || (user_data->data == NULL))
502 return (EINVAL);
503 conf = user_data->data;
505 status = format_topic (topic, sizeof (topic), ds, vl, conf);
506 if (status != 0)
507 {
508 ERROR ("mqtt plugin: format_topic failed with status %d.", status);
509 return (status);
510 }
512 status = format_values (payload, sizeof (payload),
513 ds, vl, conf->store_rates);
514 if (status != 0)
515 {
516 ERROR ("mqtt plugin: format_values failed with status %d.", status);
517 return (status);
518 }
520 status = publish (conf, topic, payload, strlen (payload) + 1);
521 if (status != 0)
522 {
523 ERROR ("mqtt plugin: publish failed: %s", mosquitto_strerror (status));
524 return (status);
525 }
527 return (status);
528 } /* mqtt_write */
530 /*
531 * <Publish "name">
532 * Host "example.com"
533 * Port 1883
534 * ClientId "collectd"
535 * User "guest"
536 * Password "secret"
537 * Prefix "collectd"
538 * StoreRates true
539 * Retain false
540 * QoS 0
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
545 * </Publish>
546 */
547 static int mqtt_config_publisher (oconfig_item_t *ci)
548 {
549 mqtt_client_conf_t *conf;
550 char cb_name[1024];
551 int status;
553 conf = calloc (1, sizeof (*conf));
554 if (conf == NULL)
555 {
556 ERROR ("mqtt plugin: calloc failed.");
557 return (-1);
558 }
559 conf->publish = 1;
561 conf->name = NULL;
562 status = cf_util_get_string (ci, &conf->name);
563 if (status != 0)
564 {
565 mqtt_free (conf);
566 return (status);
567 }
569 conf->host = strdup (MQTT_DEFAULT_HOST);
570 conf->port = MQTT_DEFAULT_PORT;
571 conf->client_id = NULL;
572 conf->qos = 0;
573 conf->topic_prefix = strdup (MQTT_DEFAULT_TOPIC_PREFIX);
574 conf->store_rates = 1;
576 status = pthread_mutex_init (&conf->lock, NULL);
577 if (status != 0)
578 {
579 mqtt_free (conf);
580 return (status);
581 }
583 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
585 for (int i = 0; i < ci->children_num; i++)
586 {
587 oconfig_item_t *child = ci->children + i;
588 if (strcasecmp ("Host", child->key) == 0)
589 cf_util_get_string (child, &conf->host);
590 else if (strcasecmp ("Port", child->key) == 0)
591 {
592 int tmp = cf_util_get_port_number (child);
593 if (tmp < 0)
594 ERROR ("mqtt plugin: Invalid port number.");
595 else
596 conf->port = tmp;
597 }
598 else if (strcasecmp ("ClientId", child->key) == 0)
599 cf_util_get_string (child, &conf->client_id);
600 else if (strcasecmp ("User", child->key) == 0)
601 cf_util_get_string (child, &conf->username);
602 else if (strcasecmp ("Password", child->key) == 0)
603 cf_util_get_string (child, &conf->password);
604 else if (strcasecmp ("QoS", child->key) == 0)
605 {
606 int tmp = -1;
607 status = cf_util_get_int (child, &tmp);
608 if ((status != 0) || (tmp < 0) || (tmp > 2))
609 ERROR ("mqtt plugin: Not a valid QoS setting.");
610 else
611 conf->qos = tmp;
612 }
613 else if (strcasecmp ("Prefix", child->key) == 0)
614 cf_util_get_string (child, &conf->topic_prefix);
615 else if (strcasecmp ("StoreRates", child->key) == 0)
616 cf_util_get_boolean (child, &conf->store_rates);
617 else if (strcasecmp ("Retain", child->key) == 0)
618 cf_util_get_boolean (child, &conf->retain);
619 else if (strcasecmp ("CACert", child->key) == 0)
620 cf_util_get_string (child, &conf->cacertificatefile);
621 else if (strcasecmp ("CertificateFile", child->key) == 0)
622 cf_util_get_string (child, &conf->certificatefile);
623 else if (strcasecmp ("CertificateKeyFile", child->key) == 0)
624 cf_util_get_string (child, &conf->certificatekeyfile);
625 else if (strcasecmp ("TLSProtocol", child->key) == 0)
626 cf_util_get_string (child, &conf->tlsprotocol);
627 else if (strcasecmp ("CipherSuite", child->key) == 0)
628 cf_util_get_string (child, &conf->ciphersuite);
629 else
630 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
631 }
633 ssnprintf (cb_name, sizeof (cb_name), "mqtt/%s", conf->name);
634 user_data_t user_data = {
635 .data = conf
636 };
638 plugin_register_write (cb_name, mqtt_write, &user_data);
639 return (0);
640 } /* mqtt_config_publisher */
642 /*
643 * <Subscribe "name">
644 * Host "example.com"
645 * Port 1883
646 * ClientId "collectd"
647 * User "guest"
648 * Password "secret"
649 * Topic "collectd/#"
650 * </Subscribe>
651 */
652 static int mqtt_config_subscriber (oconfig_item_t *ci)
653 {
654 mqtt_client_conf_t **tmp;
655 mqtt_client_conf_t *conf;
656 int status;
658 conf = calloc (1, sizeof (*conf));
659 if (conf == NULL)
660 {
661 ERROR ("mqtt plugin: calloc failed.");
662 return (-1);
663 }
664 conf->publish = 0;
666 conf->name = NULL;
667 status = cf_util_get_string (ci, &conf->name);
668 if (status != 0)
669 {
670 mqtt_free (conf);
671 return (status);
672 }
674 conf->host = strdup (MQTT_DEFAULT_HOST);
675 conf->port = MQTT_DEFAULT_PORT;
676 conf->client_id = NULL;
677 conf->qos = 2;
678 conf->topic = strdup (MQTT_DEFAULT_TOPIC);
679 conf->clean_session = 1;
681 status = pthread_mutex_init (&conf->lock, NULL);
682 if (status != 0)
683 {
684 mqtt_free (conf);
685 return (status);
686 }
688 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
690 for (int i = 0; i < ci->children_num; i++)
691 {
692 oconfig_item_t *child = ci->children + i;
693 if (strcasecmp ("Host", child->key) == 0)
694 cf_util_get_string (child, &conf->host);
695 else if (strcasecmp ("Port", child->key) == 0)
696 {
697 status = cf_util_get_port_number (child);
698 if (status < 0)
699 ERROR ("mqtt plugin: Invalid port number.");
700 else
701 conf->port = status;
702 }
703 else if (strcasecmp ("ClientId", child->key) == 0)
704 cf_util_get_string (child, &conf->client_id);
705 else if (strcasecmp ("User", child->key) == 0)
706 cf_util_get_string (child, &conf->username);
707 else if (strcasecmp ("Password", child->key) == 0)
708 cf_util_get_string (child, &conf->password);
709 else if (strcasecmp ("QoS", child->key) == 0)
710 {
711 int qos = -1;
712 status = cf_util_get_int (child, &qos);
713 if ((status != 0) || (qos < 0) || (qos > 2))
714 ERROR ("mqtt plugin: Not a valid QoS setting.");
715 else
716 conf->qos = qos;
717 }
718 else if (strcasecmp ("Topic", child->key) == 0)
719 cf_util_get_string (child, &conf->topic);
720 else if (strcasecmp ("CleanSession", child->key) == 0)
721 cf_util_get_boolean (child, &conf->clean_session);
722 else
723 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
724 }
726 tmp = realloc (subscribers, sizeof (*subscribers) * (subscribers_num + 1) );
727 if (tmp == NULL)
728 {
729 ERROR ("mqtt plugin: realloc failed.");
730 mqtt_free (conf);
731 return (-1);
732 }
733 subscribers = tmp;
734 subscribers[subscribers_num] = conf;
735 subscribers_num++;
737 return (0);
738 } /* mqtt_config_subscriber */
740 /*
741 * <Plugin mqtt>
742 * <Publish "name">
743 * # ...
744 * </Publish>
745 * <Subscribe "name">
746 * # ...
747 * </Subscribe>
748 * </Plugin>
749 */
750 static int mqtt_config (oconfig_item_t *ci)
751 {
752 for (int i = 0; i < ci->children_num; i++)
753 {
754 oconfig_item_t *child = ci->children + i;
756 if (strcasecmp ("Publish", child->key) == 0)
757 mqtt_config_publisher (child);
758 else if (strcasecmp ("Subscribe", child->key) == 0)
759 mqtt_config_subscriber (child);
760 else
761 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
762 }
764 return (0);
765 } /* int mqtt_config */
767 static int mqtt_init (void)
768 {
769 mosquitto_lib_init ();
771 for (size_t i = 0; i < subscribers_num; i++)
772 {
773 int status;
775 if (subscribers[i]->loop)
776 continue;
778 status = plugin_thread_create (&subscribers[i]->thread,
779 /* attrs = */ NULL,
780 /* func = */ subscribers_thread,
781 /* args = */ subscribers[i]);
782 if (status != 0)
783 {
784 char errbuf[1024];
785 ERROR ("mqtt plugin: pthread_create failed: %s",
786 sstrerror (errno, errbuf, sizeof (errbuf)));
787 continue;
788 }
789 }
791 return (0);
792 } /* mqtt_init */
794 void module_register (void)
795 {
796 plugin_register_complex_config ("mqtt", mqtt_config);
797 plugin_register_init ("mqtt", mqtt_init);
798 } /* void module_register */
800 /* vim: set sw=4 sts=4 et fdm=marker : */