Merge pull request #10037 from TDT-AG/pr/20190920-collectd
[feed/packages.git] / utils / collectd / files / collectd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2016 OpenWrt.org
3
4 START=80
5 STOP=10
6
7 USE_PROCD=1
8 COLLECTD_CONF="/tmp/collectd.conf"
9 LOG="logger -t collectd[$$] -p"
10 NICEPRIO=5
11
12 CONFIG_STRING=""
13
14 process_exec() {
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"
19 }
20
21 process_exec_sections() {
22 local cfg="$1"
23 local section="$2"
24
25 local cmdline cmduser cmdgroup
26
27 config_get cmdline "$cfg" cmdline
28 [ -z "$cmdline" ] && {
29 $LOG notice "No cmdline option in config $cfg defined"
30 return 0
31 }
32
33 config_get cmduser "$cfg" cmduser
34 [ -z "$cmduser" ] && {
35 $LOG notice "No cmduser option in config $cfg defined"
36 return 0
37 }
38
39 config_get cmdgroup "$cfg" cmdgroup
40 if [ -z "$cmdgroup" ]; then
41 printf "\\t%s \"%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdline}" >> "$COLLECTD_CONF"
42 else
43 printf "\\t%s \"%s:%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdgroup}" "${cmdline}" >> "$COLLECTD_CONF"
44 fi
45 }
46
47 process_curl() {
48 printf "<Plugin curl>\n" >> "$COLLECTD_CONF"
49 config_foreach process_curl_page curl_page
50 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
51 }
52
53 process_curl_page() {
54 local cfg="$1"
55
56 local name url
57
58 config_get name "$cfg" name
59 [ -z "$name" ] && {
60 $LOG notice "No name option in config $cfg defined"
61 return 0
62 }
63
64 config_get url "$cfg" url
65 [ -z "$url" ] && {
66 $LOG notice "No URL option in config $cfg defined"
67 return 0
68 }
69
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"
74 }
75
76 process_network() {
77 local cfg="$1"
78
79 local TimeToLive Forward CacheFlush
80
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"
84
85 config_get TimeToLive "$cfg" TimeToLive
86 [ -z "$TimeToLive" ] || {
87 printf "\\tTimeToLive %s\n" "${TimeToLive}" >> "$COLLECTD_CONF"
88 }
89
90 config_get CacheFlush "$cfg" CacheFlush
91 [ -z "$CacheFlush" ] || {
92 printf "\\tCacheFlush %s\n" "${CacheFlush}" >> "$COLLECTD_CONF"
93 }
94
95 config_get_bool Forward "$cfg" Forward
96 if [ "$value" = "0" ]; then
97 printf "\\tForward false\n" >> "$COLLECTD_CONF"
98 else
99 printf "\\tForward true\n" >> "$COLLECTD_CONF"
100 fi
101
102 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
103 }
104
105 process_network_sections() {
106 local cfg="$1"
107 local section="$2"
108
109 local host port output
110
111 config_get host "$cfg" host
112 [ -z "$host" ] && {
113 $LOG notice "No host option in config $cfg defined"
114 return 0
115 }
116
117 if [ "$section" = "server" ]; then
118 output="Server \"$host\""
119 else
120 output="Listen \"$host\""
121 fi
122
123 config_get port "$cfg" port
124 if [ -z "$port" ]; then
125 printf "\\t%s\n" "${output}" >> "$COLLECTD_CONF"
126 else
127 printf "\\t%s \"%s\"\n" "${output}" "${port}" >> "$COLLECTD_CONF"
128 fi
129 }
130
131 process_iptables() {
132 local cfg="$1"
133
134 printf "<Plugin iptables>\n" >> "$COLLECTD_CONF"
135 config_foreach process_iptables_sections iptables_match
136 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
137 }
138
139 process_iptables_sections() {
140 local cfg="$1"
141
142 local table chain
143
144 config_get table "$cfg" table
145 [ -z "$table" ] && {
146 $LOG notice "No table option in config $cfg defined"
147 return 0
148 }
149
150 config_get chain "$cfg" chain
151 [ -z "$chain" ] && {
152 $LOG notice "No chain option in config $cfg defined"
153 return 0
154 }
155
156 config_get index "$cfg" index
157 [ -z "$index" ] && {
158 $LOG notice "No index option in config $cfg defined"
159 return 0
160 }
161
162 config_get name "$cfg" name
163 if [ -z "$name" ]; then
164 printf "\\tChain %s %s %s\n" "${table}" "${chain}" "${index}" >> "$COLLECTD_CONF"
165 else
166 printf "\\tChain %s %s %s \"%s\"\n" "${table}" "${chain}" "${index}" "${name}">> "$COLLECTD_CONF"
167 fi
168 }
169
170 CONFIG_LIST=""
171 add_list_option() {
172 local value="$1"
173 local option="$2"
174 local indent="$3"
175
176 CONFIG_LIST="${CONFIG_LIST}${indent}${option} \"$value\"\n"
177 }
178
179 process_generic() {
180 local cfg="$1"
181 local indent="$2"
182 local json="$3"
183
184 local config=""
185
186 . /usr/share/libubox/jshn.sh
187 json_init
188 json_load_file "$json"
189
190 json_select string 1>/dev/null 2>&1
191 if [ $? -eq 0 ]; then
192 json_get_keys keys
193 for key in ${keys}; do
194 json_get_var option "$key"
195 config_get value "$cfg" "$option" ""
196 [ -z "$value" ] || {
197 config="${config}${indent}${option} \"${value}\"\n"
198 }
199 done
200 json_select ..
201 fi
202
203 json_select bool 1>/dev/null 2>&1
204 if [ $? -eq 0 ]; then
205 json_get_keys keys
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"
211 fi
212
213 if [ "$value" = "1" ]; then
214 config="${config}${indent}${option} true\n"
215 fi
216 done
217 json_select ..
218 fi
219
220 json_select list 1>/dev/null 2>&1
221 if [ $? -eq 0 ]; then
222 json_get_keys keys
223 for key in ${keys}; do
224 json_get_var option "$key"
225 CONFIG_LIST=""
226 config_list_foreach "$cfg" "$option" add_list_option "$option" "$indent"
227 config="${config}${CONFIG_LIST}"
228 done
229 json_select ..
230 fi
231
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"
236 }
237
238 printf "\n" >> "$COLLECTD_CONF"
239 }
240
241 process_plugins() {
242 local cfg="$1"
243
244 local enable keys key option value
245
246 config_get enable "$cfg" enable 0
247 [ "$enable" = "1" ] || return 0
248
249 [ -f "/usr/lib/collectd/$cfg.so" ] || {
250 $LOG notice "Plugin collectd-mod-$cfg not installed"
251 return 0
252 }
253
254 [ -f "/usr/share/collectd/plugin/$cfg.json" ] || {
255 $LOG notice "Configuration definition file for $cfg not found"
256 return 0
257 }
258
259 printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
260 case "$cfg" in
261 exec)
262 CONFIG_STRING=""
263 process_exec
264 ;;
265 curl)
266 CONFIG_STRING=""
267 process_curl
268 ;;
269 network)
270 CONFIG_STRING=""
271 process_network "$cfg"
272 ;;
273 iptables)
274 CONFIG_STRING=""
275 process_iptables
276 ;;
277 *)
278 CONFIG_STRING=""
279 process_generic "$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
280 ;;
281 esac
282 }
283
284 process_config() {
285 local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
286 local Interval ReadThreads Hostname
287
288 rm -f "$COLLECTD_CONF"
289
290 [ -f /etc/config/collectd ] || {
291 $LOG notice "UCI config not found"
292 return 0
293 }
294 config_load collectd
295 config_get alt_config_file globals alt_config_file
296
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"
301 return 0
302 }
303
304 # GOBAL CONFIG
305 config_get BaseDir globals BaseDir "/var/run/collectd"
306 printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"
307
308 config_get Include globals Include "/tmp/collectd.d"
309 printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
310 mkdir -p "$Include"
311
312 config_get PIDFile globals PIDFile "/var/run/collectd.pid"
313 printf "PIDFile \"%s\"\n" "$PIDFile" >> "$COLLECTD_CONF"
314
315 config_get PluginDir globals PluginDir "/usr/lib/collectd"
316 printf "PluginDir \"%s\"\n" "$PluginDir" >> "$COLLECTD_CONF"
317
318 config_get TypesDB globals TypesDB "/usr/share/collectd/types.db"
319 printf "TypesDB \"%s\"\n" "$TypesDB" >> "$COLLECTD_CONF"
320
321 config_get Interval globals Interval 30
322 printf "Interval \"%s\"\n" "$Interval" >> "$COLLECTD_CONF"
323
324 config_get ReadThreads globals ReadThreads 2
325 printf "ReadThreads \"%s\"\n" "$ReadThreads" >> "$COLLECTD_CONF"
326
327 config_get Hostname globals Hostname "$(uname -n)"
328 printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
329
330 printf "\n" >> "$COLLECTD_CONF"
331
332 # PLUGIN CONFIG
333 config_foreach process_plugins plugin
334 }
335
336 service_triggers()
337 {
338 procd_add_reload_trigger "collectd"
339 }
340
341 start_service() {
342 process_config
343
344 procd_open_instance
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
351 procd_close_instance
352 }
353
354 reload_service() {
355 restart "$@"
356 }