From 5473c87303271d1e25e83fd05c204221cc490f7e Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 20 Nov 2014 20:05:50 +0100 Subject: [PATCH] mqtt plugin: Add preliminary configuration support. --- src/mqtt.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/mqtt.c b/src/mqtt.c index fffa8fad..d1d22054 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -212,11 +212,20 @@ static int mqtt_write (const data_set_t *ds, const value_list_t *vl, return (status); } /* mqtt_write */ +/* + * + * Host "example.com" + * Port 1883 + * Prefix "collectd" + * ClientId "collectd" + * + */ static int mqtt_config (oconfig_item_t *ci) { mqtt_client_conf_t *conf; user_data_t user_data; int status; + int i; conf = calloc (1, sizeof (*conf)); if (conf == NULL) @@ -226,12 +235,35 @@ static int mqtt_config (oconfig_item_t *ci) } conf->connected = false; - conf->host = MQTT_DEFAULT_HOST; + conf->host = strdup (MQTT_DEFAULT_HOST); conf->port = MQTT_DEFAULT_PORT; - conf->client_id = MQTT_DEFAULT_CLIENT_ID; - conf->topic_prefix = MQTT_DEFAULT_TOPIC_PREFIX; + conf->client_id = strdup (MQTT_DEFAULT_CLIENT_ID); + conf->topic_prefix = strdup (MQTT_DEFAULT_TOPIC_PREFIX); C_COMPLAIN_INIT (&conf->complaint_cantpublish); + for (i = 0; i < ci->children_num; i++) + { + oconfig_item_t *child = ci->children + i; + if (strcasecmp ("Host", child->key) == 0) + cf_util_get_string (child, &conf->host); + else if (strcasecmp ("Port", child->key) == 0) + { + int tmp = cf_util_get_port_number (child); + if (tmp < 0) + { + ERROR ("mqtt plugin: Invalid port number."); + continue; + } + conf->port = tmp; + } + else if (strcasecmp ("Prefix", child->key) == 0) + cf_util_get_string (child, &conf->topic_prefix); + else if (strcasecmp ("ClientId", child->key) == 0) + cf_util_get_string (child, &conf->client_id); + else + ERROR ("mqtt plugin: Unknown config option: %s", child->key); + } + memset (&user_data, 0, sizeof (user_data)); user_data.data = conf; -- 2.11.0