Sunday, February 10, 2013

multi-character field delimiters

On the rsyslog mailing list, the ability to use multiple characters as field delimiters had been requested recently. Today, I took some time off the my schedule and implemented that functionality. It is probably very useful for a number of cases. An important one is probably in combination with control character escaping, where rsyslog by default expands a single character into a four-byte escape "#ooo" with o being the octal character code (so  e.g. US ASCII HT [horizontal tab] becomes "#011").

The new functionality is available for the RainerScript field() function. I do not intend to add it to template strings.

Some quick usage sample:

The following is the traditional way of single-byte delimiters, here with the comma character (US ASCII decimal code 44):
set $!usr!field2 = field($msg, 44, 2);
template (name="fld" type="string" string="'%$!usr!field2%' -- msg: %msg%\n")
action(type="omfile" file="/path/to/logfile" template="fld")

And this is the same with the string "#011" as delimiter:
set $!usr!field2 = field($msg, "#011", 2);
template (name="fld" type="string" string="'%$!usr!field2%' -- msg: %msg%\n")
action(type="omfile" file="/path/to/logfile" template="fld")

Note that the field number (index) need not necessarily to be fixed. It can be derived from an appropriately formatted message. Here the first field contains the actual field to extract, delimiter is "#011" again:
set $!usr!idx = field($msg, "#011", 1);
set $!usr!field = field($msg, "#011", $!usr!idx);
template (name="fld" type="string" string="'%$!usr!field%' -- msg: %msg%\n")
action(type="omfile" file="/path/to/logfile" template="fld")
In that last sample the $msg of

"3#011val 1#011val 2#011val 32#val 4"

would return

"val 2"

Keep in mind that the first field is the field index, so the actual data fields start at 2 (field 1 is "3", field 2 is "val 1", field 3 "val 2" and so on).

This functionality is already present in git master head and will be released as part of 7.3.7 in the not so distant future. Some more details can be found inside the RainerScript documentation page.

No comments:

Busy at the moment...

Some might have noticed that I am not as active as usual on the rsyslog project . As this seems to turn out to keep at least for the upcomi...