blob: 2146a668659d08630604bf022398bfecee4c4463 (
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
#!/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_write_http() {
printf "<Plugin write_http>\n" >> "$COLLECTD_CONF"
config_foreach process_write_http_node write_http_node
printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
}
process_write_http_node() {
local cfg="$1"
local name URL Format User Password Timeout BufferSize
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
}
config_get Format "$cfg" Format
config_get User "$cfg" User
config_get Password "$cfg" Password
config_get Timeout "$cfg" Timeout
config_get BufferSize "$cfg" BufferSize
printf "\\t<Node \"%s\">\n" "${name}" >> "$COLLECTD_CONF"
printf "\\t\\tURL \"%s\"\n" "${URL}" >> "$COLLECTD_CONF"
[ -z "$Format" ] || {
printf "\\t\\tFormat \"%s\"\n" "${Format}" >> "$COLLECTD_CONF"
}
[ -z "$User" ] || {
printf "\\t\\tUser \"%s\"\n" "${User}" >> "$COLLECTD_CONF"
}
[ -z "$Password" ] || {
printf "\\t\\tPassword \"%s\"\n" "${Password}" >> "$COLLECTD_CONF"
}
[ -z "$Timeout" ] || {
printf "\\t\\tTimeout \%s\n" "${Timeout}" >> "$COLLECTD_CONF"
}
[ -z "$BufferSize" ] || {
printf "\\t\\tBufferSize \%s\n" "${BufferSize}" >> "$COLLECTD_CONF"
}
printf "\\t</Node>\n" >> "$COLLECTD_CONF"
}
process_mqtt() {
printf "<Plugin mqtt>\n" >> "$COLLECTD_CONF"
config_foreach process_mqtt_block mqtt_block
printf "</Plugin>\n\n" >> "$COLLECTD_CONF"
}
process_mqtt_block() {
local cfg="$1"
local blocktype name Host Port User Password ClientId QoS Prefix Retain StoreRates CleanSession Topic
config_get blocktype "$cfg" blocktype
[ -z "$blocktype" ] && {
$LOG notice "No blocktype option in config $cfg defined"
return 0
}
config_get name "$cfg" name
[ -z "$name" ] && {
$LOG notice "No name option in config $cfg defined"
return 0
}
config_get Host "$cfg" Host
[ -z "$Host" ] && {
$LOG notice "No Host option in config $cfg defined"
return 0
}
config_get Port "$cfg" Port
config_get User "$cfg" User
config_get Password "$cfg" Password
config_get QoS "$cfg" QoS
config_get ClientId "$cfg" ClientId
config_get Prefix "$cfg" Prefix
[ -n "$Prefix" ] && [ "$blocktype" != "Publish" ] && {
$LOG notice "Prefix option in config $cfg defined under non Publish block"
return 0
}
config_get Retain "$cfg" Retain
[ -n "$Retain" ] && [ "$blocktype" != "Publish" ] && {
$LOG notice "Retain option in config $cfg defined under non Publish block"
return 0
}
config_get StoreRates "$cfg" StoreRates
[ -n "$StoreRates" ] && [ "$blocktype" != "Publish" ] && {
$LOG notice "StoreRates option in config $cfg defined under non Publish block"
return 0
}
config_get CleanSession "$cfg" CleanSession
[ -n "$CleanSession" ] && [ "$blocktype" != "Subscribe" ] && {
$LOG notice "CleanSession option in config $cfg defined under non Subscribe block"
return 0
}
config_get Topic "$cfg" Topic
[ -n "$Topic" ] && [ "$blocktype" != "Subscribe" ] && {
$LOG notice "Topic option in config $cfg defined under non Subscribe block"
return 0
}
printf "\\t<%s \"%s\">\n" "${blocktype}" "${name}" >> "$COLLECTD_CONF"
[ -z "$Host" ] || {
printf "\\t\\tHost \"%s\"\n" "${Host}" >> "$COLLECTD_CONF"
}
[ -z "$Port" ] || {
printf "\\t\\tPort \"%s\"\n" "${Port}" >> "$COLLECTD_CONF"
}
[ -z "$User" ] || {
printf "\\t\\tUser \"%s\"\n" "${User}" >> "$COLLECTD_CONF"
}
[ -z "$Password" ] || {
printf "\\t\\tPassword \"%s\"\n" "${Password}" >> "$COLLECTD_CONF"
}
[ -z "$QoS" ] || {
printf "\\t\\tQoS %s\n" "${QoS}" >> "$COLLECTD_CONF"
}
[ -z "$ClientId" ] || {
printf "\\t\\tClientId \"%s\"\n" "${ClientId}" >> "$COLLECTD_CONF"
}
[ -z "$Prefix" ] || {
printf "\\t\\tPrefix \"%s\"\n" "${Prefix}" >> "$COLLECTD_CONF"
}
[ -z "$Retain" ] || {
printf "\\t\\tRetain \"%s\"\n" "${Retain}" >> "$COLLECTD_CONF"
}
[ -z "$StoreRates" ] || {
printf "\\t\\tStoreRates \"%s\"\n" "${StoreRates}" >> "$COLLECTD_CONF"
}
[ -z "$CleanSession" ] || {
printf "\\t\\tCleanSession \"%s\"\n" "${CleanSession}" >> "$COLLECTD_CONF"
}
[ -z "$Topic" ] || {
printf "\\t\\tTopic \"%s\"\n" "${Topic}" >> "$COLLECTD_CONF"
}
printf "\\t</%s>\n" "${blocktype}" >> "$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_server() {
local cfg="$1"
local SecurityLevel="$2"
local Username Password ResolveInterval
config_get Username "$cfg" Username
[ -z "$Username" ] && {
$LOG notice "SecurityLevel set to '$SecurityLevel' but no option Username found in config '$cfg'"
return 1
}
printf "\\t\\tUsername \"%s\"\n" "${Username}" >> "$COLLECTD_CONF"
config_get Password "$cfg" Password
[ -z "$Password" ] && {
$LOG notice "SecurityLevel set to '$SecurityLevel' but no option Password found in config '$cfg'"
return 2
}
printf "\\t\\tPassword \"%s\"\n" "${Password}" >> "$COLLECTD_CONF"
config_get ResolveInterval "$cfg" ResolveInterval
[ -z "$ResolveInterval" ] || {
printf "\\t\\tResolveInterval \"%s\"\n" "${ResolveInterval}" >> "$COLLECTD_CONF"
}
}
process_network_listen() {
local cfg="$1"
local auth_file="/tmp/collectd-auth-${cfg}.conf"
local auth_set
rm -rf "${auth_file}"
add_auth() {
echo "$1" >> "${auth_file}"
auth_set=1
}
config_list_foreach "$cfg" auth add_auth
[ -z "$auth_set" ] && {
$LOG notice "SecurityLevel set to '$SecurityLevel' but no list option auth found in config '$cfg'"
return 1
}
printf "\\t\\tAuthFile \"%s\"\n" "${auth_file}" >> "$COLLECTD_CONF"
}
process_network_sections() {
local cfg="$1"
local section="$2"
local host port output rvalue SecurityLevel Interface
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
config_get SecurityLevel "$cfg" SecurityLevel 'None'
[ -z "$SecurityLevel" ] || {
printf "\\t\\tSecurityLevel \"%s\"\n" "${SecurityLevel}" >> "$COLLECTD_CONF"
}
if [ "$SecurityLevel" != "None" ]; then
case "$section" in
server)
process_network_server "$cfg" "$SecurityLevel"
rvalue="$?"
[ "$rvalue" != 0 ] && return 0
;;
listen)
process_network_listen "$cfg" "$SecurityLevel"
rvalue="$?"
[ "$rvalue" != 0 ] && return 0
;;
esac
else
$LOG notice "SecurityLevel set to 'None' for '$cfg'"
fi
config_get Interface "$cfg" Interface
[ -z "$Interface" ] || {
printf "\\t\\tInterface \"%s\"\n" "${Interface}" >> "$COLLECTD_CONF"
}
if [ "$section" = "server" ]; then
printf "\\t</Server>\n" >> "$COLLECTD_CONF"
else
printf "\\t</Listen>\n" >> "$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 -n -e "${config}" >> "$COLLECTD_CONF"
printf "%s</Plugin>\n" "${CONFIG_STRING}" >> "$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
;;
write_http)
CONFIG_STRING=""
process_write_http
;;
mqtt)
CONFIG_STRING=""
process_mqtt
;;
*)
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 WriteThreads Hostname
local WriteQueueLimitHigh WriteQueueLimitLow CollectInternalStats
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 WriteThreads globals WriteThreads 2
printf "WriteThreads %s\n" "$WriteThreads" >> "$COLLECTD_CONF"
config_get WriteQueueLimitLow globals WriteQueueLimitLow 0
[ "$WriteQueueLimitLow" -ne 0 ] \
&& printf "WriteQueueLimitLow %s\n" "$WriteQueueLimitLow" >> "$COLLECTD_CONF"
config_get WriteQueueLimitHigh globals WriteQueueLimitHigh 0
[ "$WriteQueueLimitHigh" -ne 0 ] \
&& printf "WriteQueueLimitHigh %s\n" "$WriteQueueLimitHigh" >> "$COLLECTD_CONF"
config_get_bool CollectInternalStats globals CollectInternalStats 0
if [ "$CollectInternalStats" = "0" ]; then
printf "CollectInternalStats false\n" >> "$COLLECTD_CONF"
else
printf "CollectInternalStats true\n" >> "$COLLECTD_CONF"
fi
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 "$@"
}
|