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"
34 #include "common.h"
35 #include "plugin.h"
36 #include "utils_cache.h"
37 #include "utils_complain.h"
39 #include <pthread.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
51 #endif
52 #ifndef SSL_VERIFY_PEER
53 # define SSL_VERIFY_PEER 1
54 #endif
57 /*
58 * Data types
59 */
60 struct mqtt_client_conf
61 {
62 _Bool publish;
63 char *name;
65 struct mosquitto *mosq;
66 _Bool connected;
68 char *host;
69 int port;
70 char *client_id;
71 char *username;
72 char *password;
73 int qos;
74 char *cacertificatefile;
75 char *certificatefile;
76 char *certificatekeyfile;
77 char *tlsprotocol;
78 char *ciphersuite;
80 /* For publishing */
81 char *topic_prefix;
82 _Bool store_rates;
83 _Bool retain;
85 /* For subscribing */
86 pthread_t thread;
87 _Bool loop;
88 char *topic;
89 _Bool clean_session;
91 c_complain_t complaint_cantpublish;
92 pthread_mutex_t lock;
93 };
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;
99 /*
100 * Functions
101 */
102 #if LIBMOSQUITTO_MAJOR == 0
103 static char const *mosquitto_strerror (int code)
104 {
105 switch (code)
106 {
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";
122 }
124 return "UNKNOWN ERROR CODE";
125 }
126 #else
127 /* provided by libmosquitto */
128 #endif
130 static void mqtt_free (mqtt_client_conf_t *conf)
131 {
132 if (conf == NULL)
133 return;
135 if (conf->connected)
136 (void) mosquitto_disconnect (conf->mosq);
137 conf->connected = 0;
138 (void) mosquitto_destroy (conf->mosq);
140 sfree (conf->host);
141 sfree (conf->username);
142 sfree (conf->password);
143 sfree (conf->client_id);
144 sfree (conf->topic_prefix);
145 sfree (conf);
146 }
148 static char *strip_prefix (char *topic)
149 {
150 size_t num;
151 size_t i;
153 num = 0;
154 for (i = 0; topic[i] != 0; i++)
155 if (topic[i] == '/')
156 num++;
158 if (num < 2)
159 return (NULL);
161 while (num > 2)
162 {
163 char *tmp = strchr (topic, '/');
164 if (tmp == NULL)
165 return (NULL);
166 topic = tmp + 1;
167 num--;
168 }
170 return (topic);
171 }
173 static void on_message (
174 #if LIBMOSQUITTO_MAJOR == 0
175 #else
176 __attribute__((unused)) struct mosquitto *m,
177 #endif
178 __attribute__((unused)) void *arg,
179 const struct mosquitto_message *msg)
180 {
181 value_list_t vl = VALUE_LIST_INIT;
182 data_set_t const *ds;
183 char *topic;
184 char *name;
185 char *payload;
186 int status;
188 if (msg->payloadlen <= 0) {
189 DEBUG ("mqtt plugin: message has empty payload");
190 return;
191 }
193 topic = strdup (msg->topic);
194 name = strip_prefix (topic);
196 status = parse_identifier_vl (name, &vl);
197 if (status != 0)
198 {
199 ERROR ("mqtt plugin: Unable to parse topic \"%s\".", topic);
200 sfree (topic);
201 return;
202 }
203 sfree (topic);
205 ds = plugin_get_ds (vl.type);
206 if (ds == NULL)
207 {
208 ERROR ("mqtt plugin: Unknown type: \"%s\".", vl.type);
209 return;
210 }
212 vl.values = calloc (ds->ds_num, sizeof (*vl.values));
213 if (vl.values == NULL)
214 {
215 ERROR ("mqtt plugin: calloc failed.");
216 return;
217 }
218 vl.values_len = ds->ds_num;
220 payload = malloc (msg->payloadlen+1);
221 if (payload == NULL)
222 {
223 ERROR ("mqtt plugin: malloc for payload buffer failed.");
224 sfree (vl.values);
225 return;
226 }
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);
232 if (status != 0)
233 {
234 ERROR ("mqtt plugin: Unable to parse payload \"%s\".", payload);
235 sfree (payload);
236 sfree (vl.values);
237 return;
238 }
239 sfree (payload);
241 plugin_dispatch_values (&vl);
242 sfree (vl.values);
243 } /* void on_message */
245 /* must hold conf->lock when calling. */
246 static int mqtt_reconnect (mqtt_client_conf_t *conf)
247 {
248 int status;
250 if (conf->connected)
251 return (0);
253 status = mosquitto_reconnect (conf->mosq);
254 if (status != MOSQ_ERR_SUCCESS)
255 {
256 char errbuf[1024];
257 ERROR ("mqtt_connect_broker: mosquitto_connect failed: %s",
258 (status == MOSQ_ERR_ERRNO)
259 ? sstrerror(errno, errbuf, sizeof (errbuf))
260 : mosquitto_strerror (status));
261 return (-1);
262 }
264 conf->connected = 1;
266 c_release (LOG_INFO,
267 &conf->complaint_cantpublish,
268 "mqtt plugin: successfully reconnected to broker \"%s:%d\"",
269 conf->host, conf->port);
271 return (0);
272 } /* mqtt_reconnect */
274 /* must hold conf->lock when calling. */
275 static int mqtt_connect (mqtt_client_conf_t *conf)
276 {
277 char const *client_id;
278 int status;
280 if (conf->mosq != NULL)
281 return mqtt_reconnect (conf);
283 if (conf->client_id)
284 client_id = conf->client_id;
285 else
286 client_id = hostname_g;
288 #if LIBMOSQUITTO_MAJOR == 0
289 conf->mosq = mosquitto_new (client_id, /* user data = */ conf);
290 #else
291 conf->mosq = mosquitto_new (client_id, conf->clean_session, /* user data = */ conf);
292 #endif
293 if (conf->mosq == NULL)
294 {
295 ERROR ("mqtt plugin: mosquitto_new failed");
296 return (-1);
297 }
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);
306 conf->mosq = NULL;
307 return (-1);
308 }
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);
314 conf->mosq = NULL;
315 return (-1);
316 }
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);
322 conf->mosq = NULL;
323 return (-1);
324 }
325 }
326 #endif
328 if (conf->username && conf->password)
329 {
330 status = mosquitto_username_pw_set (conf->mosq, conf->username, conf->password);
331 if (status != MOSQ_ERR_SUCCESS)
332 {
333 char errbuf[1024];
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);
340 conf->mosq = NULL;
341 return (-1);
342 }
343 }
345 #if LIBMOSQUITTO_MAJOR == 0
346 status = mosquitto_connect (conf->mosq, conf->host, conf->port,
347 /* keepalive = */ MQTT_KEEPALIVE, /* clean session = */ conf->clean_session);
348 #else
349 status = mosquitto_connect (conf->mosq, conf->host, conf->port, MQTT_KEEPALIVE);
350 #endif
351 if (status != MOSQ_ERR_SUCCESS)
352 {
353 char errbuf[1024];
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);
360 conf->mosq = NULL;
361 return (-1);
362 }
364 if (!conf->publish)
365 {
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)
372 {
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);
378 conf->mosq = NULL;
379 return (-1);
380 }
381 }
383 conf->connected = 1;
384 return (0);
385 } /* mqtt_connect */
387 static void *subscribers_thread (void *arg)
388 {
389 mqtt_client_conf_t *conf = arg;
390 int status;
392 conf->loop = 1;
394 while (conf->loop)
395 {
396 status = mqtt_connect (conf);
397 if (status != 0)
398 {
399 sleep (1);
400 continue;
401 }
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 */);
407 #else
408 status = mosquitto_loop (conf->mosq,
409 /* timeout[ms] = */ 1000,
410 /* max_packets = */ 100);
411 #endif
412 if (status == MOSQ_ERR_CONN_LOST)
413 {
414 conf->connected = 0;
415 continue;
416 }
417 else if (status != MOSQ_ERR_SUCCESS)
418 {
419 ERROR ("mqtt plugin: mosquitto_loop failed: %s",
420 mosquitto_strerror (status));
421 mosquitto_destroy (conf->mosq);
422 conf->mosq = NULL;
423 conf->connected = 0;
424 continue;
425 }
427 DEBUG ("mqtt plugin: mosquitto_loop succeeded.");
428 } /* while (conf->loop) */
430 pthread_exit (0);
431 } /* void *subscribers_thread */
433 static int publish (mqtt_client_conf_t *conf, char const *topic,
434 void const *payload, size_t payload_len)
435 {
436 int status;
438 pthread_mutex_lock (&conf->lock);
440 status = mqtt_connect (conf);
441 if (status != 0) {
442 pthread_mutex_unlock (&conf->lock);
443 ERROR ("mqtt plugin: unable to reconnect to broker");
444 return (status);
445 }
447 status = mosquitto_publish(conf->mosq, /* message_id */ NULL, topic,
448 #if LIBMOSQUITTO_MAJOR == 0
449 (uint32_t) payload_len, payload,
450 #else
451 (int) payload_len, payload,
452 #endif
453 conf->qos, conf->retain);
454 if (status != MOSQ_ERR_SUCCESS)
455 {
456 char errbuf[1024];
457 c_complain (LOG_ERR,
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
465 * message */
466 conf->connected = 0;
468 pthread_mutex_unlock (&conf->lock);
469 return (-1);
470 }
472 pthread_mutex_unlock (&conf->lock);
473 return (0);
474 } /* int publish */
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)
479 {
480 char name[MQTT_MAX_TOPIC_SIZE];
481 int status;
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);
487 if (status != 0)
488 return (status);
490 status = ssnprintf (buf, buf_len, "%s/%s", conf->topic_prefix, name);
491 if ((status < 0) || (((size_t) status) >= buf_len))
492 return (ENOMEM);
494 return (0);
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)
499 {
500 mqtt_client_conf_t *conf;
501 char topic[MQTT_MAX_TOPIC_SIZE];
502 char payload[MQTT_MAX_MESSAGE_SIZE];
503 int status = 0;
505 if ((user_data == NULL) || (user_data->data == NULL))
506 return (EINVAL);
507 conf = user_data->data;
509 status = format_topic (topic, sizeof (topic), ds, vl, conf);
510 if (status != 0)
511 {
512 ERROR ("mqtt plugin: format_topic failed with status %d.", status);
513 return (status);
514 }
516 status = format_values (payload, sizeof (payload),
517 ds, vl, conf->store_rates);
518 if (status != 0)
519 {
520 ERROR ("mqtt plugin: format_values failed with status %d.", status);
521 return (status);
522 }
524 status = publish (conf, topic, payload, strlen (payload) + 1);
525 if (status != 0)
526 {
527 ERROR ("mqtt plugin: publish failed: %s", mosquitto_strerror (status));
528 return (status);
529 }
531 return (status);
532 } /* mqtt_write */
534 /*
535 * <Publish "name">
536 * Host "example.com"
537 * Port 1883
538 * ClientId "collectd"
539 * User "guest"
540 * Password "secret"
541 * Prefix "collectd"
542 * StoreRates true
543 * Retain false
544 * QoS 0
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
549 * </Publish>
550 */
551 static int mqtt_config_publisher (oconfig_item_t *ci)
552 {
553 mqtt_client_conf_t *conf;
554 char cb_name[1024];
555 user_data_t user_data;
556 int status;
557 int i;
559 conf = calloc (1, sizeof (*conf));
560 if (conf == NULL)
561 {
562 ERROR ("mqtt plugin: calloc failed.");
563 return (-1);
564 }
565 conf->publish = 1;
567 conf->name = NULL;
568 status = cf_util_get_string (ci, &conf->name);
569 if (status != 0)
570 {
571 mqtt_free (conf);
572 return (status);
573 }
575 conf->host = strdup (MQTT_DEFAULT_HOST);
576 conf->port = MQTT_DEFAULT_PORT;
577 conf->client_id = NULL;
578 conf->qos = 0;
579 conf->topic_prefix = strdup (MQTT_DEFAULT_TOPIC_PREFIX);
580 conf->store_rates = 1;
582 status = pthread_mutex_init (&conf->lock, NULL);
583 if (status != 0)
584 {
585 mqtt_free (conf);
586 return (status);
587 }
589 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
591 for (i = 0; i < ci->children_num; i++)
592 {
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)
597 {
598 int tmp = cf_util_get_port_number (child);
599 if (tmp < 0)
600 ERROR ("mqtt plugin: Invalid port number.");
601 else
602 conf->port = tmp;
603 }
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)
611 {
612 int tmp = -1;
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.");
616 else
617 conf->qos = tmp;
618 }
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);
635 else
636 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
637 }
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);
644 return (0);
645 } /* mqtt_config_publisher */
647 /*
648 * <Subscribe "name">
649 * Host "example.com"
650 * Port 1883
651 * ClientId "collectd"
652 * User "guest"
653 * Password "secret"
654 * Topic "collectd/#"
655 * </Subscribe>
656 */
657 static int mqtt_config_subscriber (oconfig_item_t *ci)
658 {
659 mqtt_client_conf_t **tmp;
660 mqtt_client_conf_t *conf;
661 int status;
662 int i;
664 conf = calloc (1, sizeof (*conf));
665 if (conf == NULL)
666 {
667 ERROR ("mqtt plugin: calloc failed.");
668 return (-1);
669 }
670 conf->publish = 0;
672 conf->name = NULL;
673 status = cf_util_get_string (ci, &conf->name);
674 if (status != 0)
675 {
676 mqtt_free (conf);
677 return (status);
678 }
680 conf->host = strdup (MQTT_DEFAULT_HOST);
681 conf->port = MQTT_DEFAULT_PORT;
682 conf->client_id = NULL;
683 conf->qos = 2;
684 conf->topic = strdup (MQTT_DEFAULT_TOPIC);
685 conf->clean_session = 1;
687 status = pthread_mutex_init (&conf->lock, NULL);
688 if (status != 0)
689 {
690 mqtt_free (conf);
691 return (status);
692 }
694 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
696 for (i = 0; i < ci->children_num; i++)
697 {
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)
702 {
703 status = cf_util_get_port_number (child);
704 if (status < 0)
705 ERROR ("mqtt plugin: Invalid port number.");
706 else
707 conf->port = status;
708 }
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)
716 {
717 int qos = -1;
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.");
721 else
722 conf->qos = qos;
723 }
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);
728 else
729 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
730 }
732 tmp = realloc (subscribers, sizeof (*subscribers) * (subscribers_num + 1) );
733 if (tmp == NULL)
734 {
735 ERROR ("mqtt plugin: realloc failed.");
736 mqtt_free (conf);
737 return (-1);
738 }
739 subscribers = tmp;
740 subscribers[subscribers_num] = conf;
741 subscribers_num++;
743 return (0);
744 } /* mqtt_config_subscriber */
746 /*
747 * <Plugin mqtt>
748 * <Publish "name">
749 * # ...
750 * </Publish>
751 * <Subscribe "name">
752 * # ...
753 * </Subscribe>
754 * </Plugin>
755 */
756 static int mqtt_config (oconfig_item_t *ci)
757 {
758 int i;
760 for (i = 0; i < ci->children_num; i++)
761 {
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);
768 else
769 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
770 }
772 return (0);
773 } /* int mqtt_config */
775 static int mqtt_init (void)
776 {
777 size_t i;
779 mosquitto_lib_init ();
781 for (i = 0; i < subscribers_num; i++)
782 {
783 int status;
785 if (subscribers[i]->loop)
786 continue;
788 status = plugin_thread_create (&subscribers[i]->thread,
789 /* attrs = */ NULL,
790 /* func = */ subscribers_thread,
791 /* args = */ subscribers[i]);
792 if (status != 0)
793 {
794 char errbuf[1024];
795 ERROR ("mqtt plugin: pthread_create failed: %s",
796 sstrerror (errno, errbuf, sizeof (errbuf)));
797 continue;
798 }
799 }
801 return (0);
802 } /* mqtt_init */
804 void module_register (void)
805 {
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 : */