5

I'm trying to add an environment variable to my rsyslog template.
I tried using 'getenv()' function without any luck, I always get an empty string in return.

I'm attaching an example of my rsyslog config file, I'm using 'HOME' env variable as an example.

/etc/rsyslog.d/00-my_log.conf

set $.my_home=getenv("HOME");

template(name="json-template"
  type="list") {
    constant(value="{")
      constant(value="\",\"my_home\":\"")     property(name="$.my_home")
      constant(value="\",\"message\":\"")     property(name="msg" format="json")
    constant(value="\"}\n")
}

*.* action(type="omfile" dirCreateMode="0700" FileCreateMode="0644"
       template="json-template" File="/var/log/my_log")

/var/log/my_log

{"my_home":"","message":"my log message"}

rsyslogd: version 8.4.2

Hanoch Giner
  • 153
  • 1
  • 4
  • 1
    This works for me, but I have version 8.30.0. If you run `rsyslogd -dn` you will get lots of debug output. There might be an error message about getenv in there. For a normal input line I see in the debug: `SET .my_home = function 'getenv' ... string 'HOME' `END SET` – meuh Apr 12 '19 at 18:48
  • Sadly I can't easily update my rsyslog version, it will require me to compile rsyslog and its dependencies to my specific Debian armv7l distribution. With `rsyslogd -dn`, I can see the line you mentioned, it actually defines to variable 3 times but in the end it's still an empty string. Anyway, I'll just find a workaround as this issue doesn't worth getting into a compilation session. Thanks for the help! – Hanoch Giner Apr 14 '19 at 08:27

1 Answers1

3

i was able to use this guidance rsyslog: Specify `action` parameters from environment variables

to shell out from the config file to get the environment variables in the template:

template(name="logfile" type="list") {
   constant(value="{")

   constant(value="\"@timestamp\":\"")     
   property(name="timegenerated" dateFormat="rfc3339")
   constant(value="\", ")
   
   constant(value="\"pod_namespace\":\"")     
   constant(value=`echo $KUB_POD_NAMESPACE`)
   constant(value="\", ")
   
   constant(value="\"pod_name\":\"")     
   constant(value=`echo $KUB_POD_NAME`)
   constant(value="\", ")
   
   constant(value="\"pod_ip\":\"")     
   constant(value=`echo $KUB_INSTANCE_ADDR`)
   constant(value="\", ")
   
   property(name="$!all-json" position.from="2")
}
Datum Geek
  • 146
  • 4