aboutsummaryrefslogtreecommitdiff
path: root/utils/collectd/files/collectd.init
blob: e585f7b058c25cf4bbc82336ffe1ca55beec889f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2016 OpenWrt.org

START=80
STOP=10

USE_PROCD=1
COLLECTD_CONF="/tmp/collectd.conf"
LOG="logger -t collectd[$$] -p"
NICEPRIO=5

CONFIG_STRING=""

[ -d /usr/libexec/collectd ] && {
	find /usr/libexec/collectd ! -perm 0500 -exec chmod 0500 '{}' '+'
	find /usr/libexec/collectd ! \( -user nobody -a -group nogroup \) -exec chown nobody:nogroup '{}' '+'
}

process_exec() {
	printf "<Plugin exec>\n" >> "$COLLECTD_CONF"
	config_foreach process_exec_sections exec_input "Exec"
	config_foreach process_exec_sections exec_notify "NotificationExec"
	printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
}

process_exec_sections() {
	local cfg="$1"
	local section="$2"

	local cmdline cmduser cmdgroup

	config_get cmdline "$cfg" cmdline
	[ -z "$cmdline" ] && {
		$LOG notice "No cmdline option in config $cfg defined"
		return 0
	}

	config_get cmduser "$cfg" cmduser
	[ -z "$cmduser" ] && {
		$LOG notice "No cmduser option in config $cfg defined"
		return 0
	}

	config_get cmdgroup "$cfg" cmdgroup
	if [ -z "$cmdgroup" ]; then
		printf "\\t%s \"%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdline}" >> "$COLLECTD_CONF"
	else
		printf "\\t%s \"%s:%s\" \"%s\"\n" "${section}" "${cmduser}" "${cmdgroup}" "${cmdline}" >> "$COLLECTD_CONF"
	fi
}

process_curl() {
	printf "<Plugin curl>\n" >> "$COLLECTD_CONF"
	config_foreach process_curl_page curl_page
	printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
}

process_curl_page() {
	local cfg="$1"

	local name url

	config_get name "$cfg" name
	[ -z "$name" ] && {
		$LOG notice "No name option in config $cfg defined"
		return 0
	}

	config_get url "$cfg" url
	[ -z "$url" ] && {
		$LOG notice "No URL option in config $cfg defined"
		return 0
	}

	printf "\\t<Page \"%s\">\n" "${name}" >> "$COLLECTD_CONF"
	printf "\\t\\tURL \"%s\"\n" "${url}" >> "$COLLECTD_CONF"
	printf "\\t\\tMeasureResponseTime true\n" >> "$COLLECTD_CONF"
	printf "\\t</Page>\n" >> "$COLLECTD_CONF"
}

process_network() {
	local cfg="$1"

	local TimeToLive Forward CacheFlush

	printf "<Plugin network>\n" >> "$COLLECTD_CONF"
	config_foreach process_network_sections network_listen "listen"
	config_foreach process_network_sections network_server "server"

	config_get TimeToLive "$cfg" TimeToLive
	[ -z "$TimeToLive" ] || {
		printf "\\tTimeToLive %s\n" "${TimeToLive}" >> "$COLLECTD_CONF"
	}

	config_get CacheFlush "$cfg" CacheFlush
	[ -z "$CacheFlush" ] || {
		printf "\\tCacheFlush %s\n" "${CacheFlush}" >> "$COLLECTD_CONF"
	}

	config_get_bool Forward "$cfg" Forward
	if [ "$Forward" = "0" ]; then
		printf "\\tForward false\n" >> "$COLLECTD_CONF"
	else
		printf "\\tForward true\n" >> "$COLLECTD_CONF"
	fi

	printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
}

process_network_sections() {
	local cfg="$1"
	local section="$2"

	local host port output

	config_get host "$cfg" host
	[ -z "$host" ] && {
		$LOG notice "No host option in config $cfg defined"
		return 0
	}

	if [ "$section" = "server" ]; then
		output="Server \"$host\""
	else
		output="Listen \"$host\""
	fi

	config_get port "$cfg" port
	if [ -z "$port" ]; then
		printf "\\t%s\n" "${output}" >> "$COLLECTD_CONF"
	else
		printf "\\t%s \"%s\"\n" "${output}" "${port}" >> "$COLLECTD_CONF"
	fi
}

process_iptables() {
	local cfg="$1"

	printf "<Plugin iptables>\n" >> "$COLLECTD_CONF"
	config_foreach process_iptables_sections iptables_match
	printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
}

process_iptables_sections() {
	local cfg="$1"

	local table chain

	config_get table "$cfg" table
	[ -z "$table" ] && {
		$LOG notice "No table option in config $cfg defined"
		return 0
	}

	config_get chain "$cfg" chain
	[ -z "$chain" ] && {
		$LOG notice "No chain option in config $cfg defined"
		return 0
	}

	config_get index "$cfg" index
	[ -z "$index" ] && {
		$LOG notice "No index option in config $cfg defined"
		return 0
	}

	config_get name "$cfg" name
	if [ -z "$name" ]; then
		printf "\\tChain %s %s %s\n" "${table}" "${chain}" "${index}" >> "$COLLECTD_CONF"
	else
		printf "\\tChain %s %s %s \"%s\"\n" "${table}" "${chain}" "${index}" "${name}">> "$COLLECTD_CONF"
	fi
}

CONFIG_LIST=""
add_list_option() {
	local value="$1"
	local option="$2"
	local indent="$3"

	CONFIG_LIST="${CONFIG_LIST}${indent}${option} \"$value\"\n"
}

process_generic() {
	local cfg="$1"
	local indent="$2"
	local json="$3"

	local config=""

	. /usr/share/libubox/jshn.sh
	json_init
	json_load_file "$json"

	json_select string 1>/dev/null 2>&1
	if [ $? -eq 0 ]; then
		json_get_keys keys
		for key in ${keys}; do
			json_get_var option "$key"
			config_get value "$cfg" "$option" ""
			[ -z "$value" ] || {
				config="${config}${indent}${option} \"${value}\"\n"
			}
		done
		json_select ..
	fi

	json_select bool 1>/dev/null 2>&1
	if [ $? -eq 0 ]; then
		json_get_keys keys
		for key in ${keys}; do
			json_get_var option "$key"
			config_get_bool value "$cfg" "$option"
			if [ "$value" = "0" ]; then
				config="${config}${indent}${option} false\n"
			fi

			if [ "$value" = "1" ]; then
				config="${config}${indent}${option} true\n"
			fi
		done
		json_select ..
	fi

	json_select list 1>/dev/null 2>&1
	if [ $? -eq 0 ]; then
		json_get_keys keys
		for key in ${keys}; do
			json_get_var option "$key"
			CONFIG_LIST=""
			config_list_foreach "$cfg" "$option" add_list_option "$option" "$indent"
			config="${config}${CONFIG_LIST}"
		done
		json_select ..
	fi

	[ -z "$config" ] || {
		printf "%s<Plugin %s>\n" "${CONFIG_STRING}" "$cfg" >> "$COLLECTD_CONF"
		echo -e "${config}" >> "$COLLECTD_CONF"
		printf "%s</Plugin>\n" "${CONFIG_STRING}" >> "$COLLECTD_CONF"
	}

	printf "\n" >> "$COLLECTD_CONF"
}

process_plugins() {
	local cfg="$1"

	local enable keys key option value

	config_get enable "$cfg" enable 0
	[ "$enable" = "1" ] || return 0

	[ -f "/usr/lib/collectd/$cfg.so" ] || {
		$LOG notice "Plugin collectd-mod-$cfg not installed"
		return 0
	}

	[ -f "/usr/share/collectd/plugin/$cfg.json" ] || {
		$LOG notice "Configuration definition file for $cfg not found"
		return 0
	}

	printf "LoadPlugin %s\n" "$cfg" >> "$COLLECTD_CONF"
	case "$cfg" in
		exec)
			CONFIG_STRING=""
			process_exec
			;;
		curl)
			CONFIG_STRING=""
			process_curl
			;;
		network)
			CONFIG_STRING=""
			process_network "$cfg"
			;;
		iptables)
			CONFIG_STRING=""
			process_iptables
			;;
		*)
			CONFIG_STRING=""
			process_generic "$cfg" "\\t" "/usr/share/collectd/plugin/$cfg.json"
		;;
	esac
}

process_config() {
	local alt_config_file BaseDir Include PIDFile PluginDir TypesDB
	local Interval ReadThreads Hostname

	rm -f "$COLLECTD_CONF"

	[ -f /etc/config/collectd ] || {
		$LOG notice "UCI config not found"
		return 0
	}
	config_load collectd
	config_get alt_config_file globals alt_config_file

	# If "alt_config_file" specified, use that instead
	[ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
		rm -f "$COLLECTD_CONF"
		ln -s "$alt_config_file" "$COLLECTD_CONF"
		return 0
	}

	# GOBAL CONFIG
	config_get BaseDir globals BaseDir "/var/run/collectd"
	printf "BaseDir \"%s\"\n" "$BaseDir" >> "$COLLECTD_CONF"

	config_get PIDFile globals PIDFile "/var/run/collectd.pid"
	printf "PIDFile \"%s\"\n" "$PIDFile" >> "$COLLECTD_CONF"

	config_get PluginDir globals PluginDir "/usr/lib/collectd"
	printf "PluginDir \"%s\"\n" "$PluginDir" >> "$COLLECTD_CONF"

	config_get TypesDB globals TypesDB "/usr/share/collectd/types.db"
	printf "TypesDB \"%s\"\n" "$TypesDB" >> "$COLLECTD_CONF"

	config_get Interval globals Interval 30
	printf "Interval %s\n" "$Interval" >> "$COLLECTD_CONF"

	config_get ReadThreads globals ReadThreads 2
	printf "ReadThreads \"%s\"\n" "$ReadThreads" >> "$COLLECTD_CONF"

	config_get Hostname globals Hostname "$(uname -n)"
	printf "Hostname \"%s\"\n" "$Hostname" >> "$COLLECTD_CONF"

	config_get Include globals Include "/tmp/collectd.d"
	printf "Include \"%s\"\n" "$Include" >> "$COLLECTD_CONF"
	mkdir -p "$Include"

	printf "\n" >> "$COLLECTD_CONF"

	# PLUGIN CONFIG
	config_foreach process_plugins plugin
}

service_triggers()
{
	procd_add_reload_trigger "collectd"
}

start_service() {
	process_config

	procd_open_instance
	procd_set_param command /usr/sbin/collectd
	procd_append_param command -C "$COLLECTD_CONF"
	procd_append_param command -f # don't daemonize
	procd_set_param nice "$NICEPRIO"
	procd_set_param stderr 1
	procd_set_param respawn
	procd_close_instance
}

reload_service() {
	restart "$@"
}