collectd: extend network uci plugin 19314/head
authorFlorian Eckert <fe@dev.tdt.de>
Tue, 6 Oct 2020 10:52:20 +0000 (12:52 +0200)
committerFlorian Eckert <fe@dev.tdt.de>
Mon, 5 Sep 2022 13:28:29 +0000 (15:28 +0200)
The network plugin from collectd also has the option to encrypt the
metrics when sending them to another server. Until now, this was not
possible via the UCI. This commit adds that feature.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
utils/collectd/files/collectd.init

index 05e21e109f78827602225df7bc16215d8ef506e1..e372e70f216ae6b60b892357118f306121b36a9a 100644 (file)
@@ -153,11 +153,58 @@ process_network() {
        printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
 }
 
+process_network_server() {
+       local cfg="$1"
+       local SecurityLevel="$2"
+
+       local Username Password ResolveInterval
+
+       config_get Username "$cfg" Username
+       [ -z "$Username" ] && {
+               $LOG notice "SecurityLevel set to '$SecurityLevel' but no option Username found in config '$cfg'"
+               return 1
+       }
+       printf "\\t\\tUsername \"%s\"\n" "${Username}" >> "$COLLECTD_CONF"
+
+       config_get Password "$cfg" Password
+       [ -z "$Password" ] && {
+               $LOG notice "SecurityLevel set to '$SecurityLevel' but no option Password found in config '$cfg'"
+               return 2
+       }
+       printf "\\t\\tPassword \"%s\"\n" "${Password}" >> "$COLLECTD_CONF"
+
+       config_get ResolveInterval "$cfg" ResolveInterval
+       [ -z "$ResolveInterval" ] || {
+               printf "\\t\\tResolveInterval \"%s\"\n" "${ResolveInterval}" >> "$COLLECTD_CONF"
+       }
+}
+
+process_network_listen() {
+       local cfg="$1"
+
+       local auth_file="/tmp/collectd-auth-${cfg}.conf"
+       local auth_set
+
+       rm -rf "${auth_file}"
+       add_auth() {
+               echo "$1" >> "${auth_file}"
+               auth_set=1
+       }
+       config_list_foreach "$cfg" auth add_auth
+
+       [ -z "$auth_set" ] && {
+               $LOG notice "SecurityLevel set to '$SecurityLevel' but no list option auth found in config '$cfg'"
+               return 1
+       }
+
+       printf "\\t\\tAuthFile \"%s\"\n" "${auth_file}" >> "$COLLECTD_CONF"
+}
+
 process_network_sections() {
        local cfg="$1"
        local section="$2"
 
-       local host port output
+       local host port output rvalue SecurityLevel Interface
 
        config_get host "$cfg" host
        [ -z "$host" ] && {
@@ -173,9 +220,42 @@ process_network_sections() {
 
        config_get port "$cfg" port
        if [ -z "$port" ]; then
-               printf "\\t%s\n" "${output}" >> "$COLLECTD_CONF"
+               printf "\\t<%s>\n" "${output}" >> "$COLLECTD_CONF"
+       else
+               printf "\\t<%s \"%s\">\n" "${output}" "${port}" >> "$COLLECTD_CONF"
+       fi
+
+       config_get SecurityLevel "$cfg" SecurityLevel 'None'
+       [ -z "$SecurityLevel" ] || {
+               printf "\\t\\tSecurityLevel \"%s\"\n" "${SecurityLevel}" >> "$COLLECTD_CONF"
+       }
+
+       if [ "$SecurityLevel" != "None" ]; then
+               case "$section" in
+                       server)
+                               process_network_server "$cfg" "$SecurityLevel"
+                               rvalue="$?"
+                               [ "$rvalue" != 0 ] && return 0
+                               ;;
+                       listen)
+                               process_network_listen "$cfg" "$SecurityLevel"
+                               rvalue="$?"
+                               [ "$rvalue" != 0 ] && return 0
+                               ;;
+               esac
+       else
+               $LOG notice "SecurityLevel set to 'None' for '$cfg'"
+       fi
+
+       config_get Interface "$cfg" Interface
+       [ -z "$Interface" ] || {
+               printf "\\t\\tInterface \"%s\"\n" "${Interface}" >> "$COLLECTD_CONF"
+       }
+
+       if [ "$section" = "server" ]; then
+               printf "\\t</Server>\n" >> "$COLLECTD_CONF"
        else
-               printf "\\t%s \"%s\"\n" "${output}" "${port}" >> "$COLLECTD_CONF"
+               printf "\\t</Listen>\n" >> "$COLLECTD_CONF"
        fi
 }