rsyslog: write config file to RAM
[feed/packages.git] / admin / rsyslog / files / rsyslog.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2014 OpenWrt.org
3
4 START=20
5
6 USE_PROCD=1
7
8 UCI_CONF="rsyslog"
9 CONFIG_FILE="/var/etc/rsyslog.conf"
10
11 modules=""
12 selectors=""
13 forwarders=""
14
15 handle_selector() {
16 local config="$1"
17 local src
18 local dst
19
20 config_get src ${config} source
21 config_get dst ${config} destination
22 if [ ${src} != "" ] && [ "$dst" != "" ]; then
23 selectors="${selectors}\n${src}\t${dst}\n"
24 fi
25 }
26
27 handle_forwarder() {
28 local config="$1"
29 local src
30 local target
31 local protocol
32 local port
33 local rfc
34 local opts
35
36 config_get src ${config} source
37 config_get target ${config} target
38 config_get protocol ${config} protocol "udp"
39 config_get port ${config} port "514"
40 config_get rfc ${config} rfc "3164"
41
42 if [ "$rfc" == "5424" ]; then
43 opts='Template="RSYSLOG_SyslogProtocol23Format" TCP_Framing="octet-counted"'
44 fi
45
46 if [ ${src} != "" ] && [ "${target}" != "" ]; then
47 action="action(type=\"omfwd\" target=\"$target\" port=\"$port\" protocol=\"$protocol\" $opts action.resumeRetryCount=\"100\" queue.type=\"linkedList\" queue.size=\"10000\")"
48 forwarders="${forwarders}\n${src}\t${action}\n"
49 fi
50 }
51
52
53 expand_config() {
54 local input_t=""
55 local input_u=""
56
57 config_load ${UCI_CONF}
58 config_list_foreach syslog modules handle_module
59 config_get_bool tcp_input syslog tcp_input
60 if [ ${tcp_input} -eq 1 ]; then
61 modules="${modules} imtcp"
62 config_get tcp_port syslog tcp_input_port
63 input_t="input(type=\"imtcp\" port=\"${tcp_port}\")"
64 fi
65
66 config_get_bool udp_input syslog udp_input
67 if [ ${udp_input} -eq 1 ]; then
68 modules="${modules} imudp"
69 config_get udp_port syslog udp_input_port
70 input_u="input(type=\"imudp\" port=\"${udp_port}\")"
71
72 fi
73 config_get template syslog default_template
74 config_foreach handle_selector selector
75 config_foreach handle_forwarder forwarder
76
77 > ${CONFIG_FILE}
78 for m in ${modules}; do
79 echo "module(load=\"${m}\")" >> ${CONFIG_FILE}
80 done
81 echo ${input_t} >> ${CONFIG_FILE}
82 echo ${input_u} >> ${CONFIG_FILE}
83 echo "\$ActionFileDefaultTemplate ${template}" >> ${CONFIG_FILE}
84 echo -e ${selectors} >> ${CONFIG_FILE}
85 echo -e ${forwarders} >> ${CONFIG_FILE}
86 }
87
88 handle_module() {
89 local module="$1"
90 modules="${modules} $module"
91 }
92
93 start_service() {
94 expand_config
95 procd_open_instance
96 procd_set_param command /usr/sbin/rsyslogd -f ${CONFIG_FILE} -n
97 procd_close_instance
98 }
99
100
101 service_triggers()
102 {
103 procd_add_reload_trigger ${UCI_CONF}
104 }