Merge pull request #19372 from cotequeiroz/libgd
[feed/packages.git] / utils / collectd / files / collectd.init
index 89715d4c465b4e3ac4e49f3cd35efbb602774003..e372e70f216ae6b60b892357118f306121b36a9a 100644 (file)
@@ -11,6 +11,11 @@ NICEPRIO=5
 
 CONFIG_STRING=""
 
+[ -d /usr/libexec/collectd ] && {
+       find /usr/libexec/collectd ! -perm 0500 -exec chmod 0500 '{}' '+'
+       find /usr/libexec/collectd ! \( -user nobody -a -group nogroup \) -exec chown nobody:nogroup '{}' '+'
+}
+
 process_exec() {
        printf "<Plugin exec>\n" >> "$COLLECTD_CONF"
        config_foreach process_exec_sections exec_input "Exec"
@@ -73,6 +78,52 @@ process_curl_page() {
        printf "\\t</Page>\n" >> "$COLLECTD_CONF"
 }
 
+process_write_http() {
+       printf "<Plugin write_http>\n" >> "$COLLECTD_CONF"
+       config_foreach process_write_http_node write_http_node
+       printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
+}
+
+process_write_http_node() {
+       local cfg="$1"
+
+       local name URL Format User Password Timeout BufferSize
+
+       config_get name "$cfg" name
+       [ -z "$name" ] && {
+               $LOG notice "No name option in config $cfg defined"
+               return 0
+       }
+       config_get URL "$cfg" URL
+       [ -z "$URL" ] && {
+               $LOG notice "No URL option in config $cfg defined"
+               return 0
+       }
+       config_get Format "$cfg" Format
+       config_get User "$cfg" User
+       config_get Password "$cfg" Password
+       config_get Timeout "$cfg" Timeout
+       config_get BufferSize "$cfg" BufferSize
+       printf "\\t<Node \"%s\">\n" "${name}" >> "$COLLECTD_CONF"
+       printf "\\t\\tURL \"%s\"\n" "${URL}" >> "$COLLECTD_CONF"
+       [ -z "$Format" ] || {
+               printf "\\t\\tFormat \"%s\"\n" "${Format}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$User" ] || {
+               printf "\\t\\tUser \"%s\"\n" "${User}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$Password" ] || {
+               printf "\\t\\tPassword \"%s\"\n" "${Password}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$Timeout" ] || {
+               printf "\\t\\tTimeout \%s\n" "${Timeout}" >> "$COLLECTD_CONF"
+       }
+       [ -z "$BufferSize" ] || {
+               printf "\\t\\tBufferSize \%s\n" "${BufferSize}" >> "$COLLECTD_CONF"
+       }
+       printf "\\t</Node>\n" >> "$COLLECTD_CONF"
+}
+
 process_network() {
        local cfg="$1"
 
@@ -93,7 +144,7 @@ process_network() {
        }
 
        config_get_bool Forward "$cfg" Forward
-       if [ "$value" = "0" ]; then
+       if [ "$Forward" = "0" ]; then
                printf "\\tForward false\n" >> "$COLLECTD_CONF"
        else
                printf "\\tForward true\n" >> "$COLLECTD_CONF"
@@ -102,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" ] && {
@@ -122,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
-               printf "\\t%s \"%s\"\n" "${output}" "${port}" >> "$COLLECTD_CONF"
+               $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</Listen>\n" >> "$COLLECTD_CONF"
        fi
 }
 
@@ -231,11 +362,9 @@ process_generic() {
 
        [ -z "$config" ] || {
                printf "%s<Plugin %s>\n" "${CONFIG_STRING}" "$cfg" >> "$COLLECTD_CONF"
-               echo -e "${config}" >> "$COLLECTD_CONF"
+               echo -n -e "${config}" >> "$COLLECTD_CONF"
                printf "%s</Plugin>\n" "${CONFIG_STRING}" >> "$COLLECTD_CONF"
        }
-
-       printf "\n" >> "$COLLECTD_CONF"
 }
 
 process_plugins() {
@@ -274,6 +403,10 @@ process_plugins() {
                        CONFIG_STRING=""
                        process_iptables
                        ;;
+               write_http)
+                       CONFIG_STRING=""
+                       process_write_http
+                       ;;
                *)
                        CONFIG_STRING=""
                        process_generic "$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
@@ -283,7 +416,8 @@ process_plugins() {
 
 process_config() {
        local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
-       local Interval ReadThreads Hostname
+       local Interval ReadThreads WriteThreads Hostname
+       local WriteQueueLimitHigh WriteQueueLimitLow CollectInternalStats
 
        rm -f "$COLLECTD_CONF"
 
@@ -305,10 +439,6 @@ process_config() {
        config_get BaseDir globals BaseDir "/var/run/collectd"
        printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"
 
-       config_get Include globals Include "/tmp/collectd.d"
-       printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
-       mkdir -p "$Include"
-
        config_get PIDFile globals PIDFile "/var/run/collectd.pid"
        printf "PIDFile \"%s\"\n" "$PIDFile" >> "$COLLECTD_CONF"
 
@@ -319,20 +449,47 @@ process_config() {
        printf "TypesDB \"%s\"\n" "$TypesDB" >> "$COLLECTD_CONF"
 
        config_get Interval globals Interval 30
-       printf "Interval \"%s\"\n" "$Interval" >> "$COLLECTD_CONF"
+       printf "Interval %s\n" "$Interval" >> "$COLLECTD_CONF"
 
        config_get ReadThreads globals ReadThreads 2
-       printf "ReadThreads \"%s\"\n" "$ReadThreads" >> "$COLLECTD_CONF"
+       printf "ReadThreads %s\n" "$ReadThreads" >> "$COLLECTD_CONF"
+
+       config_get WriteThreads globals WriteThreads 2
+       printf "WriteThreads %s\n" "$WriteThreads" >> "$COLLECTD_CONF"
+
+       config_get WriteQueueLimitLow globals WriteQueueLimitLow 0
+       [ "$WriteQueueLimitLow" -ne 0 ] \
+               && printf "WriteQueueLimitLow %s\n" "$WriteQueueLimitLow" >> "$COLLECTD_CONF"
+
+       config_get WriteQueueLimitHigh globals WriteQueueLimitHigh 0
+       [ "$WriteQueueLimitHigh" -ne 0 ] \
+               && printf "WriteQueueLimitHigh %s\n" "$WriteQueueLimitHigh" >> "$COLLECTD_CONF"
+
+       config_get_bool CollectInternalStats globals CollectInternalStats 0
+       if [ "$CollectInternalStats" = "0" ]; then
+               printf "CollectInternalStats false\n" >> "$COLLECTD_CONF"
+       else
+               printf "CollectInternalStats true\n" >> "$COLLECTD_CONF"
+       fi
 
        config_get Hostname globals Hostname "$(uname -n)"
        printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
 
+       config_get Include globals Include "/tmp/collectd.d"
+       printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
+       mkdir -p "$Include"
+
        printf "\n" >> "$COLLECTD_CONF"
 
        # PLUGIN CONFIG
        config_foreach process_plugins plugin
 }
 
+service_triggers()
+{
+       procd_add_reload_trigger "collectd"
+}
+
 start_service() {
        process_config
 
@@ -345,3 +502,7 @@ start_service() {
        procd_set_param respawn
        procd_close_instance
 }
+
+reload_service() {
+       restart "$@"
+}