1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2016 OpenWrt.org
8 COLLECTD_CONF
="/tmp/collectd.conf"
9 LOG
="logger -t collectd[$$] -p"
14 [ -d /usr
/libexec
/collectd
] && {
15 find /usr
/libexec
/collectd
! -perm 0500 -exec chmod 0500 '{}' '+'
16 find /usr
/libexec
/collectd
! \
( -user nobody
-a -group nogroup \
) -exec chown nobody
:nogroup
'{}' '+'
20 printf "<Plugin exec>\n" >> "$COLLECTD_CONF"
21 config_foreach process_exec_sections exec_input
"Exec"
22 config_foreach process_exec_sections exec_notify
"NotificationExec"
23 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
26 process_exec_sections
() {
30 local cmdline cmduser cmdgroup
32 config_get cmdline
"$cfg" cmdline
33 [ -z "$cmdline" ] && {
34 $LOG notice
"No cmdline option in config $cfg defined"
38 config_get cmduser
"$cfg" cmduser
39 [ -z "$cmduser" ] && {
40 $LOG notice
"No cmduser option in config $cfg defined"
44 config_get cmdgroup
"$cfg" cmdgroup
45 if [ -z "$cmdgroup" ]; then
46 printf "\\t%s \"%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdline}" >> "$COLLECTD_CONF"
48 printf "\\t%s \"%s:%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdgroup}" "${cmdline}" >> "$COLLECTD_CONF"
53 printf "<Plugin curl>\n" >> "$COLLECTD_CONF"
54 config_foreach process_curl_page curl_page
55 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
63 config_get name
"$cfg" name
65 $LOG notice
"No name option in config $cfg defined"
69 config_get url
"$cfg" url
71 $LOG notice
"No URL option in config $cfg defined"
75 printf "\\t<Page \"%s\">\n" "${name}" >> "$COLLECTD_CONF"
76 printf "\\t\\tURL \"%s\"\n" "${url}" >> "$COLLECTD_CONF"
77 printf "\\t\\tMeasureResponseTime true\n" >> "$COLLECTD_CONF"
78 printf "\\t</Page>\n" >> "$COLLECTD_CONF"
81 process_write_http
() {
82 printf "<Plugin write_http>\n" >> "$COLLECTD_CONF"
83 config_foreach process_write_http_node write_http_node
84 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
87 process_write_http_node
() {
90 local name URL Format User Password Timeout BufferSize
92 config_get name
"$cfg" name
94 $LOG notice
"No name option in config $cfg defined"
97 config_get URL
"$cfg" URL
99 $LOG notice
"No URL option in config $cfg defined"
102 config_get Format
"$cfg" Format
103 config_get User
"$cfg" User
104 config_get Password
"$cfg" Password
105 config_get Timeout
"$cfg" Timeout
106 config_get BufferSize
"$cfg" BufferSize
107 printf "\\t<Node \"%s\">\n" "${name}" >> "$COLLECTD_CONF"
108 printf "\\t\\tURL \"%s\"\n" "${URL}" >> "$COLLECTD_CONF"
109 [ -z "$Format" ] ||
{
110 printf "\\t\\tFormat \"%s\"\n" "${Format}" >> "$COLLECTD_CONF"
113 printf "\\t\\tUser \"%s\"\n" "${User}" >> "$COLLECTD_CONF"
115 [ -z "$Password" ] ||
{
116 printf "\\t\\tPassword \"%s\"\n" "${Password}" >> "$COLLECTD_CONF"
118 [ -z "$Timeout" ] ||
{
119 printf "\\t\\tTimeout \%s\n" "${Timeout}" >> "$COLLECTD_CONF"
121 [ -z "$BufferSize" ] ||
{
122 printf "\\t\\tBufferSize \%s\n" "${BufferSize}" >> "$COLLECTD_CONF"
124 printf "\\t</Node>\n" >> "$COLLECTD_CONF"
130 local TimeToLive Forward CacheFlush
132 printf "<Plugin network>\n" >> "$COLLECTD_CONF"
133 config_foreach process_network_sections network_listen
"listen"
134 config_foreach process_network_sections network_server
"server"
136 config_get TimeToLive
"$cfg" TimeToLive
137 [ -z "$TimeToLive" ] ||
{
138 printf "\\tTimeToLive %s\n" "${TimeToLive}" >> "$COLLECTD_CONF"
141 config_get CacheFlush
"$cfg" CacheFlush
142 [ -z "$CacheFlush" ] ||
{
143 printf "\\tCacheFlush %s\n" "${CacheFlush}" >> "$COLLECTD_CONF"
146 config_get_bool Forward
"$cfg" Forward
147 if [ "$Forward" = "0" ]; then
148 printf "\\tForward false\n" >> "$COLLECTD_CONF"
150 printf "\\tForward true\n" >> "$COLLECTD_CONF"
153 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
156 process_network_server
() {
158 local SecurityLevel
="$2"
160 local Username Password ResolveInterval
162 config_get Username
"$cfg" Username
163 [ -z "$Username" ] && {
164 $LOG notice
"SecurityLevel set to '$SecurityLevel' but no option Username found in config '$cfg'"
167 printf "\\t\\tUsername \"%s\"\n" "${Username}" >> "$COLLECTD_CONF"
169 config_get Password
"$cfg" Password
170 [ -z "$Password" ] && {
171 $LOG notice
"SecurityLevel set to '$SecurityLevel' but no option Password found in config '$cfg'"
174 printf "\\t\\tPassword \"%s\"\n" "${Password}" >> "$COLLECTD_CONF"
176 config_get ResolveInterval
"$cfg" ResolveInterval
177 [ -z "$ResolveInterval" ] ||
{
178 printf "\\t\\tResolveInterval \"%s\"\n" "${ResolveInterval}" >> "$COLLECTD_CONF"
182 process_network_listen
() {
185 local auth_file
="/tmp/collectd-auth-${cfg}.conf"
188 rm -rf "${auth_file}"
190 echo "$1" >> "${auth_file}"
193 config_list_foreach
"$cfg" auth add_auth
195 [ -z "$auth_set" ] && {
196 $LOG notice
"SecurityLevel set to '$SecurityLevel' but no list option auth found in config '$cfg'"
200 printf "\\t\\tAuthFile \"%s\"\n" "${auth_file}" >> "$COLLECTD_CONF"
203 process_network_sections
() {
207 local host port output rvalue SecurityLevel Interface
209 config_get
host "$cfg" host
211 $LOG notice
"No host option in config $cfg defined"
215 if [ "$section" = "server" ]; then
216 output
="Server \"$host\""
218 output
="Listen \"$host\""
221 config_get port
"$cfg" port
222 if [ -z "$port" ]; then
223 printf "\\t<%s>\n" "${output}" >> "$COLLECTD_CONF"
225 printf "\\t<%s \"%s\">\n" "${output}" "${port}" >> "$COLLECTD_CONF"
228 config_get SecurityLevel
"$cfg" SecurityLevel
'None'
229 [ -z "$SecurityLevel" ] ||
{
230 printf "\\t\\tSecurityLevel \"%s\"\n" "${SecurityLevel}" >> "$COLLECTD_CONF"
233 if [ "$SecurityLevel" != "None" ]; then
236 process_network_server
"$cfg" "$SecurityLevel"
238 [ "$rvalue" != 0 ] && return 0
241 process_network_listen
"$cfg" "$SecurityLevel"
243 [ "$rvalue" != 0 ] && return 0
247 $LOG notice
"SecurityLevel set to 'None' for '$cfg'"
250 config_get Interface
"$cfg" Interface
251 [ -z "$Interface" ] ||
{
252 printf "\\t\\tInterface \"%s\"\n" "${Interface}" >> "$COLLECTD_CONF"
255 if [ "$section" = "server" ]; then
256 printf "\\t</Server>\n" >> "$COLLECTD_CONF"
258 printf "\\t</Listen>\n" >> "$COLLECTD_CONF"
265 printf "<Plugin iptables>\n" >> "$COLLECTD_CONF"
266 config_foreach process_iptables_sections iptables_match
267 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
270 process_iptables_sections
() {
275 config_get table
"$cfg" table
277 $LOG notice
"No table option in config $cfg defined"
281 config_get chain
"$cfg" chain
283 $LOG notice
"No chain option in config $cfg defined"
287 config_get index
"$cfg" index
289 $LOG notice
"No index option in config $cfg defined"
293 config_get name
"$cfg" name
294 if [ -z "$name" ]; then
295 printf "\\tChain %s %s %s\n" "${table}" "${chain}" "${index}" >> "$COLLECTD_CONF"
297 printf "\\tChain %s %s %s \"%s\"\n" "${table}" "${chain}" "${index}" "${name}">> "$COLLECTD_CONF"
307 CONFIG_LIST
="${CONFIG_LIST}${indent}${option} \"$value\"\n"
317 .
/usr
/share
/libubox
/jshn.sh
319 json_load_file
"$json"
321 json_select string
1>/dev
/null
2>&1
322 if [ $?
-eq 0 ]; then
324 for key
in ${keys}; do
325 json_get_var option
"$key"
326 config_get value
"$cfg" "$option" ""
328 config
="${config}${indent}${option} \"${value}\"\n"
334 json_select bool
1>/dev
/null
2>&1
335 if [ $?
-eq 0 ]; then
337 for key
in ${keys}; do
338 json_get_var option
"$key"
339 config_get_bool value
"$cfg" "$option"
340 if [ "$value" = "0" ]; then
341 config
="${config}${indent}${option} false\n"
344 if [ "$value" = "1" ]; then
345 config
="${config}${indent}${option} true\n"
351 json_select list
1>/dev
/null
2>&1
352 if [ $?
-eq 0 ]; then
354 for key
in ${keys}; do
355 json_get_var option
"$key"
357 config_list_foreach
"$cfg" "$option" add_list_option
"$option" "$indent"
358 config
="${config}${CONFIG_LIST}"
363 [ -z "$config" ] ||
{
364 printf "%s<Plugin %s>\n" "${CONFIG_STRING}" "$cfg" >> "$COLLECTD_CONF"
365 echo -n -e "${config}" >> "$COLLECTD_CONF"
366 printf "%s</Plugin>\n" "${CONFIG_STRING}" >> "$COLLECTD_CONF"
373 local enable keys key option value
375 config_get
enable "$cfg" enable 0
376 [ "$enable" = "1" ] ||
return 0
378 [ -f "/usr/lib/collectd/$cfg.so" ] ||
{
379 $LOG notice
"Plugin collectd-mod-$cfg not installed"
383 [ -f "/usr/share/collectd/plugin/$cfg.json" ] ||
{
384 $LOG notice
"Configuration definition file for $cfg not found"
388 printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
400 process_network
"$cfg"
412 process_generic
"$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
418 local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
419 local Interval ReadThreads WriteThreads Hostname
420 local WriteQueueLimitHigh WriteQueueLimitLow CollectInternalStats
422 rm -f "$COLLECTD_CONF"
424 [ -f /etc
/config
/collectd
] ||
{
425 $LOG notice
"UCI config not found"
429 config_get alt_config_file globals alt_config_file
431 # If "alt_config_file" specified, use that instead
432 [ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
433 rm -f "$COLLECTD_CONF"
434 ln -s "$alt_config_file" "$COLLECTD_CONF"
439 config_get BaseDir globals BaseDir
"/var/run/collectd"
440 printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"
442 config_get PIDFile globals PIDFile
"/var/run/collectd.pid"
443 printf "PIDFile \"%s\"\n" "$PIDFile" >> "$COLLECTD_CONF"
445 config_get PluginDir globals PluginDir
"/usr/lib/collectd"
446 printf "PluginDir \"%s\"\n" "$PluginDir" >> "$COLLECTD_CONF"
448 config_get TypesDB globals TypesDB
"/usr/share/collectd/types.db"
449 printf "TypesDB \"%s\"\n" "$TypesDB" >> "$COLLECTD_CONF"
451 config_get Interval globals Interval
30
452 printf "Interval %s\n" "$Interval" >> "$COLLECTD_CONF"
454 config_get ReadThreads globals ReadThreads
2
455 printf "ReadThreads %s\n" "$ReadThreads" >> "$COLLECTD_CONF"
457 config_get WriteThreads globals WriteThreads
2
458 printf "WriteThreads %s\n" "$WriteThreads" >> "$COLLECTD_CONF"
460 config_get WriteQueueLimitLow globals WriteQueueLimitLow
0
461 [ "$WriteQueueLimitLow" -ne 0 ] \
462 && printf "WriteQueueLimitLow %s\n" "$WriteQueueLimitLow" >> "$COLLECTD_CONF"
464 config_get WriteQueueLimitHigh globals WriteQueueLimitHigh
0
465 [ "$WriteQueueLimitHigh" -ne 0 ] \
466 && printf "WriteQueueLimitHigh %s\n" "$WriteQueueLimitHigh" >> "$COLLECTD_CONF"
468 config_get_bool CollectInternalStats globals CollectInternalStats
0
469 if [ "$CollectInternalStats" = "0" ]; then
470 printf "CollectInternalStats false\n" >> "$COLLECTD_CONF"
472 printf "CollectInternalStats true\n" >> "$COLLECTD_CONF"
475 config_get Hostname globals Hostname
"$(uname -n)"
476 printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
478 config_get Include globals Include
"/tmp/collectd.d"
479 printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
482 printf "\n" >> "$COLLECTD_CONF"
485 config_foreach process_plugins plugin
490 procd_add_reload_trigger
"collectd"
497 procd_set_param
command /usr
/sbin
/collectd
498 procd_append_param
command -C "$COLLECTD_CONF"
499 procd_append_param
command -f # don't daemonize
500 procd_set_param nice
"$NICEPRIO"
501 procd_set_param stderr
1
502 procd_set_param respawn