EDIT: More context.

I have the follow example:

set $.dev1IP =;
set $.dev2IP =;
set $.dev3IP =;

set $.sourceIP = $fromhost-ip;
set $.sourceTag = "";

template(name="temp1" type="string" string="/path/to/logDir/%.sourceIP%/%.sourceIP%.%.sourceTag%.log")

ruleset(name="rules1") {
if ($.sourceIP == $.dev1IP) then {
    set $.sourceTag = "tag1";
    do something
} else if ($.sourceIP == $.dev2IP) then {
    set $.sourceTag = "tag2";
    do something
} else if ($.sourceIP == $.dev3IP) then {
    set $.sourceTag = "tag3";
    do something
} else {
    do something if nothing else matches
action(type="omfile" dynaFile="temp1")

This is a more complete code block to show how I know it's not working. I have logs coming in from those IP addresses but the dynamic file generated is named "" when instead it should be named "".

Running "rsyslogd -N1 -f /etc/rsyslog.conf" results in no errors.

What is the correct conditional statement to make that comparison work?

  • Besides the ruleset being defined, there's no other mention of that comparison and rsyslog doesn't return any errors when executing rsyslogd -N1. What am I supposed to look for in the debug? – AnthonyBB May 13 at 14:26
  • See updated question. – AnthonyBB May 13 at 14:55
  • In the template, the properties are missing their $ sign, eg %.sourceTag% should be %$.sourceTag%, and the first 3 lines are missing their set command. – meuh May 13 at 15:12
  • While I forgot to write it in this post, I am properly setting the variables in the configuration. The issue is definitely not that it is missing a $ as it is currently working without them. The $.sourceTag is set to "" and that is why the dynamic file is being named $.sourceIP..log. The if statements are not properly comparing and therefore the $.sourceTag variable never gets a value. The issue still stands that comparing the $.sourceIP against a pre-set variable is never returning true. So how do I run that comparison? – AnthonyBB May 13 at 19:58

I tried a simpler example, setting a local variable outside a ruleset, then accessing it inside the ruleset, and its value was empty. I presume this is a feature, to make ruleset spaces more independent.

I'm not sure what can be done about this and I do not have any suitable counter-examples. The same is true for $!var json-style variables. There is no mention of this in ruleset concepts.

The extensive debugging you get with rsyslog -dn confirms how a variable set with set $.var1 = "abc"; outside the ruleset is (null) inside the ruleset for the evaluation of if($.var1 == $.mydir)... where $.mydir is set inside the ruleset:

eval expr 0x556bdce30480, return datatype 'S':0
        var '.var1'
        var '.mydir'
eval expr 0x556bdce30560, type 'CMP_EQ'
eval expr 0x556bdce30390, type 'V'
rainerscript: (json/string) var 202: '(null)'
eval expr 0x556bdce30390, return datatype 'S':0
eval expr 0x556bdce1e2b0, type 'V'
rainerscript: (json/string) var 202: 'abc'
if condition result is 0
| improve this answer | |
  • The key was to set the local variables within the ruleset. Even using an include() function with variables defined in another file works as long as they are included within the ruleset that references them. Thank you! – AnthonyBB May 23 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.