collectd: Support config of mqtt plugin 20662/head
authorAlexandros Kosiaris <akosiaris@gmail.com>
Tue, 14 Mar 2023 08:38:52 +0000 (10:38 +0200)
committerAlexandros Kosiaris <akosiaris@gmail.com>
Thu, 27 Jul 2023 06:49:28 +0000 (09:49 +0300)
mqtt plugin is already built and shipped in
collectd-mod-mqtt, however it is not possible to configure it via
uci currently, instead having to rely on populating the config file manually.

Add support by adding 2 functions, process_mqtt() and
process_mqtt_block(). First one just enables/disables the plugin.
The second one, in the spirit of the curl plugin, adds support for
populating multiple <Publish> and <Subscribe> blocks under <Plugin mqtt>
with support for some parameters. Those are:

* blocktype. Publish or Subscribe. Mandatory
* name. The name of the block. Mandatory
* Host. Mandatory
* Port. Optional
* User. Optional
* Password. Optional
* ClientId. Optional
* QoS. Optional
* Prefix. Optional
* Retain. Optional
* StoreRates. Optional
* CleanSession. Optional
* Topic. Optional

Bump PKG_RELEASE per comments in PR

Signed-off-by: Alexandros Kosiaris <akosiaris@gmail.com>
utils/collectd/Makefile
utils/collectd/files/collectd.init
utils/collectd/files/collectd.uci
utils/collectd/files/usr/share/collectd/plugin/mqtt.json [new file with mode: 0644]

index 1c8a04bc0457f167363e7bc7875a584bf939f2d8..47c60a0b19a2319cbdece907b2751cabcaff77ee 100644 (file)
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=collectd
 PKG_VERSION:=5.12.0
-PKG_RELEASE:=43
+PKG_RELEASE:=44
 
 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.bz2
 PKG_SOURCE_URL:=https://collectd.org/files/ \
index e372e70f216ae6b60b892357118f306121b36a9a..2146a668659d08630604bf022398bfecee4c4463 100644 (file)
@@ -124,6 +124,100 @@ process_write_http_node() {
        printf "\\t</Node>\n" >> "$COLLECTD_CONF"
 }
 
+process_mqtt() {
+       printf "<Plugin mqtt>\n" >> "$COLLECTD_CONF"
+       config_foreach process_mqtt_block mqtt_block
+       printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
+}
+
+process_mqtt_block() {
+       local cfg="$1"
+
+       local blocktype name Host Port User Password ClientId QoS Prefix Retain StoreRates CleanSession Topic
+
+       config_get blocktype "$cfg" blocktype
+       [ -z "$blocktype" ] && {
+               $LOG notice "No blocktype option in config $cfg defined"
+               return 0
+       }
+       config_get name "$cfg" name
+       [ -z "$name" ] && {
+               $LOG notice "No name option in config $cfg defined"
+               return 0
+       }
+       config_get Host "$cfg" Host
+       [ -z "$Host" ] && {
+               $LOG notice "No Host option in config $cfg defined"
+               return 0
+       }
+       config_get Port "$cfg" Port
+       config_get User "$cfg" User
+       config_get Password "$cfg" Password
+       config_get QoS "$cfg" QoS
+       config_get ClientId "$cfg" ClientId
+       config_get Prefix "$cfg" Prefix
+       [ -n "$Prefix" ] && [ "$blocktype" != "Publish" ] && {
+               $LOG notice "Prefix option in config $cfg defined under non Publish block"
+               return 0
+       }
+       config_get Retain "$cfg" Retain
+       [ -n "$Retain" ] && [ "$blocktype" != "Publish" ] && {
+               $LOG notice "Retain option in config $cfg defined under non Publish block"
+               return 0
+       }
+       config_get StoreRates "$cfg" StoreRates
+       [ -n "$StoreRates" ] && [ "$blocktype" != "Publish" ] && {
+               $LOG notice "StoreRates option in config $cfg defined under non Publish block"
+               return 0
+       }
+       config_get CleanSession "$cfg" CleanSession
+       [ -n "$CleanSession" ] && [ "$blocktype" != "Subscribe" ] && {
+               $LOG notice "CleanSession option in config $cfg defined under non Subscribe block"
+               return 0
+       }
+       config_get Topic "$cfg" Topic
+       [ -n "$Topic" ] && [ "$blocktype" != "Subscribe" ] && {
+               $LOG notice "Topic option in config $cfg defined under non Subscribe block"
+               return 0
+       }
+
+       printf "\\t<%s \"%s\">\n" "${blocktype}" "${name}" >> "$COLLECTD_CONF"
+       [ -z "$Host" ] || {
+               printf "\\t\\tHost \"%s\"\n" "${Host}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$Port" ] || {
+               printf "\\t\\tPort \"%s\"\n" "${Port}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$User" ] || {
+               printf "\\t\\tUser \"%s\"\n" "${User}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$Password" ] || {
+               printf "\\t\\tPassword \"%s\"\n" "${Password}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$QoS" ] || {
+               printf "\\t\\tQoS %s\n" "${QoS}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$ClientId" ] || {
+               printf "\\t\\tClientId \"%s\"\n" "${ClientId}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$Prefix" ] || {
+               printf "\\t\\tPrefix \"%s\"\n" "${Prefix}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$Retain" ] || {
+               printf "\\t\\tRetain \"%s\"\n" "${Retain}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$StoreRates" ] || {
+               printf "\\t\\tStoreRates \"%s\"\n" "${StoreRates}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$CleanSession" ] || {
+               printf "\\t\\tCleanSession \"%s\"\n" "${CleanSession}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$Topic" ] || {
+               printf "\\t\\tTopic \"%s\"\n" "${Topic}" >> "$COLLECTD_CONF"
+       }
+       printf "\\t</%s>\n" "${blocktype}" >> "$COLLECTD_CONF"
+}
+
 process_network() {
        local cfg="$1"
 
@@ -407,6 +501,10 @@ process_plugins() {
                        CONFIG_STRING=""
                        process_write_http
                        ;;
+               mqtt)
+                       CONFIG_STRING=""
+                       process_mqtt
+                       ;;
                *)
                        CONFIG_STRING=""
                        process_generic "$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
index 075788272b55c6f527c0016993fb48a6d544cca3..3b2088a831277e27511850fd3b1b92ddbc6d4498 100644 (file)
@@ -221,3 +221,12 @@ config globals 'globals'
 #config write_http_node
 #      option name 'foo'
 #      option URL 'http://example.org/post-collectd'
+#
+#config plugin 'mqtt'
+#      option enable '1'
+#
+#config mqtt_block
+#      option name 'foo'
+#      option blocktype 'Subscribe'
+#      option Host 'localhost'
+#      option Topic 'collectd/#'
diff --git a/utils/collectd/files/usr/share/collectd/plugin/mqtt.json b/utils/collectd/files/usr/share/collectd/plugin/mqtt.json
new file mode 100644 (file)
index 0000000..2c63c08
--- /dev/null
@@ -0,0 +1,2 @@
+{
+}