From 530ccbc90daea6ad3cd24b3c2a7474005f8e6997 Mon Sep 17 00:00:00 2001 From: Alexandros Kosiaris Date: Tue, 14 Mar 2023 10:38:52 +0200 Subject: [PATCH] collectd: Support config of mqtt plugin 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 and blocks under 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 --- utils/collectd/Makefile | 2 +- utils/collectd/files/collectd.init | 98 +++++++++++++++++++ utils/collectd/files/collectd.uci | 9 ++ .../files/usr/share/collectd/plugin/mqtt.json | 2 + 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 utils/collectd/files/usr/share/collectd/plugin/mqtt.json diff --git a/utils/collectd/Makefile b/utils/collectd/Makefile index 1c8a04bc04..47c60a0b19 100644 --- a/utils/collectd/Makefile +++ b/utils/collectd/Makefile @@ -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/ \ diff --git a/utils/collectd/files/collectd.init b/utils/collectd/files/collectd.init index e372e70f21..2146a66865 100644 --- a/utils/collectd/files/collectd.init +++ b/utils/collectd/files/collectd.init @@ -124,6 +124,100 @@ process_write_http_node() { printf "\\t\n" >> "$COLLECTD_CONF" } +process_mqtt() { + printf "\n" >> "$COLLECTD_CONF" + config_foreach process_mqtt_block mqtt_block + printf "\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\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" diff --git a/utils/collectd/files/collectd.uci b/utils/collectd/files/collectd.uci index 075788272b..3b2088a831 100644 --- a/utils/collectd/files/collectd.uci +++ b/utils/collectd/files/collectd.uci @@ -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 index 0000000000..2c63c08510 --- /dev/null +++ b/utils/collectd/files/usr/share/collectd/plugin/mqtt.json @@ -0,0 +1,2 @@ +{ +}