prometheus-node-exporter-lua: add dawn exporter
[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_network() {
82 local cfg="$1"
83
84 local TimeToLive Forward CacheFlush
85
86 printf "<Plugin network>\n" >> "$COLLECTD_CONF"
87 config_foreach process_network_sections network_listen "listen"
88 config_foreach process_network_sections network_server "server"
89
90 config_get TimeToLive "$cfg" TimeToLive
91 [ -z "$TimeToLive" ] || {
92 printf "\\tTimeToLive %s\n" "${TimeToLive}" >> "$COLLECTD_CONF"
93 }
94
95 config_get CacheFlush "$cfg" CacheFlush
96 [ -z "$CacheFlush" ] || {
97 printf "\\tCacheFlush %s\n" "${CacheFlush}" >> "$COLLECTD_CONF"
98 }
99
100 config_get_bool Forward "$cfg" Forward
101 if [ "$value" = "0" ]; then
102 printf "\\tForward false\n" >> "$COLLECTD_CONF"
103 else
104 printf "\\tForward true\n" >> "$COLLECTD_CONF"
105 fi
106
107 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
108 }
109
110 process_network_sections() {
111 local cfg="$1"
112 local section="$2"
113
114 local host port output
115
116 config_get host "$cfg" host
117 [ -z "$host" ] && {
118 $LOG notice "No host option in config $cfg defined"
119 return 0
120 }
121
122 if [ "$section" = "server" ]; then
123 output="Server \"$host\""
124 else
125 output="Listen \"$host\""
126 fi
127
128 config_get port "$cfg" port
129 if [ -z "$port" ]; then
130 printf "\\t%s\n" "${output}" >> "$COLLECTD_CONF"
131 else
132 printf "\\t%s \"%s\"\n" "${output}" "${port}" >> "$COLLECTD_CONF"
133 fi
134 }
135
136 process_iptables() {
137 local cfg="$1"
138
139 printf "<Plugin iptables>\n" >> "$COLLECTD_CONF"
140 config_foreach process_iptables_sections iptables_match
141 printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
142 }
143
144 process_iptables_sections() {
145 local cfg="$1"
146
147 local table chain
148
149 config_get table "$cfg" table
150 [ -z "$table" ] && {
151 $LOG notice "No table option in config $cfg defined"
152 return 0
153 }
154
155 config_get chain "$cfg" chain
156 [ -z "$chain" ] && {
157 $LOG notice "No chain option in config $cfg defined"
158 return 0
159 }
160
161 config_get index "$cfg" index
162 [ -z "$index" ] && {
163 $LOG notice "No index option in config $cfg defined"
164 return 0
165 }
166
167 config_get name "$cfg" name
168 if [ -z "$name" ]; then
169 printf "\\tChain %s %s %s\n" "${table}" "${chain}" "${index}" >> "$COLLECTD_CONF"
170 else
171 printf "\\tChain %s %s %s \"%s\"\n" "${table}" "${chain}" "${index}" "${name}">> "$COLLECTD_CONF"
172 fi
173 }
174
175 CONFIG_LIST=""
176 add_list_option() {
177 local value="$1"
178 local option="$2"
179 local indent="$3"
180
181 CONFIG_LIST="${CONFIG_LIST}${indent}${option} \"$value\"\n"
182 }
183
184 process_generic() {
185 local cfg="$1"
186 local indent="$2"
187 local json="$3"
188
189 local config=""
190
191 . /usr/share/libubox/jshn.sh
192 json_init
193 json_load_file "$json"
194
195 json_select string 1>/dev/null 2>&1
196 if [ $? -eq 0 ]; then
197 json_get_keys keys
198 for key in ${keys}; do
199 json_get_var option "$key"
200 config_get value "$cfg" "$option" ""
201 [ -z "$value" ] || {
202 config="${config}${indent}${option} \"${value}\"\n"
203 }
204 done
205 json_select ..
206 fi
207
208 json_select bool 1>/dev/null 2>&1
209 if [ $? -eq 0 ]; then
210 json_get_keys keys
211 for key in ${keys}; do
212 json_get_var option "$key"
213 config_get_bool value "$cfg" "$option"
214 if [ "$value" = "0" ]; then
215 config="${config}${indent}${option} false\n"
216 fi
217
218 if [ "$value" = "1" ]; then
219 config="${config}${indent}${option} true\n"
220 fi
221 done
222 json_select ..
223 fi
224
225 json_select list 1>/dev/null 2>&1
226 if [ $? -eq 0 ]; then
227 json_get_keys keys
228 for key in ${keys}; do
229 json_get_var option "$key"
230 CONFIG_LIST=""
231 config_list_foreach "$cfg" "$option" add_list_option "$option" "$indent"
232 config="${config}${CONFIG_LIST}"
233 done
234 json_select ..
235 fi
236
237 [ -z "$config" ] || {
238 printf "%s<Plugin %s>\n" "${CONFIG_STRING}" "$cfg" >> "$COLLECTD_CONF"
239 echo -e "${config}" >> "$COLLECTD_CONF"
240 printf "%s</Plugin>\n" "${CONFIG_STRING}" >> "$COLLECTD_CONF"
241 }
242
243 printf "\n" >> "$COLLECTD_CONF"
244 }
245
246 process_plugins() {
247 local cfg="$1"
248
249 local enable keys key option value
250
251 config_get enable "$cfg" enable 0
252 [ "$enable" = "1" ] || return 0
253
254 [ -f "/usr/lib/collectd/$cfg.so" ] || {
255 $LOG notice "Plugin collectd-mod-$cfg not installed"
256 return 0
257 }
258
259 [ -f "/usr/share/collectd/plugin/$cfg.json" ] || {
260 $LOG notice "Configuration definition file for $cfg not found"
261 return 0
262 }
263
264 printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
265 case "$cfg" in
266 exec)
267 CONFIG_STRING=""
268 process_exec
269 ;;
270 curl)
271 CONFIG_STRING=""
272 process_curl
273 ;;
274 network)
275 CONFIG_STRING=""
276 process_network "$cfg"
277 ;;
278 iptables)
279 CONFIG_STRING=""
280 process_iptables
281 ;;
282 *)
283 CONFIG_STRING=""
284 process_generic "$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
285 ;;
286 esac
287 }
288
289 process_config() {
290 local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
291 local Interval ReadThreads Hostname
292
293 rm -f "$COLLECTD_CONF"
294
295 [ -f /etc/config/collectd ] || {
296 $LOG notice "UCI config not found"
297 return 0
298 }
299 config_load collectd
300 config_get alt_config_file globals alt_config_file
301
302 # If "alt_config_file" specified, use that instead
303 [ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
304 rm -f "$COLLECTD_CONF"
305 ln -s "$alt_config_file" "$COLLECTD_CONF"
306 return 0
307 }
308
309 # GOBAL CONFIG
310 config_get BaseDir globals BaseDir "/var/run/collectd"
311 printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"
312
313 config_get Include globals Include "/tmp/collectd.d"
314 printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
315 mkdir -p "$Include"
316
317 config_get PIDFile globals PIDFile "/var/run/collectd.pid"
318 printf "PIDFile \"%s\"\n" "$PIDFile" >> "$COLLECTD_CONF"
319
320 config_get PluginDir globals PluginDir "/usr/lib/collectd"
321 printf "PluginDir \"%s\"\n" "$PluginDir" >> "$COLLECTD_CONF"
322
323 config_get TypesDB globals TypesDB "/usr/share/collectd/types.db"
324 printf "TypesDB \"%s\"\n" "$TypesDB" >> "$COLLECTD_CONF"
325
326 config_get Interval globals Interval 30
327 printf "Interval \"%s\"\n" "$Interval" >> "$COLLECTD_CONF"
328
329 config_get ReadThreads globals ReadThreads 2
330 printf "ReadThreads \"%s\"\n" "$ReadThreads" >> "$COLLECTD_CONF"
331
332 config_get Hostname globals Hostname "$(uname -n)"
333 printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"
334
335 printf "\n" >> "$COLLECTD_CONF"
336
337 # PLUGIN CONFIG
338 config_foreach process_plugins plugin
339 }
340
341 service_triggers()
342 {
343 procd_add_reload_trigger "collectd"
344 }
345
346 start_service() {
347 process_config
348
349 procd_open_instance
350 procd_set_param command /usr/sbin/collectd
351 procd_append_param command -C "$COLLECTD_CONF"
352 procd_append_param command -f # don't daemonize
353 procd_set_param nice "$NICEPRIO"
354 procd_set_param stderr 1
355 procd_set_param respawn
356 procd_close_instance
357 }
358
359 reload_service() {
360 restart "$@"
361 }