From: Mark Ferry Date: Fri, 10 Oct 2025 18:11:30 +0000 (+0100) Subject: mqtt plugin: add Format config, default to PLAIN X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ba461b9f9983385a20be13426d22da8addc43204;p=thirdparty%2Fcollectd.git mqtt plugin: add Format config, default to PLAIN --- diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 77e702887..30bae9c4e 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1092,6 +1092,7 @@ # User "user" # Password "secret" # QoS 0 +# Format PLAIN # Prefix "collectd" # StoreRates true # Retain false diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 7ae344ac7..f1ba1eb3f 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -5445,6 +5445,10 @@ QoS setting the client is going to accept and defaults to B<2>. If the QoS flag on a message is larger than the maximum accepted QoS of a subscriber, the message's QoS will be downgraded. +=item B I (Publish only) + +The output format to use. Defaults to C. + =item B I (Publish only) This plugin will use one topic per I which will looks like a path. diff --git a/src/mqtt.c b/src/mqtt.c index f26b8cd27..a53890e3d 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -68,6 +68,9 @@ struct mqtt_client_conf { char *username; char *password; int qos; +#define MQTT_FORMAT_PLAIN 0 +#define MQTT_FORMAT_JSON 1 + uint8_t format; char *cacertificatefile; char *certificatefile; char *certificatekeyfile; @@ -607,6 +610,29 @@ static int mqtt_notification(const notification_t *n, return status; } /* int mqtt_notification */ +static int config_set_format(mqtt_client_conf_t *conf, oconfig_item_t *ci) { + char *string; + + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { + WARNING("mqtt plugin: The `%s' config option " + "needs exactly one string argument.", + ci->key); + return -1; + } + + string = ci->values[0].value.string; + if (strcasecmp("Plain", string) == 0) + conf->format = MQTT_FORMAT_PLAIN; + else if (strcasecmp("JSON", string) == 0) + conf->format = MQTT_FORMAT_JSON; + else { + ERROR("mqtt plugin: Invalid format string: %s", string); + return -1; + } + + return 0; +} /* int config_set_format */ + /* * * Host "example.com" @@ -619,6 +645,7 @@ static int mqtt_notification(const notification_t *n, * Retain false * SendNotifications false * QoS 0 + * Format PLAIN * CACert "ca.pem" Enables TLS if set * CertificateFile "client-cert.pem" optional * CertificateKeyFile "client-key.pem" optional @@ -648,6 +675,7 @@ static int mqtt_config_publisher(oconfig_item_t *ci) { conf->port = MQTT_DEFAULT_PORT; conf->client_id = NULL; conf->qos = 0; + conf->format = MQTT_FORMAT_PLAIN; conf->topic_prefix = strdup(MQTT_DEFAULT_TOPIC_PREFIX); conf->store_rates = true; @@ -684,6 +712,8 @@ static int mqtt_config_publisher(oconfig_item_t *ci) { conf->qos = tmp; } else if (strcasecmp("Prefix", child->key) == 0) cf_util_get_string(child, &conf->topic_prefix); + else if (strcasecmp("Format", child->key) == 0) + config_set_format(conf, child); else if (strcasecmp("StoreRates", child->key) == 0) cf_util_get_boolean(child, &conf->store_rates); else if (strcasecmp("Retain", child->key) == 0)