]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
mqtt plugin: add NotificationPrefix config, default to "collectd/event"
authorMark Ferry <mark@markferry.net>
Sat, 11 Oct 2025 22:15:16 +0000 (23:15 +0100)
committerMark Ferry <mark@markferry.net>
Mon, 24 Nov 2025 14:11:20 +0000 (14:11 +0000)
src/collectd.conf.in
src/collectd.conf.pod
src/mqtt.c

index 30bae9c4e82a4a37fa8f1a5d23774dd5a99049c1..249e594ad7586dfe3c9ef9c98f5056d3e76de42e 100644 (file)
 #              StoreRates true
 #              Retain false
 #              SendNotifications false
+#              NotificationPrefix "collectd/event"
 #              CACert "/etc/ssl/ca.crt"
 #              CertificateFile "/etc/ssl/client.crt"
 #              CertificateKeyFile "/etc/ssl/client.pem"
index ae7a75967a34d834ac75c0350f23d152b4d404a6..521a8906d68e94694ed9ebeea54ac7416f9512b1 100644 (file)
@@ -5458,6 +5458,15 @@ An example topic name would be:
 
  collectd/cpu-0/cpu-user
 
+=item B<NotificationPrefix> I<Prefix> (Publish only)
+
+Like B<Prefix>, this plugin will use one topic per I<notification> instance.
+I<Prefix> is used as the first path element and defaults to B<collectd/event>.
+
+An example topic name would be:
+
+ collectd/event/host/interface-eth0/if_octets
+
 =item B<Retain> B<false>|B<true> (Publish only)
 
 Controls whether the MQTT broker will retain (keep a copy of) the last message
@@ -5472,11 +5481,7 @@ before sending. Defaults to B<true>.
 
 Controls whether notifications are sent. Defaults to B<false>.
 
-Notifications are published to an C<event> subtopic for each type instance.
-
-Example:
-
- collectd/host/interface-eth0/if_octets/event
+Notifications are published with a B<NotificationPrefix> for each type instance.
 
 =item B<CleanSession> B<true>|B<false> (Subscribe only)
 
index 376687c475a5d685a99b70f5d4805a1b5e7cd066..4bbf8b613595891faa9fa911527d8343d7625f13 100644 (file)
@@ -44,6 +44,7 @@
 #define MQTT_DEFAULT_HOST "localhost"
 #define MQTT_DEFAULT_PORT 1883
 #define MQTT_DEFAULT_TOPIC_PREFIX "collectd"
+#define MQTT_DEFAULT_NOTIFICATION_PREFIX "collectd/event"
 #define MQTT_DEFAULT_TOPIC "collectd/#"
 #ifndef MQTT_KEEPALIVE
 #define MQTT_KEEPALIVE 60
@@ -79,6 +80,7 @@ struct mqtt_client_conf {
 
   /* For publishing */
   char *topic_prefix;
+  char *notification_prefix;
   bool store_rates;
   bool retain;
   bool send_notifications;
@@ -155,6 +157,7 @@ static void mqtt_free(mqtt_client_conf_t *conf) {
   sfree(conf->password);
   sfree(conf->client_id);
   sfree(conf->topic_prefix);
+  sfree(conf->notification_prefix);
   sfree(conf);
 }
 
@@ -567,7 +570,8 @@ static int format_notification_topic(char *buf, size_t buf_len,
   int status;
   char *c;
 
-  if ((conf->topic_prefix == NULL) || (conf->topic_prefix[0] == 0)) {
+  if ((conf->notification_prefix == NULL) ||
+      (conf->notification_prefix[0] == 0)) {
     // tempting but unsafe to use FORMAT_VL here
     return format_name(buf, buf_len, n->host, n->plugin, n->plugin_instance,
                        n->type, n->type_instance);
@@ -578,7 +582,7 @@ static int format_notification_topic(char *buf, size_t buf_len,
   if (status != 0)
     return status;
 
-  status = ssnprintf(buf, buf_len, "%s/%s/event", conf->topic_prefix, name);
+  status = ssnprintf(buf, buf_len, "%s/%s", conf->notification_prefix, name);
   if ((status < 0) || (((size_t)status) >= buf_len))
     return ENOMEM;
 
@@ -727,6 +731,7 @@ static int config_set_format(mqtt_client_conf_t *conf, oconfig_item_t *ci) {
  *   StoreRates true
  *   Retain false
  *   SendNotifications false
+ *   NotificationPrefix "collectd/event"
  *   QoS 0
  *   Format PLAIN
  *   CACert "ca.pem"                      Enables TLS if set
@@ -760,6 +765,7 @@ static int mqtt_config_publisher(oconfig_item_t *ci) {
   conf->qos = 0;
   conf->format = MQTT_FORMAT_PLAIN;
   conf->topic_prefix = strdup(MQTT_DEFAULT_TOPIC_PREFIX);
+  conf->notification_prefix = strdup(MQTT_DEFAULT_NOTIFICATION_PREFIX);
   conf->store_rates = true;
 
   status = pthread_mutex_init(&conf->lock, NULL);
@@ -803,6 +809,8 @@ static int mqtt_config_publisher(oconfig_item_t *ci) {
       cf_util_get_boolean(child, &conf->retain);
     else if (strcasecmp("SendNotifications", child->key) == 0)
       cf_util_get_boolean(child, &conf->send_notifications);
+    else if (strcasecmp("NotificationPrefix", child->key) == 0)
+      cf_util_get_string(child, &conf->notification_prefix);
     else if (strcasecmp("CACert", child->key) == 0)
       cf_util_get_string(child, &conf->cacertificatefile);
     else if (strcasecmp("CertificateFile", child->key) == 0)