【Rsyslog】 从json 中通过正则 key 获取 value值,rsyslog正则匹配获取key 的 value值

示例:{srv: 'server1', time: 20210101}

普通正则匹配

则正则表达式为:/(?<=srv:)[^,]*//src_ip:([^,]*)/ 可以获取 srv 的值为 'server1'

(?:str)   非捕获组

(?=str) 肯定式向前查找

(?!str) 否定式向前查找

(?<=str) 肯定式向后查找

(?<!str) 否定式向后查找

在rsyslog 中正则匹配json数据

参考链接:
rsyslog regex 测试工具:https://www.rsyslog.com/regex/
文档:https://www.rsyslog.com/doc/v8-stable/configuration/templates.html#property-statement

constant(value="\",\"srv\":\"") property(name="msg" regex.expression="(srv:([^,]*))" regex.type="ERE" regex.submatch="2" regex.nomatchmode="BLANK")
注意此处的匹配是 regex.submatch="2"

template(name="tpl1" type="list" option.json="on") {
    
    
  constant(value="{")
  constant(value="\"timestamp\":\"")      property(name="timereported" dateFormat="rfc3339")
  constant(value="\",\"message\":\"")     property(name="msg")
  constant(value="\",\"srv\":\"")         property(name="msg" regex.expression="(srv:([^,]*))" regex.type="ERE" regex.submatch="2" regex.nomatchmode="BLANK")  
  constant(value="\",\"host\":\"")        property(name="hostname")
  constant(value="\",\"severity\":\"")    property(name="syslogseverity-text")
  constant(value="\",\"facility\":\"")    property(name="syslogfacility-text")
  constant(value="\",\"syslogtag\":\"")   property(name="syslogtag")
  constant(value="\"}")
}

rsyslog疑问?

明明子匹配是取第一位,但是在rsyslog 配置中要 submatch要设置为2

还请各位大佬解惑,^_^

猜你喜欢

转载自blog.csdn.net/qq_22227087/article/details/115333912
今日推荐