Merge pull request #19372 from cotequeiroz/libgd
[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 [ -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 '{}' '+'
17 }
18
19 process_exec() {
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"
24 }
25
26 process_exec_sections() {
27 local cfg="$1"
28 local section="$2"
29
30 local cmdline cmduser cmdgroup
31
32 config_get cmdline "$cfg" cmdline
33 [ -z "$cmdline" ] && {
34 $LOG notice "No cmdline option in config $cfg defined"
35 return 0
36 }
37
38 config_get cmduser "$cfg" cmduser
39 [ -z "$cmduser" ] && {
40 $LOG notice "No cmduser option in config $cfg defined"
41 return 0
42 }
43
44 config_get cmdgroup "$cfg" cmdgroup
45 if [ -z "$cmdgroup" ]; then
46 printf "\\t%s \"%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdline}" >> "$COLLECTD_CONF"
47 else
48 printf "\\t%s \"%s:%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdgroup}" "${cmdline}" >> "$COLLECTD_CONF"
49 fi
50 }
51
52 process_curl() {
53 printf "<Plugin curl>\n" >> "$COLLECTD_CONF"
54 config_foreach process_curl_page curl_page
55 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
56 }
57
58 process_curl_page() {
59 local cfg="$1"
60
61 local name url
62
63 config_get name "$cfg" name
64 [ -z "$name" ] && {
65 $LOG notice "No name option in config $cfg defined"
66 return 0
67 }
68
69 config_get url "$cfg" url
70 [ -z "$url" ] && {
71 $LOG notice "No URL option in config $cfg defined"
72 return 0
73 }
74
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"
79 }
80
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"
85 }
86
87 process_write_http_node() {
88 local cfg="$1"
89
90 local name URL Format User Password Timeout BufferSize
91
92 config_get name "$cfg" name
93 [ -z "$name" ] && {
94 $LOG notice "No name option in config $cfg defined"
95 return 0
96 }
97 config_get URL "$cfg" URL
98 [ -z "$URL" ] && {
99 $LOG notice "No URL option in config $cfg defined"
100 return 0
101 }
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"
111 }
112 [ -z "$User" ] || {
113 printf "\\t\\tUser \"%s\"\n" "${User}" >> "$COLLECTD_CONF"
114 }
115 [ -z "$Password" ] || {
116 printf "\\t\\tPassword \"%s\"\n" "${Password}" >> "$COLLECTD_CONF"
117 }
118 [ -z "$Timeout" ] || {
119 printf "\\t\\tTimeout \%s\n" "${Timeout}" >> "$COLLECTD_CONF"
120 }
121 [ -z "$BufferSize" ] || {
122 printf "\\t\\tBufferSize \%s\n" "${BufferSize}" >> "$COLLECTD_CONF"
123 }
124 printf "\\t</Node>\n" >> "$COLLECTD_CONF"
125 }
126
127 process_network() {
128 local cfg="$1"
129
130 local TimeToLive Forward CacheFlush
131
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"
135
136 config_get TimeToLive "$cfg" TimeToLive
137 [ -z "$TimeToLive" ] || {
138 printf "\\tTimeToLive %s\n" "${TimeToLive}" >> "$COLLECTD_CONF"
139 }
140
141 config_get CacheFlush "$cfg" CacheFlush
142 [ -z "$CacheFlush" ] || {
143 printf "\\tCacheFlush %s\n" "${CacheFlush}" >> "$COLLECTD_CONF"
144 }
145
146 config_get_bool Forward "$cfg" Forward
147 if [ "$Forward" = "0" ]; then
148 printf "\\tForward false\n" >> "$COLLECTD_CONF"
149 else
150 printf "\\tForward true\n" >> "$COLLECTD_CONF"
151 fi
152
153 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
154 }
155
156 process_network_server() {
157 local cfg="$1"
158 local SecurityLevel="$2"
159
160 local Username Password ResolveInterval
161
162 config_get Username "$cfg" Username
163 [ -z "$Username" ] && {
164 $LOG notice "SecurityLevel set to '$SecurityLevel' but no option Username found in config '$cfg'"
165 return 1
166 }
167 printf "\\t\\tUsername \"%s\"\n" "${Username}" >> "$COLLECTD_CONF"
168
169 config_get Password "$cfg" Password
170 [ -z "$Password" ] && {
171 $LOG notice "SecurityLevel set to '$SecurityLevel' but no option Password found in config '$cfg'"
172 return 2
173 }
174 printf "\\t\\tPassword \"%s\"\n" "${Password}" >> "$COLLECTD_CONF"
175
176 config_get ResolveInterval "$cfg" ResolveInterval
177 [ -z "$ResolveInterval" ] || {
178 printf "\\t\\tResolveInterval \"%s\"\n" "${ResolveInterval}" >> "$COLLECTD_CONF"
179 }
180 }
181
182 process_network_listen() {
183 local cfg="$1"
184
185 local auth_file="/tmp/collectd-auth-${cfg}.conf"
186 local auth_set
187
188 rm -rf "${auth_file}"
189 add_auth() {
190 echo "$1" >> "${auth_file}"
191 auth_set=1
192 }
193 config_list_foreach "$cfg" auth add_auth
194
195 [ -z "$auth_set" ] && {
196 $LOG notice "SecurityLevel set to '$SecurityLevel' but no list option auth found in config '$cfg'"
197 return 1
198 }
199
200 printf "\\t\\tAuthFile \"%s\"\n" "${auth_file}" >> "$COLLECTD_CONF"
201 }
202
203 process_network_sections() {
204 local cfg="$1"
205 local section="$2"
206
207 local host port output rvalue SecurityLevel Interface
208
209 config_get host "$cfg" host
210 [ -z "$host" ] && {
211 $LOG notice "No host option in config $cfg defined"
212 return 0
213 }
214
215 if [ "$section" = "server" ]; then
216 output="Server \"$host\""
217 else
218 output="Listen \"$host\""
219 fi
220
221 config_get port "$cfg" port
222 if [ -z "$port" ]; then
223 printf "\\t<%s>\n" "${output}" >> "$COLLECTD_CONF"
224 else
225 printf "\\t<%s \"%s\">\n" "${output}" "${port}" >> "$COLLECTD_CONF"
226 fi
227
228 config_get SecurityLevel "$cfg" SecurityLevel 'None'
229 [ -z "$SecurityLevel" ] || {
230 printf "\\t\\tSecurityLevel \"%s\"\n" "${SecurityLevel}" >> "$COLLECTD_CONF"
231 }
232
233 if [ "$SecurityLevel" != "None" ]; then
234 case "$section" in
235 server)
236 process_network_server "$cfg" "$SecurityLevel"
237 rvalue="$?"
238 [ "$rvalue" != 0 ] && return 0
239 ;;
240 listen)
241 process_network_listen "$cfg" "$SecurityLevel"
242 rvalue="$?"
243 [ "$rvalue" != 0 ] && return 0
244 ;;
245 esac
246 else
247 $LOG notice "SecurityLevel set to 'None' for '$cfg'"
248 fi
249
250 config_get Interface "$cfg" Interface
251 [ -z "$Interface" ] || {
252 printf "\\t\\tInterface \"%s\"\n" "${Interface}" >> "$COLLECTD_CONF"
253 }
254
255 if [ "$section" = "server" ]; then
256 printf "\\t</Server>\n" >> "$COLLECTD_CONF"
257 else
258 printf "\\t</Listen>\n" >> "$COLLECTD_CONF"
259 fi
260 }
261
262 process_iptables() {
263 local cfg="$1"
264
265 printf "<Plugin iptables>\n" >> "$COLLECTD_CONF"
266 config_foreach process_iptables_sections iptables_match
267 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
268 }
269
270 process_iptables_sections() {
271 local cfg="$1"
272
273 local table chain
274
275 config_get table "$cfg" table
276 [ -z "$table" ] && {
277 $LOG notice "No table option in config $cfg defined"
278 return 0
279 }
280
281 config_get chain "$cfg" chain
282 [ -z "$chain" ] && {
283 $LOG notice "No chain option in config $cfg defined"
284 return 0
285 }
286
287 config_get index "$cfg" index
288 [ -z "$index" ] && {
289 $LOG notice "No index option in config $cfg defined"
290 return 0
291 }
292
293 config_get name "$cfg" name
294 if [ -z "$name" ]; then
295 printf "\\tChain %s %s %s\n" "${table}" "${chain}" "${index}" >> "$COLLECTD_CONF"
296 else
297 printf "\\tChain %s %s %s \"%s\"\n" "${table}" "${chain}" "${index}" "${name}">> "$COLLECTD_CONF"
298 fi
299 }
300
301 CONFIG_LIST=""
302 add_list_option() {
303 local value="$1"
304 local option="$2"
305 local indent="$3"
306
307 CONFIG_LIST="${CONFIG_LIST}${indent}${option} \"$value\"\n"
308 }
309
310 process_generic() {
311 local cfg="$1"
312 local indent="$2"
313 local json="$3"
314
315 local config=""
316
317 . /usr/share/libubox/jshn.sh
318 json_init
319 json_load_file "$json"
320
321 json_select string 1>/dev/null 2>&1
322 if [ $? -eq 0 ]; then
323 json_get_keys keys
324 for key in ${keys}; do
325 json_get_var option "$key"
326 config_get value "$cfg" "$option" ""
327 [ -z "$value" ] || {
328 config="${config}${indent}${option} \"${value}\"\n"
329 }
330 done
331 json_select ..
332 fi
333
334 json_select bool 1>/dev/null 2>&1
335 if [ $? -eq 0 ]; then
336 json_get_keys keys
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"
342 fi
343
344 if [ "$value" = "1" ]; then
345 config="${config}${indent}${option} true\n"
346 fi
347 done
348 json_select ..
349 fi
350
351 json_select list 1>/dev/null 2>&1
352 if [ $? -eq 0 ]; then
353 json_get_keys keys
354 for key in ${keys}; do
355 json_get_var option "$key"
356 CONFIG_LIST=""
357 config_list_foreach "$cfg" "$option" add_list_option "$option" "$indent"
358 config="${config}${CONFIG_LIST}"
359 done
360 json_select ..
361 fi
362
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"
367 }
368 }
369
370 process_plugins() {
371 local cfg="$1"
372
373 local enable keys key option value
374
375 config_get enable "$cfg" enable 0
376 [ "$enable" = "1" ] || return 0
377
378 [ -f "/usr/lib/collectd/$cfg.so" ] || {
379 $LOG notice "Plugin collectd-mod-$cfg not installed"
380 return 0
381 }
382
383 [ -f "/usr/share/collectd/plugin/$cfg.json" ] || {
384 $LOG notice "Configuration definition file for $cfg not found"
385 return 0
386 }
387
388 printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
389 case "$cfg" in
390 exec)
391 CONFIG_STRING=""
392 process_exec
393 ;;
394 curl)
395 CONFIG_STRING=""
396 process_curl
397 ;;
398 network)
399 CONFIG_STRING=""
400 process_network "$cfg"
401 ;;
402 iptables)
403 CONFIG_STRING=""
404 process_iptables
405 ;;
406 write_http)
407 CONFIG_STRING=""
408 process_write_http
409 ;;
410 *)
411 CONFIG_STRING=""
412 process_generic "$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
413 ;;
414 esac
415 }
416
417 process_config() {
418 local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
419 local Interval ReadThreads WriteThreads Hostname
420 local WriteQueueLimitHigh WriteQueueLimitLow CollectInternalStats
421
422 rm -f "$COLLECTD_CONF"
423
424 [ -f /etc/config/collectd ] || {
425 $LOG notice "UCI config not found"
426 return 0
427 }
428 config_load collectd
429 config_get alt_config_file globals alt_config_file
430
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"
435 return 0
436 }
437
438 # GOBAL CONFIG
439 config_get BaseDir globals BaseDir "/var/run/collectd"
440 printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"
441
442 config_get PIDFile globals PIDFile "/var/run/collectd.pid"
443 printf "PIDFile \"%s\"\n" "$PIDFile" >> "$COLLECTD_CONF"
444
445 config_get PluginDir globals PluginDir "/usr/lib/collectd"
446 printf "PluginDir \"%s\"\n" "$PluginDir" >> "$COLLECTD_CONF"
447
448 config_get TypesDB globals TypesDB "/usr/share/collectd/types.db"
449 printf "TypesDB \"%s\"\n" "$TypesDB" >> "$COLLECTD_CONF"
450
451 config_get Interval globals Interval 30
452 printf "Interval %s\n" "$Interval" >> "$COLLECTD_CONF"
453
454 config_get ReadThreads globals ReadThreads 2
455 printf "ReadThreads %s\n" "$ReadThreads" >> "$COLLECTD_CONF"
456
457 config_get WriteThreads globals WriteThreads 2
458 printf "WriteThreads %s\n" "$WriteThreads" >> "$COLLECTD_CONF"
459
460 config_get WriteQueueLimitLow globals WriteQueueLimitLow 0
461 [ "$WriteQueueLimitLow" -ne 0 ] \
462 && printf "WriteQueueLimitLow %s\n" "$WriteQueueLimitLow" >> "$COLLECTD_CONF"
463
464 config_get WriteQueueLimitHigh globals WriteQueueLimitHigh 0
465 [ "$WriteQueueLimitHigh" -ne 0 ] \
466 && printf "WriteQueueLimitHigh %s\n" "$WriteQueueLimitHigh" >> "$COLLECTD_CONF"
467
468 config_get_bool CollectInternalStats globals CollectInternalStats 0
469 if [ "$CollectInternalStats" = "0" ]; then
470 printf "CollectInternalStats false\n" >> "$COLLECTD_CONF"
471 else
472 printf "CollectInternalStats true\n" >> "$COLLECTD_CONF"
473 fi
474
475 config_get Hostname globals Hostname "$(uname -n)"
476 printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
477
478 config_get Include globals Include "/tmp/collectd.d"
479 printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
480 mkdir -p "$Include"
481
482 printf "\n" >> "$COLLECTD_CONF"
483
484 # PLUGIN CONFIG
485 config_foreach process_plugins plugin
486 }
487
488 service_triggers()
489 {
490 procd_add_reload_trigger "collectd"
491 }
492
493 start_service() {
494 process_config
495
496 procd_open_instance
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
503 procd_close_instance
504 }
505
506 reload_service() {
507 restart "$@"
508 }