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"
15 printf "<Plugin exec>\n" >> "$COLLECTD_CONF"
16 config_foreach process_exec_sections exec_input
"Exec"
17 config_foreach process_exec_sections exec_notify
"NotificationExec"
18 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
21 process_exec_sections
() {
25 local cmdline cmduser cmdgroup
27 config_get cmdline
"$cfg" cmdline
28 [ -z "$cmdline" ] && {
29 $LOG notice
"No cmdline option in config $cfg defined"
33 config_get cmduser
"$cfg" cmduser
34 [ -z "$cmduser" ] && {
35 $LOG notice
"No cmduser option in config $cfg defined"
39 config_get cmdgroup
"$cfg" cmdgroup
40 if [ -z "$cmdgroup" ]; then
41 printf "\\t%s \"%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdline}" >> "$COLLECTD_CONF"
43 printf "\\t%s \"%s:%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdgroup}" "${cmdline}" >> "$COLLECTD_CONF"
48 printf "<Plugin curl>\n" >> "$COLLECTD_CONF"
49 config_foreach process_curl_page curl_page
50 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
58 config_get name
"$cfg" name
60 $LOG notice
"No name option in config $cfg defined"
64 config_get url
"$cfg" url
66 $LOG notice
"No URL option in config $cfg defined"
70 printf "\\t<Page \"%s\">\n" "${name}" >> "$COLLECTD_CONF"
71 printf "\\t\\tURL \"%s\"\n" "${url}" >> "$COLLECTD_CONF"
72 printf "\\t\\tMeasureResponseTime true\n" >> "$COLLECTD_CONF"
73 printf "\\t</Page>\n" >> "$COLLECTD_CONF"
79 local TimeToLive Forward CacheFlush
81 printf "<Plugin network>\n" >> "$COLLECTD_CONF"
82 config_foreach process_network_sections network_listen
"listen"
83 config_foreach process_network_sections network_server
"server"
85 config_get TimeToLive
"$cfg" TimeToLive
86 [ -z "$TimeToLive" ] ||
{
87 printf "\\tTimeToLive %s\n" "${TimeToLive}" >> "$COLLECTD_CONF"
90 config_get CacheFlush
"$cfg" CacheFlush
91 [ -z "$CacheFlush" ] ||
{
92 printf "\\tCacheFlush %s\n" "${CacheFlush}" >> "$COLLECTD_CONF"
95 config_get_bool Forward
"$cfg" Forward
96 if [ "$value" = "0" ]; then
97 printf "\\tForward false\n" >> "$COLLECTD_CONF"
99 printf "\\tForward true\n" >> "$COLLECTD_CONF"
102 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
105 process_network_sections
() {
109 local host port output
111 config_get
host "$cfg" host
113 $LOG notice
"No host option in config $cfg defined"
117 if [ "$section" = "server" ]; then
118 output
="Server \"$host\""
120 output
="Listen \"$host\""
123 config_get port
"$cfg" port
124 if [ -z "$port" ]; then
125 printf "\\t%s\n" "${output}" >> "$COLLECTD_CONF"
127 printf "\\t%s \"%s\"\n" "${output}" "${port}" >> "$COLLECTD_CONF"
134 printf "<Plugin iptables>\n" >> "$COLLECTD_CONF"
135 config_foreach process_iptables_sections iptables_match
136 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
139 process_iptables_sections
() {
144 config_get table
"$cfg" table
146 $LOG notice
"No table option in config $cfg defined"
150 config_get chain
"$cfg" chain
152 $LOG notice
"No chain option in config $cfg defined"
156 config_get index
"$cfg" index
158 $LOG notice
"No index option in config $cfg defined"
162 config_get name
"$cfg" name
163 if [ -z "$name" ]; then
164 printf "\\tChain %s %s %s\n" "${table}" "${chain}" "${index}" >> "$COLLECTD_CONF"
166 printf "\\tChain %s %s %s \"%s\"\n" "${table}" "${chain}" "${index}" "${name}">> "$COLLECTD_CONF"
176 CONFIG_LIST
="${CONFIG_LIST}${indent}${option} \"$value\"\n"
186 .
/usr
/share
/libubox
/jshn.sh
188 json_load_file
"$json"
190 json_select string
1>/dev
/null
2>&1
191 if [ $?
-eq 0 ]; then
193 for key
in ${keys}; do
194 json_get_var option
"$key"
195 config_get value
"$cfg" "$option" ""
197 config
="${config}${indent}${option} \"${value}\"\n"
203 json_select bool
1>/dev
/null
2>&1
204 if [ $?
-eq 0 ]; then
206 for key
in ${keys}; do
207 json_get_var option
"$key"
208 config_get_bool value
"$cfg" "$option"
209 if [ "$value" = "0" ]; then
210 config
="${config}${indent}${option} false\n"
213 if [ "$value" = "1" ]; then
214 config
="${config}${indent}${option} true\n"
220 json_select list
1>/dev
/null
2>&1
221 if [ $?
-eq 0 ]; then
223 for key
in ${keys}; do
224 json_get_var option
"$key"
226 config_list_foreach
"$cfg" "$option" add_list_option
"$option" "$indent"
227 config
="${config}${CONFIG_LIST}"
232 [ -z "$config" ] ||
{
233 printf "%s<Plugin %s>\n" "${CONFIG_STRING}" "$cfg" >> "$COLLECTD_CONF"
234 echo -e "${config}" >> "$COLLECTD_CONF"
235 printf "%s</Plugin>\n" "${CONFIG_STRING}" >> "$COLLECTD_CONF"
238 printf "\n" >> "$COLLECTD_CONF"
244 local enable keys key option value
246 config_get
enable "$cfg" enable 0
247 [ "$enable" = "1" ] ||
return 0
249 [ -f "/usr/lib/collectd/$cfg.so" ] ||
{
250 $LOG notice
"Plugin collectd-mod-$cfg not installed"
254 [ -f "/usr/share/collectd/plugin/$cfg.json" ] ||
{
255 $LOG notice
"Configuration definition file for $cfg not found"
259 printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
271 process_network
"$cfg"
279 process_generic
"$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
285 local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
286 local Interval ReadThreads Hostname
288 rm -f "$COLLECTD_CONF"
290 [ -f /etc
/config
/collectd
] ||
{
291 $LOG notice
"UCI config not found"
295 config_get alt_config_file globals alt_config_file
297 # If "alt_config_file" specified, use that instead
298 [ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
299 rm -f "$COLLECTD_CONF"
300 ln -s "$alt_config_file" "$COLLECTD_CONF"
305 config_get BaseDir globals BaseDir
"/var/run/collectd"
306 printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"
308 config_get Include globals Include
"/tmp/collectd.d"
309 printf "Include \"%s\"\n" "$Include" >> "$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 Hostname globals Hostname
"$(uname -n)"
328 printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
330 printf "\n" >> "$COLLECTD_CONF"
333 config_foreach process_plugins plugin
338 procd_add_reload_trigger
"collectd"
345 procd_set_param
command /usr
/sbin
/collectd
346 procd_append_param
command -C "$COLLECTD_CONF"
347 procd_append_param
command -f # don't daemonize
348 procd_set_param nice
"$NICEPRIO"
349 procd_set_param stderr
1
350 procd_set_param respawn