File: constant_strings.rst

package info (click to toggle)
rsyslog 8.2512.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,464 kB
  • sloc: ansic: 123,809; sh: 42,109; makefile: 5,962; javascript: 1,842; python: 1,222; lex: 607; yacc: 193; perl: 162; sql: 103; tcl: 9; ruby: 2
file content (255 lines) | stat: -rw-r--r-- 7,060 bytes parent folder | download
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
Rsyslog Parameter String Constants
==================================

String constants provide values that are evaluated at startup and remain unchanged
for the lifetime of the rsyslog process.

Uses
----

String constants are used in comparisons, configuration parameter values, and
function arguments, among other places.

Escaping
--------

In string constants, special characters are escaped by prepending a backslash,
similar to C or PHP.

If in doubt how to escape properly, use the
`RainerScript String Escape Online Tool
<http://www.rsyslog.com/rainerscript-constant-string-escaper/>`_.

Types
-----

Rsyslog provides different types of string constants, inspired by common shell
semantics.

Single quotes
.............

Values are used unaltered, except that escape sequences are processed.

Double quotes
.............

Equivalent to single quotes; a literal dollar sign must be escaped (``\$``).
If ``$`` is not escaped, a syntax error is raised and rsyslog usually refuses
to start.

Backticks
.........

.. versionadded:: 8.33.0
   Initial support for backticks (limited subset).

.. versionchanged:: 8.37.0
   Multiple environment variable expansions and constant text between them.

.. versionchanged:: 8.2508.0
   Support for brace-style variables (``${VAR}``), correct termination of
   unbraced ``$VAR`` at the first non-``[A-Za-z0-9_]`` character, and support
   for variable text **adjacent** to static text (e.g., key/value pairs).

Backticks intentionally implement a **small, safe subset** of shell-like
behavior. Currently, only the following forms are supported:

- ``echo …`` — evaluate simple text with environment variable expansion.
- ``cat <filename>`` — include the contents of a single file.
  If the file is unreadable, the result is an empty string.

Any other construct results in a parse error. There must be **exactly one space**
between ``echo`` or ``cat`` and the following argument.

Environment variable expansion rules
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

- Both ``$VAR`` and ``${VAR}`` are supported.
- For unbraced ``$VAR``, the variable name ends at the first character
  **not** in ``[A-Za-z0-9_]``; that character is emitted literally.
  This makes expressions like ``$VAR!`` or ``$VAR.suffix`` work as expected.
- For braced ``${VAR}``, expansion ends at the matching ``}``, which naturally
  supports **adjacent** static text (e.g., ``prefix${VAR}suffix``).
- Unknown variables expand to the empty string (not an error).
- Command substitutions and other shell features (e.g., ``$(pwd)``) are
  **not supported** by design.

Examples
========

Simple expansion
----------------

Given ``SOMEPATH=/var/log/custompath``:

.. code-block:: rsyslog

   param = `echo $SOMEPATH/myfile`
   # expands to: /var/log/custompath/myfile

Brace form and adjacent text
----------------------------

Given ``HOSTNAME=myhost``:

.. code-block:: rsyslog

   title = `echo Log-${HOSTNAME}!`
   # expands to: Log-myhost!

Key/value pairs (common with module parameters)
-----------------------------------------------

Given ``KAFKA_PASSWORD=supersecret`` and rsyslog >= 8.2508.0:

.. code-block:: rsyslog

   action(
     type="omkafka"
     broker=["kafka.example.com:9093"]
     confParam=[
       "security.protocol=SASL_SSL",
       "sasl.mechanism=SCRAM-SHA-512",
       "sasl.username=myuser",
       `echo sasl.password=${KAFKA_PASSWORD}`
     ]
   )

.. index::
   single: config.enabled
   single: environment variables; config.enabled
   single: systemd; environment variables
   single: imtcp; conditional loading
   pair: containers; environment-style configuration

Conditional enablement of configuration objects via ``config.enabled``
----------------------------------------------------------------------

You can toggle modules, inputs, actions, etc. based on environment variables by
setting the generic boolean parameter ``config.enabled``. The example below loads
``imtcp`` and starts a TCP listener only if ``LOAD_IMTCP`` evaluates to a
truthy value.

**systemd service (recommended on most distributions)**

Use a drop-in to set the environment variable in rsyslog’s service context:

.. code-block:: bash

   sudo mkdir -p /etc/systemd/system/rsyslog.service.d
   sudo tee /etc/systemd/system/rsyslog.service.d/10-imtcp.conf >/dev/null <<'EOF'
   [Service]
   # Enable
   Environment=LOAD_IMTCP=on
   EOF

   sudo systemctl daemon-reload
   sudo systemctl restart rsyslog

Alternatively, you can point to an ``EnvironmentFile=`` that contains
``LOAD_IMTCP=…``.

.. code-block:: ini

   # /etc/systemd/system/rsyslog.service.d/10-imtcp.conf
   [Service]
   EnvironmentFile=-/etc/default/rsyslog

   # /etc/default/rsyslog
   LOAD_IMTCP=on

**Bash / non-systemd service environment**

If rsyslog is started from a shell wrapper (e.g., SysV init or container entrypoint):

.. code-block:: bash

   # enable (any of 1, true, on, yes will do)
   export LOAD_IMTCP=on

   # or disable
   # export LOAD_IMTCP=off

**RainerScript (rsyslog.conf)**

.. code-block:: rsyslog

   # Load the module conditionally. If LOAD_IMTCP is unset/empty/0/false,
   # the statement is disabled and the module is not loaded.
   module(
     load="imtcp"
     config.enabled=`echo ${LOAD_IMTCP}`
   )

   # Start a TCP input only when enabled. This also works if you prefer
   # to always load the module but gate the input itself.
   input(
     type="imtcp"
     port="514"
     ruleset="in_tcp"
     config.enabled=`echo ${LOAD_IMTCP}`
   )

   ruleset(name="in_tcp") {
     action(type="omfile" file="/var/log/remote/tcp.log")
   }

Accepted truthy value for ``config.enabled`` is ``on``. Any other value,
including no value, is treated as false.

.. note::

   In containerized deployments, this pattern is often the simplest way to pass
   environment-style toggles into a static configuration. For example:

   .. code-block:: bash

      docker run --rm \
        -e LOAD_IMTCP=on \
        -v /path/to/rsyslog.conf:/etc/rsyslog.conf:ro \
        my-rsyslog-image


Including a file’s content
--------------------------

.. code-block:: rsyslog

   token = `cat /etc/rsyslog.d/token.txt`

Compatibility note for older releases
-------------------------------------

On versions **before 8.2508.0**, backticks did **not** support ``${VAR}`` or
adjacent text reliably. If you need to build a ``key=value`` pair, pre-compose
it outside rsyslog and expand the already-complete string, e.g.:

In bash or init script:

.. code-block:: bash

   export KAFKA_PASSWORD='supersecret'
   export SASL_PWDPARAM="sasl.password=${KAFKA_PASSWORD}"

In rsyslog conf:

.. code-block:: rsyslog

   action(
     type="omkafka"
     broker=["kafka.example.com:9093"]
     confParam=[
       "security.protocol=SASL_SSL",
       "sasl.mechanism=SCRAM-SHA-512",
       "sasl.username=myuser",
       `echo $SASL_PWDPARAM`
     ]
   )

Rationale
---------

Backticks are intended to provide just enough functionality to customize
configuration with external data while avoiding the complexity and risks of a
general shell interpreter.