rsyslog 模板日期/时间格式(带秒)

rsyslog 模板日期/时间格式(带秒)

我当前的 rsyslog 模板配置如下:

/etc/rsyslog.d/00-samba-audit.conf

template(name="sambalog" type="string"
 string="%$year%-%$month%-%$day% %$hour%:%$minute% %HOSTNAME% %app-name% %msg%\n")

if $programname == 'smbd_audit' then /var/log/samba/log.audit;sambalog

可悲的是,毫无原因地没有秒数的变量。

问题:我怎样才能使日期/时间格式变为下一个?

2018-08-09 20:12:58

答案1

变量$year, $day, ...$minute指的是当前的时间。你更希望知道事件发生的时间戳生成或者已报告(看这里差值)。属性timegeneratedtimereported(== timestamp)允许进一步处理(即,您可以从这些属性中选择某些字段)。您不能从当前的时间,但只能来自上面提到的两个时间戳。所以:

template(name="sambalog" type="string"
    string="%timereported:::date-year%-%timereported:::date-month%-%timereported:::date-day% %timereported:::date-hour%:%timereported:::date-minute%:%timereported:::date-second% %HOSTNAME% %app-name% %msg%\n")

哇,这是一行很长的线,而不是将模板定义为细绳你也可以将其定义为列表。行为是一样的,只是定义不同,并且允许在中间使用换行符和注释。也许这可以提高可读性:

template(name="sambalog_list" type="list") {
    property(name="timereported" dateFormat="year")
    constant(value="-")
    property(name="timereported" dateFormat="month")
    constant(value="-")
    property(name="timereported" dateFormat="day")
    constant(value=" ")
    property(name="timereported" dateFormat="hour")
    constant(value=":")
    property(name="timereported" dateFormat="minute")
    constant(value=":")
    property(name="timereported" dateFormat="second")
    constant(value=" ")
    property(name="hostname")
    constant(value=" ")
    property(name="app-name")
    property(name="msg" spifno1stsp="on" ) # add space if $msg doesn't start with one
    property(name="msg" droplastlf="on" )  # remove trailing \n from $msg if there is one   
    constant(value="\n")
}

if $programname == 'smbd_audit' then /var/log/so-test.log;sambalog_list

当我把上面的内容放入/etc/rsyslog.d/so.conf然后systemctl restart syslog.service最终发出

logger -t smbd_audit "Hello, $RANDOM"

那么该文件/var/log/so-test.log包含:

2018-10-12 22:14:12 myhost smbd_audit Hello, 15793

答案2

根据rsyslog 文档

百分号('%')之间的文本由 rsyslog 属性替换器解释。

属性替换器文档说:

日期-秒

仅时间戳的第二部分(2 位数字)

因此您应该能够输入%second%%date-second%放入您的模板中来列出秒数。

答案3

我这样做了

%timegenerated:::date-pgsql%

结果看起来像2019-09-05 12:22:46

相关内容