大网数据广告活动
服务器运维问答

Using linux environment variable in rsyslog template

· 52CE 我爱测

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' en…

问题原文

原帖链接 · Hanoch Giner · CC BY-SA 4.0

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

已采纳解答

原帖链接 · Datum Geek · CC BY-SA 4.0

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")
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-04-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。