]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
mqtt plugin: add Format config, default to PLAIN
authorMark Ferry <mark@markferry.net>
Fri, 10 Oct 2025 18:11:30 +0000 (19:11 +0100)
committerMark Ferry <mark@markferry.net>
Mon, 24 Nov 2025 14:11:19 +0000 (14:11 +0000)
src/collectd.conf.in
src/collectd.conf.pod
src/mqtt.c

index 77e7028879572490ea6908ccbb88dfb5a97fe799..30bae9c4e82a4a37fa8f1a5d23774dd5a99049c1 100644 (file)
 #              User "user"
 #              Password "secret"
 #              QoS 0
+#              Format PLAIN
 #              Prefix "collectd"
 #              StoreRates true
 #              Retain false
index 7ae344ac7b3107d042ca50411171a5fd80d0af75..f1ba1eb3f8dfc1684cb0e9858f6ac1e5e4dc02b7 100644 (file)
@@ -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<Format> I<Format> (Publish only)
+
+The output format to use. Defaults to C<PLAIN>.
+
 =item B<Prefix> I<Prefix> (Publish only)
 
 This plugin will use one topic per I<value list> which will looks like a path.
index f26b8cd27b958fd35515d827d1d586307464d1c9..a53890e3d088f19bc56b0fa2766d5d8f69a9a3aa 100644 (file)
@@ -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 */
+
 /*
  * <Publish "name">
  *   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)