libgd: avoid recursive and redundant dependencies
[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_sections() {
157 local cfg="$1"
158 local section="$2"
159
160 local host port output
161
162 config_get host "$cfg" host
163 [ -z "$host" ] && {
164 $LOG notice "No host option in config $cfg defined"
165 return 0
166 }
167
168 if [ "$section" = "server" ]; then
169 output="Server \"$host\""
170 else
171 output="Listen \"$host\""
172 fi
173
174 config_get port "$cfg" port
175 if [ -z "$port" ]; then
176 printf "\\t%s\n" "${output}" >> "$COLLECTD_CONF"
177 else
178 printf "\\t%s \"%s\"\n" "${output}" "${port}" >> "$COLLECTD_CONF"
179 fi
180 }
181
182 process_iptables() {
183 local cfg="$1"
184
185 printf "<Plugin iptables>\n" >> "$COLLECTD_CONF"
186 config_foreach process_iptables_sections iptables_match
187 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
188 }
189
190 process_iptables_sections() {
191 local cfg="$1"
192
193 local table chain
194
195 config_get table "$cfg" table
196 [ -z "$table" ] && {
197 $LOG notice "No table option in config $cfg defined"
198 return 0
199 }
200
201 config_get chain "$cfg" chain
202 [ -z "$chain" ] && {
203 $LOG notice "No chain option in config $cfg defined"
204 return 0
205 }
206
207 config_get index "$cfg" index
208 [ -z "$index" ] && {
209 $LOG notice "No index option in config $cfg defined"
210 return 0
211 }
212
213 config_get name "$cfg" name
214 if [ -z "$name" ]; then
215 printf "\\tChain %s %s %s\n" "${table}" "${chain}" "${index}" >> "$COLLECTD_CONF"
216 else
217 printf "\\tChain %s %s %s \"%s\"\n" "${table}" "${chain}" "${index}" "${name}">> "$COLLECTD_CONF"
218 fi
219 }
220
221 CONFIG_LIST=""
222 add_list_option() {
223 local value="$1"
224 local option="$2"
225 local indent="$3"
226
227 CONFIG_LIST="${CONFIG_LIST}${indent}${option} \"$value\"\n"
228 }
229
230 process_generic() {
231 local cfg="$1"
232 local indent="$2"
233 local json="$3"
234
235 local config=""
236
237 . /usr/share/libubox/jshn.sh
238 json_init
239 json_load_file "$json"
240
241 json_select string 1>/dev/null 2>&1
242 if [ $? -eq 0 ]; then
243 json_get_keys keys
244 for key in ${keys}; do
245 json_get_var option "$key"
246 config_get value "$cfg" "$option" ""
247 [ -z "$value" ] || {
248 config="${config}${indent}${option} \"${value}\"\n"
249 }
250 done
251 json_select ..
252 fi
253
254 json_select bool 1>/dev/null 2>&1
255 if [ $? -eq 0 ]; then
256 json_get_keys keys
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"
262 fi
263
264 if [ "$value" = "1" ]; then
265 config="${config}${indent}${option} true\n"
266 fi
267 done
268 json_select ..
269 fi
270
271 json_select list 1>/dev/null 2>&1
272 if [ $? -eq 0 ]; then
273 json_get_keys keys
274 for key in ${keys}; do
275 json_get_var option "$key"
276 CONFIG_LIST=""
277 config_list_foreach "$cfg" "$option" add_list_option "$option" "$indent"
278 config="${config}${CONFIG_LIST}"
279 done
280 json_select ..
281 fi
282
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"
287 }
288 }
289
290 process_plugins() {
291 local cfg="$1"
292
293 local enable keys key option value
294
295 config_get enable "$cfg" enable 0
296 [ "$enable" = "1" ] || return 0
297
298 [ -f "/usr/lib/collectd/$cfg.so" ] || {
299 $LOG notice "Plugin collectd-mod-$cfg not installed"
300 return 0
301 }
302
303 [ -f "/usr/share/collectd/plugin/$cfg.json" ] || {
304 $LOG notice "Configuration definition file for $cfg not found"
305 return 0
306 }
307
308 printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
309 case "$cfg" in
310 exec)
311 CONFIG_STRING=""
312 process_exec
313 ;;
314 curl)
315 CONFIG_STRING=""
316 process_curl
317 ;;
318 network)
319 CONFIG_STRING=""
320 process_network "$cfg"
321 ;;
322 iptables)
323 CONFIG_STRING=""
324 process_iptables
325 ;;
326 write_http)
327 CONFIG_STRING=""
328 process_write_http
329 ;;
330 *)
331 CONFIG_STRING=""
332 process_generic "$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
333 ;;
334 esac
335 }
336
337 process_config() {
338 local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
339 local Interval ReadThreads WriteThreads Hostname
340 local WriteQueueLimitHigh WriteQueueLimitLow CollectInternalStats
341
342 rm -f "$COLLECTD_CONF"
343
344 [ -f /etc/config/collectd ] || {
345 $LOG notice "UCI config not found"
346 return 0
347 }
348 config_load collectd
349 config_get alt_config_file globals alt_config_file
350
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"
355 return 0
356 }
357
358 # GOBAL CONFIG
359 config_get BaseDir globals BaseDir "/var/run/collectd"
360 printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"
361
362 config_get PIDFile globals PIDFile "/var/run/collectd.pid"
363 printf "PIDFile \"%s\"\n" "$PIDFile" >> "$COLLECTD_CONF"
364
365 config_get PluginDir globals PluginDir "/usr/lib/collectd"
366 printf "PluginDir \"%s\"\n" "$PluginDir" >> "$COLLECTD_CONF"
367
368 config_get TypesDB globals TypesDB "/usr/share/collectd/types.db"
369 printf "TypesDB \"%s\"\n" "$TypesDB" >> "$COLLECTD_CONF"
370
371 config_get Interval globals Interval 30
372 printf "Interval %s\n" "$Interval" >> "$COLLECTD_CONF"
373
374 config_get ReadThreads globals ReadThreads 2
375 printf "ReadThreads %s\n" "$ReadThreads" >> "$COLLECTD_CONF"
376
377 config_get WriteThreads globals WriteThreads 2
378 printf "WriteThreads %s\n" "$WriteThreads" >> "$COLLECTD_CONF"
379
380 config_get WriteQueueLimitLow globals WriteQueueLimitLow 0
381 [ "$WriteQueueLimitLow" -ne 0 ] \
382 && printf "WriteQueueLimitLow %s\n" "$WriteQueueLimitLow" >> "$COLLECTD_CONF"
383
384 config_get WriteQueueLimitHigh globals WriteQueueLimitHigh 0
385 [ "$WriteQueueLimitHigh" -ne 0 ] \
386 && printf "WriteQueueLimitHigh %s\n" "$WriteQueueLimitHigh" >> "$COLLECTD_CONF"
387
388 config_get_bool CollectInternalStats globals CollectInternalStats 0
389 if [ "$CollectInternalStats" = "0" ]; then
390 printf "CollectInternalStats false\n" >> "$COLLECTD_CONF"
391 else
392 printf "CollectInternalStats true\n" >> "$COLLECTD_CONF"
393 fi
394
395 config_get Hostname globals Hostname "$(uname -n)"
396 printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
397
398 config_get Include globals Include "/tmp/collectd.d"
399 printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
400 mkdir -p "$Include"
401
402 printf "\n" >> "$COLLECTD_CONF"
403
404 # PLUGIN CONFIG
405 config_foreach process_plugins plugin
406 }
407
408 service_triggers()
409 {
410 procd_add_reload_trigger "collectd"
411 }
412
413 start_service() {
414 process_config
415
416 procd_open_instance
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
423 procd_close_instance
424 }
425
426 reload_service() {
427 restart "$@"
428 }