X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fmqtt.c;h=8bc412c9497213074a738ca7dfa355209c1872d1;hb=6286127470b83578c2889db546cfa32cb0fe0147;hp=7a7ba59dfb57b06d850ebb5f13619c5291b5f8c2;hpb=1f2b236c9aa0aec7c5ca9d69cc36d9f7742578e3;p=collectd.git diff --git a/src/mqtt.c b/src/mqtt.c index 7a7ba59d..8bc412c9 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -1,6 +1,7 @@ /** * collectd - src/mqtt.c - * Copyright (C) 2014 Marc Falzon + * Copyright (C) 2014 Marc Falzon + * Copyright (C) 2014,2015 Florian octo Forster * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -19,6 +20,11 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Marc Falzon + * Florian octo Forster + * Jan-Piet Mens **/ // Reference: http://mosquitto.org/api/files/mosquitto-h.html @@ -40,6 +46,13 @@ #define MQTT_DEFAULT_PORT 1883 #define MQTT_DEFAULT_TOPIC_PREFIX "collectd" #define MQTT_DEFAULT_TOPIC "collectd/#" +#ifndef MQTT_KEEPALIVE +# define MQTT_KEEPALIVE 60 +#endif +#ifndef SSL_VERIFY_PEER +# define SSL_VERIFY_PEER 1 +#endif + /* * Data types @@ -58,6 +71,11 @@ struct mqtt_client_conf char *username; char *password; int qos; + char *cacertificatefile; + char *certificatefile; + char *certificatekeyfile; + char *tlsprotocol; + char *ciphersuite; /* For publishing */ char *topic_prefix; @@ -81,6 +99,7 @@ static size_t subscribers_num = 0; /* * Functions */ +#if LIBMOSQUITTO_MAJOR == 0 static char const *mosquitto_strerror (int code) { switch (code) @@ -104,6 +123,9 @@ static char const *mosquitto_strerror (int code) return "UNKNOWN ERROR CODE"; } +#else +/* provided by libmosquitto */ +#endif static void mqtt_free (mqtt_client_conf_t *conf) { @@ -148,7 +170,12 @@ static char *strip_prefix (char *topic) return (topic); } -static void on_message (__attribute__((unused)) void *arg, +static void on_message ( +#if LIBMOSQUITTO_MAJOR == 0 +#else + __attribute__((unused)) struct mosquitto *m, +#endif + __attribute__((unused)) void *arg, const struct mosquitto_message *msg) { value_list_t vl = VALUE_LIST_INIT; @@ -158,8 +185,10 @@ static void on_message (__attribute__((unused)) void *arg, char *payload; int status; - if ((msg->payloadlen <= 0) || (msg->payload[msg->payloadlen - 1] != 0)) + if (msg->payloadlen <= 0) { + DEBUG ("mqtt plugin: message has empty payload"); return; + } topic = strdup (msg->topic); name = strip_prefix (topic); @@ -188,7 +217,16 @@ static void on_message (__attribute__((unused)) void *arg, } vl.values_len = ds->ds_num; - payload = strdup ((void *) msg->payload); + payload = malloc (msg->payloadlen+1); + if (payload == NULL) + { + ERROR ("mqtt plugin: malloc for payload buffer failed."); + sfree (vl.values); + return; + } + memmove (payload, msg->payload, msg->payloadlen); + payload[msg->payloadlen] = 0; + DEBUG ("mqtt plugin: payload = \"%s\"", payload); status = parse_values (payload, &vl, ds); if (status != 0) @@ -247,13 +285,46 @@ static int mqtt_connect (mqtt_client_conf_t *conf) else client_id = hostname_g; +#if LIBMOSQUITTO_MAJOR == 0 conf->mosq = mosquitto_new (client_id, /* user data = */ conf); +#else + conf->mosq = mosquitto_new (client_id, conf->clean_session, /* user data = */ conf); +#endif if (conf->mosq == NULL) { ERROR ("mqtt plugin: mosquitto_new failed"); return (-1); } +#if LIBMOSQUITTO_MAJOR != 0 + if (conf->cacertificatefile) { + status = mosquitto_tls_set(conf->mosq, conf->cacertificatefile, NULL, + conf->certificatefile, conf->certificatekeyfile, /* pw_callback */NULL); + if (status != MOSQ_ERR_SUCCESS) { + ERROR ("mqtt plugin: cannot mosquitto_tls_set: %s", mosquitto_strerror(status)); + mosquitto_destroy (conf->mosq); + conf->mosq = NULL; + return (-1); + } + + status = mosquitto_tls_opts_set(conf->mosq, SSL_VERIFY_PEER, conf->tlsprotocol, conf->ciphersuite); + if (status != MOSQ_ERR_SUCCESS) { + ERROR ("mqtt plugin: cannot mosquitto_tls_opts_set: %s", mosquitto_strerror(status)); + mosquitto_destroy (conf->mosq); + conf->mosq = NULL; + return (-1); + } + + status = mosquitto_tls_insecure_set(conf->mosq, false); + if (status != MOSQ_ERR_SUCCESS) { + ERROR ("mqtt plugin: cannot mosquitto_tls_insecure_set: %s", mosquitto_strerror(status)); + mosquitto_destroy (conf->mosq); + conf->mosq = NULL; + return (-1); + } + } +#endif + if (conf->username && conf->password) { status = mosquitto_username_pw_set (conf->mosq, conf->username, conf->password); @@ -271,8 +342,12 @@ static int mqtt_connect (mqtt_client_conf_t *conf) } } +#if LIBMOSQUITTO_MAJOR == 0 status = mosquitto_connect (conf->mosq, conf->host, conf->port, - /* keepalive = */ 10, /* clean session = */ conf->clean_session); + /* keepalive = */ MQTT_KEEPALIVE, /* clean session = */ conf->clean_session); +#else + status = mosquitto_connect (conf->mosq, conf->host, conf->port, MQTT_KEEPALIVE); +#endif if (status != MOSQ_ERR_SUCCESS) { char errbuf[1024]; @@ -290,7 +365,8 @@ static int mqtt_connect (mqtt_client_conf_t *conf) { mosquitto_message_callback_set (conf->mosq, on_message); - status = mosquitto_subscribe (conf->mosq, /* mid = */ NULL, + status = mosquitto_subscribe (conf->mosq, + /* message_id = */ NULL, conf->topic, conf->qos); if (status != MOSQ_ERR_SUCCESS) { @@ -326,7 +402,13 @@ static void *subscribers_thread (void *arg) /* The documentation says "0" would map to the default (1000ms), but * that does not work on some versions. */ +#if LIBMOSQUITTO_MAJOR == 0 status = mosquitto_loop (conf->mosq, /* timeout = */ 1000 /* ms */); +#else + status = mosquitto_loop (conf->mosq, + /* timeout[ms] = */ 1000, + /* max_packets = */ 100); +#endif if (status == MOSQ_ERR_CONN_LOST) { conf->connected = 0; @@ -362,21 +444,22 @@ static int publish (mqtt_client_conf_t *conf, char const *topic, return (status); } - status = mosquitto_publish(conf->mosq, - /* message id */ NULL, - topic, + status = mosquitto_publish(conf->mosq, /* message_id */ NULL, topic, +#if LIBMOSQUITTO_MAJOR == 0 (uint32_t) payload_len, payload, - /* qos */ conf->qos, - /* retain */ conf->retain); +#else + (int) payload_len, payload, +#endif + conf->qos, conf->retain); if (status != MOSQ_ERR_SUCCESS) { char errbuf[1024]; c_complain (LOG_ERR, - &conf->complaint_cantpublish, - "plugin mqtt: mosquitto_publish failed: %s", - status == MOSQ_ERR_ERRNO ? - sstrerror(errno, errbuf, sizeof (errbuf)) : - mosquitto_strerror(status)); + &conf->complaint_cantpublish, + "mqtt plugin: mosquitto_publish failed: %s", + (status == MOSQ_ERR_ERRNO) + ? sstrerror(errno, errbuf, sizeof (errbuf)) + : mosquitto_strerror(status)); /* Mark our connection "down" regardless of the error as a safety * measure; we will try to reconnect the next time we have to publish a * message */ @@ -459,11 +542,16 @@ static int mqtt_write (const data_set_t *ds, const value_list_t *vl, * StoreRates true * Retain false * QoS 0 + * CACert "ca.pem" Enables TLS if set + * CertificateFile "client-cert.pem" optional + * CertificateKeyFile "client-key.pem" optional + * TLSProtocol "tlsv1.2" optional * */ static int mqtt_config_publisher (oconfig_item_t *ci) { mqtt_client_conf_t *conf; + char cb_name[1024]; user_data_t user_data; int status; int i; @@ -487,7 +575,16 @@ static int mqtt_config_publisher (oconfig_item_t *ci) conf->host = strdup (MQTT_DEFAULT_HOST); conf->port = MQTT_DEFAULT_PORT; conf->client_id = NULL; + conf->qos = 0; conf->topic_prefix = strdup (MQTT_DEFAULT_TOPIC_PREFIX); + conf->store_rates = 1; + + status = pthread_mutex_init (&conf->lock, NULL); + if (status != 0) + { + mqtt_free (conf); + return (status); + } C_COMPLAIN_INIT (&conf->complaint_cantpublish); @@ -525,14 +622,25 @@ static int mqtt_config_publisher (oconfig_item_t *ci) cf_util_get_boolean (child, &conf->store_rates); else if (strcasecmp ("Retain", child->key) == 0) cf_util_get_boolean (child, &conf->retain); + else if (strcasecmp ("CACert", child->key) == 0) + cf_util_get_string (child, &conf->cacertificatefile); + else if (strcasecmp ("CertificateFile", child->key) == 0) + cf_util_get_string (child, &conf->certificatefile); + else if (strcasecmp ("CertificateKeyFile", child->key) == 0) + cf_util_get_string (child, &conf->certificatekeyfile); + else if (strcasecmp ("TLSProtocol", child->key) == 0) + cf_util_get_string (child, &conf->tlsprotocol); + else if (strcasecmp ("CipherSuite", child->key) == 0) + cf_util_get_string (child, &conf->ciphersuite); else ERROR ("mqtt plugin: Unknown config option: %s", child->key); } + ssnprintf (cb_name, sizeof (cb_name), "mqtt/%s", conf->name); memset (&user_data, 0, sizeof (user_data)); user_data.data = conf; - plugin_register_write ("mqtt", mqtt_write, &user_data); + plugin_register_write (cb_name, mqtt_write, &user_data); return (0); } /* mqtt_config_publisher */ @@ -544,7 +652,7 @@ static int mqtt_config_publisher (oconfig_item_t *ci) * User "guest" * Password "secret" * Topic "collectd/#" - * + * */ static int mqtt_config_subscriber (oconfig_item_t *ci) { @@ -572,7 +680,16 @@ static int mqtt_config_subscriber (oconfig_item_t *ci) conf->host = strdup (MQTT_DEFAULT_HOST); conf->port = MQTT_DEFAULT_PORT; conf->client_id = NULL; + conf->qos = 2; conf->topic = strdup (MQTT_DEFAULT_TOPIC); + conf->clean_session = 1; + + status = pthread_mutex_init (&conf->lock, NULL); + if (status != 0) + { + mqtt_free (conf); + return (status); + } C_COMPLAIN_INIT (&conf->complaint_cantpublish); @@ -612,7 +729,7 @@ static int mqtt_config_subscriber (oconfig_item_t *ci) ERROR ("mqtt plugin: Unknown config option: %s", child->key); } - tmp = realloc (subscribers, sizeof (*subscribers) * subscribers_num); + tmp = realloc (subscribers, sizeof (*subscribers) * (subscribers_num + 1) ); if (tmp == NULL) { ERROR ("mqtt plugin: realloc failed.");