2 * collectd - src/mqtt.c
3 * Copyright (C) 2014 Marc Falzon <marc at baha dot mu>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 // Reference: http://mosquitto.org/api/files/mosquitto-h.html
30 #include "utils_cache.h"
31 #include "utils_complain.h"
35 #include <mosquitto.h>
37 #define MQTT_MAX_TOPIC_SIZE 1024
38 #define MQTT_MAX_MESSAGE_SIZE MQTT_MAX_TOPIC_SIZE + 1024
39 #define MQTT_DEFAULT_HOST "localhost"
40 #define MQTT_DEFAULT_PORT 1883
41 #define MQTT_DEFAULT_CLIENT_ID "collectd"
42 #define MQTT_DEFAULT_TOPIC_PREFIX "collectd"
47 struct mqtt_client_conf
49 struct mosquitto *mosq;
55 c_complain_t complaint_cantpublish;
58 typedef struct mqtt_client_conf mqtt_client_conf_t;
60 static char const *mosquitto_strerror (int code)
64 case MOSQ_ERR_SUCCESS: return "MOSQ_ERR_SUCCESS";
65 case MOSQ_ERR_NOMEM: return "MOSQ_ERR_NOMEM";
66 case MOSQ_ERR_PROTOCOL: return "MOSQ_ERR_PROTOCOL";
67 case MOSQ_ERR_INVAL: return "MOSQ_ERR_INVAL";
68 case MOSQ_ERR_NO_CONN: return "MOSQ_ERR_NO_CONN";
69 case MOSQ_ERR_CONN_REFUSED: return "MOSQ_ERR_CONN_REFUSED";
70 case MOSQ_ERR_NOT_FOUND: return "MOSQ_ERR_NOT_FOUND";
71 case MOSQ_ERR_CONN_LOST: return "MOSQ_ERR_CONN_LOST";
72 case MOSQ_ERR_SSL: return "MOSQ_ERR_SSL";
73 case MOSQ_ERR_PAYLOAD_SIZE: return "MOSQ_ERR_PAYLOAD_SIZE";
74 case MOSQ_ERR_NOT_SUPPORTED: return "MOSQ_ERR_NOT_SUPPORTED";
75 case MOSQ_ERR_AUTH: return "MOSQ_ERR_AUTH";
76 case MOSQ_ERR_ACL_DENIED: return "MOSQ_ERR_ACL_DENIED";
77 case MOSQ_ERR_UNKNOWN: return "MOSQ_ERR_UNKNOWN";
78 case MOSQ_ERR_ERRNO: return "MOSQ_ERR_ERRNO";
81 return "UNKNOWN ERROR CODE";
87 /* must hold conf->lock when calling. */
88 static int mqtt_reconnect_broker (mqtt_client_conf_t *conf)
95 status = mosquitto_reconnect (conf->mosq);
96 if (status != MOSQ_ERR_SUCCESS)
99 ERROR ("mqtt_connect_broker: mosquitto_connect failed: %s",
100 (status == MOSQ_ERR_ERRNO)
101 ? sstrerror(errno, errbuf, sizeof (errbuf))
102 : mosquitto_strerror (status));
109 &conf->complaint_cantpublish,
110 "mqtt plugin: successfully reconnected to broker \"%s:%d\"",
111 conf->host, conf->port);
114 } /* mqtt_reconnect_broker */
116 static int publish (mqtt_client_conf_t *conf, char const *topic,
117 void const *payload, size_t payload_len)
119 int const qos = 0; /* TODO: Config option */
122 pthread_mutex_lock (&conf->lock);
124 status = mqtt_reconnect_broker (conf);
126 pthread_mutex_unlock (&conf->lock);
127 ERROR ("mqtt plugin: unable to reconnect to broker");
131 status = mosquitto_publish(conf->mosq,
132 /* message id */ NULL,
134 (uint32_t) payload_len, payload,
137 if (status != MOSQ_ERR_SUCCESS)
141 &conf->complaint_cantpublish,
142 "plugin mqtt: mosquitto_publish failed: %s",
143 status == MOSQ_ERR_ERRNO ?
144 sstrerror(errno, errbuf, sizeof (errbuf)) :
145 mosquitto_strerror(status));
146 /* Mark our connection "down" regardless of the error as a safety
147 * measure; we will try to reconnect the next time we have to publish a
151 pthread_mutex_unlock (&conf->lock);
155 pthread_mutex_unlock (&conf->lock);
159 static int format_topic (char *buf, size_t buf_len,
160 data_set_t const *ds, value_list_t const *vl,
161 mqtt_client_conf_t *conf)
163 char name[MQTT_MAX_TOPIC_SIZE];
166 if ((conf->topic_prefix == NULL) || (conf->topic_prefix[0] == 0))
167 return (FORMAT_VL (buf, buf_len, vl));
169 status = FORMAT_VL (name, sizeof (name), vl);
173 status = ssnprintf (buf, buf_len, "%s/%s", conf->topic_prefix, name);
174 if ((status < 0) || (((size_t) status) >= buf_len))
178 } /* int format_topic */
180 static int mqtt_write (const data_set_t *ds, const value_list_t *vl,
181 user_data_t *user_data)
183 mqtt_client_conf_t *conf;
184 char topic[MQTT_MAX_TOPIC_SIZE];
185 char payload[MQTT_MAX_MESSAGE_SIZE];
187 _Bool const store_rates = 0; /* TODO: Config option */
189 if ((user_data == NULL) || (user_data->data == NULL))
191 conf = user_data->data;
193 status = format_topic (topic, sizeof (topic), ds, vl, conf);
196 ERROR ("mqtt plugin: format_topic failed with status %d.", status);
200 status = format_values (payload, sizeof (payload),
201 ds, vl, store_rates);
204 ERROR ("mqtt plugin: format_values failed with status %d.", status);
208 status = publish (conf, topic, payload, sizeof (payload));
211 ERROR ("mqtt plugin: publish failed: %s", mosquitto_strerror (status));
223 * ClientId "collectd"
226 static int mqtt_config (oconfig_item_t *ci)
228 mqtt_client_conf_t *conf;
229 user_data_t user_data;
233 conf = calloc (1, sizeof (*conf));
236 ERROR ("mqtt plugin: malloc failed.");
241 conf->host = strdup (MQTT_DEFAULT_HOST);
242 conf->port = MQTT_DEFAULT_PORT;
243 conf->client_id = strdup (MQTT_DEFAULT_CLIENT_ID);
244 conf->topic_prefix = strdup (MQTT_DEFAULT_TOPIC_PREFIX);
245 C_COMPLAIN_INIT (&conf->complaint_cantpublish);
247 for (i = 0; i < ci->children_num; i++)
249 oconfig_item_t *child = ci->children + i;
250 if (strcasecmp ("Host", child->key) == 0)
251 cf_util_get_string (child, &conf->host);
252 else if (strcasecmp ("Port", child->key) == 0)
254 int tmp = cf_util_get_port_number (child);
257 ERROR ("mqtt plugin: Invalid port number.");
262 else if (strcasecmp ("Prefix", child->key) == 0)
263 cf_util_get_string (child, &conf->topic_prefix);
264 else if (strcasecmp ("ClientId", child->key) == 0)
265 cf_util_get_string (child, &conf->client_id);
267 ERROR ("mqtt plugin: Unknown config option: %s", child->key);
270 memset (&user_data, 0, sizeof (user_data));
271 user_data.data = conf;
273 conf->mosq = mosquitto_new (conf->client_id, /* user data = */ conf);
274 if (conf->mosq == NULL)
276 ERROR ("mqtt plugin: mosquitto_new failed");
281 status = mosquitto_connect (conf->mosq, conf->host, conf->port,
282 /* keepalive = */ 10, /* clean session = */ 1);
283 if (status != MOSQ_ERR_SUCCESS)
286 ERROR ("mqtt plugin: mosquitto_connect failed: %s",
287 (status == MOSQ_ERR_ERRNO)
288 ? sstrerror (errno, errbuf, sizeof (errbuf))
289 : mosquitto_strerror (status));
294 DEBUG ("mqtt plugin: successfully connected to broker \"%s:%d\"",
295 conf->host, conf->port);
299 plugin_register_write ("mqtt", mqtt_write, &user_data);
304 static int mqtt_init (void)
306 mosquitto_lib_init();
311 void module_register (void)
313 plugin_register_complex_config ("mqtt", mqtt_config);
314 plugin_register_init ("mqtt", mqtt_init);
315 } /* void module_register */
317 /* vim: set sw=4 sts=4 et fdm=marker : */