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"
84 local TimeToLive Forward CacheFlush
86 printf "<Plugin network>\n" >> "$COLLECTD_CONF"
87 config_foreach process_network_sections network_listen
"listen"
88 config_foreach process_network_sections network_server
"server"
90 config_get TimeToLive
"$cfg" TimeToLive
91 [ -z "$TimeToLive" ] ||
{
92 printf "\\tTimeToLive %s\n" "${TimeToLive}" >> "$COLLECTD_CONF"
95 config_get CacheFlush
"$cfg" CacheFlush
96 [ -z "$CacheFlush" ] ||
{
97 printf "\\tCacheFlush %s\n" "${CacheFlush}" >> "$COLLECTD_CONF"
100 config_get_bool Forward
"$cfg" Forward
101 if [ "$Forward" = "0" ]; then
102 printf "\\tForward false\n" >> "$COLLECTD_CONF"
104 printf "\\tForward true\n" >> "$COLLECTD_CONF"
107 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
110 process_network_sections
() {
114 local host port output
116 config_get
host "$cfg" host
118 $LOG notice
"No host option in config $cfg defined"
122 if [ "$section" = "server" ]; then
123 output
="Server \"$host\""
125 output
="Listen \"$host\""
128 config_get port
"$cfg" port
129 if [ -z "$port" ]; then
130 printf "\\t%s\n" "${output}" >> "$COLLECTD_CONF"
132 printf "\\t%s \"%s\"\n" "${output}" "${port}" >> "$COLLECTD_CONF"
139 printf "<Plugin iptables>\n" >> "$COLLECTD_CONF"
140 config_foreach process_iptables_sections iptables_match
141 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
144 process_iptables_sections
() {
149 config_get table
"$cfg" table
151 $LOG notice
"No table option in config $cfg defined"
155 config_get chain
"$cfg" chain
157 $LOG notice
"No chain option in config $cfg defined"
161 config_get index
"$cfg" index
163 $LOG notice
"No index option in config $cfg defined"
167 config_get name
"$cfg" name
168 if [ -z "$name" ]; then
169 printf "\\tChain %s %s %s\n" "${table}" "${chain}" "${index}" >> "$COLLECTD_CONF"
171 printf "\\tChain %s %s %s \"%s\"\n" "${table}" "${chain}" "${index}" "${name}">> "$COLLECTD_CONF"
181 CONFIG_LIST
="${CONFIG_LIST}${indent}${option} \"$value\"\n"
191 .
/usr
/share
/libubox
/jshn.sh
193 json_load_file
"$json"
195 json_select string
1>/dev
/null
2>&1
196 if [ $?
-eq 0 ]; then
198 for key
in ${keys}; do
199 json_get_var option
"$key"
200 config_get value
"$cfg" "$option" ""
202 config
="${config}${indent}${option} \"${value}\"\n"
208 json_select bool
1>/dev
/null
2>&1
209 if [ $?
-eq 0 ]; then
211 for key
in ${keys}; do
212 json_get_var option
"$key"
213 config_get_bool value
"$cfg" "$option"
214 if [ "$value" = "0" ]; then
215 config
="${config}${indent}${option} false\n"
218 if [ "$value" = "1" ]; then
219 config
="${config}${indent}${option} true\n"
225 json_select list
1>/dev
/null
2>&1
226 if [ $?
-eq 0 ]; then
228 for key
in ${keys}; do
229 json_get_var option
"$key"
231 config_list_foreach
"$cfg" "$option" add_list_option
"$option" "$indent"
232 config
="${config}${CONFIG_LIST}"
237 [ -z "$config" ] ||
{
238 printf "%s<Plugin %s>\n" "${CONFIG_STRING}" "$cfg" >> "$COLLECTD_CONF"
239 echo -n -e "${config}" >> "$COLLECTD_CONF"
240 printf "%s</Plugin>\n" "${CONFIG_STRING}" >> "$COLLECTD_CONF"
247 local enable keys key option value
249 config_get
enable "$cfg" enable 0
250 [ "$enable" = "1" ] ||
return 0
252 [ -f "/usr/lib/collectd/$cfg.so" ] ||
{
253 $LOG notice
"Plugin collectd-mod-$cfg not installed"
257 [ -f "/usr/share/collectd/plugin/$cfg.json" ] ||
{
258 $LOG notice
"Configuration definition file for $cfg not found"
262 printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
274 process_network
"$cfg"
282 process_generic
"$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
288 local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
289 local Interval ReadThreads WriteThreads Hostname
290 local WriteQueueLimitHigh WriteQueueLimitLow CollectInternalStats
292 rm -f "$COLLECTD_CONF"
294 [ -f /etc
/config
/collectd
] ||
{
295 $LOG notice
"UCI config not found"
299 config_get alt_config_file globals alt_config_file
301 # If "alt_config_file" specified, use that instead
302 [ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
303 rm -f "$COLLECTD_CONF"
304 ln -s "$alt_config_file" "$COLLECTD_CONF"
309 config_get BaseDir globals BaseDir
"/var/run/collectd"
310 printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"
312 config_get PIDFile globals PIDFile
"/var/run/collectd.pid"
313 printf "PIDFile \"%s\"\n" "$PIDFile" >> "$COLLECTD_CONF"
315 config_get PluginDir globals PluginDir
"/usr/lib/collectd"
316 printf "PluginDir \"%s\"\n" "$PluginDir" >> "$COLLECTD_CONF"
318 config_get TypesDB globals TypesDB
"/usr/share/collectd/types.db"
319 printf "TypesDB \"%s\"\n" "$TypesDB" >> "$COLLECTD_CONF"
321 config_get Interval globals Interval
30
322 printf "Interval %s\n" "$Interval" >> "$COLLECTD_CONF"
324 config_get ReadThreads globals ReadThreads
2
325 printf "ReadThreads %s\n" "$ReadThreads" >> "$COLLECTD_CONF"
327 config_get WriteThreads globals WriteThreads
2
328 printf "WriteThreads %s\n" "$WriteThreads" >> "$COLLECTD_CONF"
330 config_get WriteQueueLimitLow globals WriteQueueLimitLow
0
331 [ "$WriteQueueLimitLow" -ne 0 ] \
332 && printf "WriteQueueLimitLow %s\n" "$WriteQueueLimitLow" >> "$COLLECTD_CONF"
334 config_get WriteQueueLimitHigh globals WriteQueueLimitHigh
0
335 [ "$WriteQueueLimitHigh" -ne 0 ] \
336 && printf "WriteQueueLimitHigh %s\n" "$WriteQueueLimitHigh" >> "$COLLECTD_CONF"
338 config_get_bool CollectInternalStats globals CollectInternalStats
0
339 if [ "$CollectInternalStats" = "0" ]; then
340 printf "CollectInternalStats false\n" >> "$COLLECTD_CONF"
342 printf "CollectInternalStats true\n" >> "$COLLECTD_CONF"
345 config_get Hostname globals Hostname
"$(uname -n)"
346 printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
348 config_get Include globals Include
"/tmp/collectd.d"
349 printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
352 printf "\n" >> "$COLLECTD_CONF"
355 config_foreach process_plugins plugin
360 procd_add_reload_trigger
"collectd"
367 procd_set_param
command /usr
/sbin
/collectd
368 procd_append_param
command -C "$COLLECTD_CONF"
369 procd_append_param
command -f # don't daemonize
370 procd_set_param nice
"$NICEPRIO"
371 procd_set_param stderr
1
372 procd_set_param respawn