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_sections
() {
160 local host port output
162 config_get
host "$cfg" host
164 $LOG notice
"No host option in config $cfg defined"
168 if [ "$section" = "server" ]; then
169 output
="Server \"$host\""
171 output
="Listen \"$host\""
174 config_get port
"$cfg" port
175 if [ -z "$port" ]; then
176 printf "\\t%s\n" "${output}" >> "$COLLECTD_CONF"
178 printf "\\t%s \"%s\"\n" "${output}" "${port}" >> "$COLLECTD_CONF"
185 printf "<Plugin iptables>\n" >> "$COLLECTD_CONF"
186 config_foreach process_iptables_sections iptables_match
187 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
190 process_iptables_sections
() {
195 config_get table
"$cfg" table
197 $LOG notice
"No table option in config $cfg defined"
201 config_get chain
"$cfg" chain
203 $LOG notice
"No chain option in config $cfg defined"
207 config_get index
"$cfg" index
209 $LOG notice
"No index option in config $cfg defined"
213 config_get name
"$cfg" name
214 if [ -z "$name" ]; then
215 printf "\\tChain %s %s %s\n" "${table}" "${chain}" "${index}" >> "$COLLECTD_CONF"
217 printf "\\tChain %s %s %s \"%s\"\n" "${table}" "${chain}" "${index}" "${name}">> "$COLLECTD_CONF"
227 CONFIG_LIST
="${CONFIG_LIST}${indent}${option} \"$value\"\n"
237 .
/usr
/share
/libubox
/jshn.sh
239 json_load_file
"$json"
241 json_select string
1>/dev
/null
2>&1
242 if [ $?
-eq 0 ]; then
244 for key
in ${keys}; do
245 json_get_var option
"$key"
246 config_get value
"$cfg" "$option" ""
248 config
="${config}${indent}${option} \"${value}\"\n"
254 json_select bool
1>/dev
/null
2>&1
255 if [ $?
-eq 0 ]; then
257 for key
in ${keys}; do
258 json_get_var option
"$key"
259 config_get_bool value
"$cfg" "$option"
260 if [ "$value" = "0" ]; then
261 config
="${config}${indent}${option} false\n"
264 if [ "$value" = "1" ]; then
265 config
="${config}${indent}${option} true\n"
271 json_select list
1>/dev
/null
2>&1
272 if [ $?
-eq 0 ]; then
274 for key
in ${keys}; do
275 json_get_var option
"$key"
277 config_list_foreach
"$cfg" "$option" add_list_option
"$option" "$indent"
278 config
="${config}${CONFIG_LIST}"
283 [ -z "$config" ] ||
{
284 printf "%s<Plugin %s>\n" "${CONFIG_STRING}" "$cfg" >> "$COLLECTD_CONF"
285 echo -n -e "${config}" >> "$COLLECTD_CONF"
286 printf "%s</Plugin>\n" "${CONFIG_STRING}" >> "$COLLECTD_CONF"
293 local enable keys key option value
295 config_get
enable "$cfg" enable 0
296 [ "$enable" = "1" ] ||
return 0
298 [ -f "/usr/lib/collectd/$cfg.so" ] ||
{
299 $LOG notice
"Plugin collectd-mod-$cfg not installed"
303 [ -f "/usr/share/collectd/plugin/$cfg.json" ] ||
{
304 $LOG notice
"Configuration definition file for $cfg not found"
308 printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
320 process_network
"$cfg"
332 process_generic
"$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
338 local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
339 local Interval ReadThreads WriteThreads Hostname
340 local WriteQueueLimitHigh WriteQueueLimitLow CollectInternalStats
342 rm -f "$COLLECTD_CONF"
344 [ -f /etc
/config
/collectd
] ||
{
345 $LOG notice
"UCI config not found"
349 config_get alt_config_file globals alt_config_file
351 # If "alt_config_file" specified, use that instead
352 [ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
353 rm -f "$COLLECTD_CONF"
354 ln -s "$alt_config_file" "$COLLECTD_CONF"
359 config_get BaseDir globals BaseDir
"/var/run/collectd"
360 printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"
362 config_get PIDFile globals PIDFile
"/var/run/collectd.pid"
363 printf "PIDFile \"%s\"\n" "$PIDFile" >> "$COLLECTD_CONF"
365 config_get PluginDir globals PluginDir
"/usr/lib/collectd"
366 printf "PluginDir \"%s\"\n" "$PluginDir" >> "$COLLECTD_CONF"
368 config_get TypesDB globals TypesDB
"/usr/share/collectd/types.db"
369 printf "TypesDB \"%s\"\n" "$TypesDB" >> "$COLLECTD_CONF"
371 config_get Interval globals Interval
30
372 printf "Interval %s\n" "$Interval" >> "$COLLECTD_CONF"
374 config_get ReadThreads globals ReadThreads
2
375 printf "ReadThreads %s\n" "$ReadThreads" >> "$COLLECTD_CONF"
377 config_get WriteThreads globals WriteThreads
2
378 printf "WriteThreads %s\n" "$WriteThreads" >> "$COLLECTD_CONF"
380 config_get WriteQueueLimitLow globals WriteQueueLimitLow
0
381 [ "$WriteQueueLimitLow" -ne 0 ] \
382 && printf "WriteQueueLimitLow %s\n" "$WriteQueueLimitLow" >> "$COLLECTD_CONF"
384 config_get WriteQueueLimitHigh globals WriteQueueLimitHigh
0
385 [ "$WriteQueueLimitHigh" -ne 0 ] \
386 && printf "WriteQueueLimitHigh %s\n" "$WriteQueueLimitHigh" >> "$COLLECTD_CONF"
388 config_get_bool CollectInternalStats globals CollectInternalStats
0
389 if [ "$CollectInternalStats" = "0" ]; then
390 printf "CollectInternalStats false\n" >> "$COLLECTD_CONF"
392 printf "CollectInternalStats true\n" >> "$COLLECTD_CONF"
395 config_get Hostname globals Hostname
"$(uname -n)"
396 printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
398 config_get Include globals Include
"/tmp/collectd.d"
399 printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
402 printf "\n" >> "$COLLECTD_CONF"
405 config_foreach process_plugins plugin
410 procd_add_reload_trigger
"collectd"
417 procd_set_param
command /usr
/sbin
/collectd
418 procd_append_param
command -C "$COLLECTD_CONF"
419 procd_append_param
command -f # don't daemonize
420 procd_set_param nice
"$NICEPRIO"
421 procd_set_param stderr
1
422 procd_set_param respawn