From: Mark Ferry Date: Sat, 11 Oct 2025 22:15:16 +0000 (+0100) Subject: mqtt plugin: add NotificationPrefix config, default to "collectd/event" X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8e916ce11b3d963d177c71f030484a982cc1ae9d;p=thirdparty%2Fcollectd.git mqtt plugin: add NotificationPrefix config, default to "collectd/event" --- diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 30bae9c4e..249e594ad 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1097,6 +1097,7 @@ # StoreRates true # Retain false # SendNotifications false +# NotificationPrefix "collectd/event" # CACert "/etc/ssl/ca.crt" # CertificateFile "/etc/ssl/client.crt" # CertificateKeyFile "/etc/ssl/client.pem" diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index ae7a75967..521a8906d 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -5458,6 +5458,15 @@ An example topic name would be: collectd/cpu-0/cpu-user +=item B I (Publish only) + +Like B, this plugin will use one topic per I instance. +I is used as the first path element and defaults to B. + +An example topic name would be: + + collectd/event/host/interface-eth0/if_octets + =item B B|B (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. Controls whether notifications are sent. Defaults to B. -Notifications are published to an C subtopic for each type instance. - -Example: - - collectd/host/interface-eth0/if_octets/event +Notifications are published with a B for each type instance. =item B B|B (Subscribe only) diff --git a/src/mqtt.c b/src/mqtt.c index 376687c47..4bbf8b613 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -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)