简体   繁体   English

如何在logstash中使用mutate和gsub替换值的一部分

[英]How to replace the part of value using mutate and gsub in logstash

I've got in my log file something like this:我的日志文件中有这样的内容:

DATATYPE::SERVICEPERFDATA       TIMET::1519222690       HOSTNAME::localhost     SERVICEDESC::Total Processes    SERVICEPERFDATA::procs=59;250;400;0;    SERVICECHECKCOMMAND::check_local_procs!250!400!RSZDT  HOSTSTATE::UP    HOSTSTATETYPE::HARD     SERVICESTATE::OK        SERVICESTATETYPE::HARD

I want to replace SERVICEPERFDATA::procs=59;250;400;0;我想替换SERVICEPERFDATA::procs=59;250;400;0; this key value pair in my message to something like this.我的消息中的这个键值对是这样的。

SERVICEPERFDATA::59

so I can use kv filter to split the data into key and value.所以我可以使用 kv 过滤器将数据拆分为键和值。

I've tried with Logstash mutate and gsub but couldn't find the right regex to achieve my goal.我尝试过使用 Logstash mutate 和 gsub,但找不到合适的正则表达式来实现我的目标。

Thanks, Charan谢谢,查兰

You can use a capturing group to grab a part of a regex and use it in the replace part of the mutate/gsub configuration.您可以使用捕获组来获取正则表达式的一部分并将其用于 mutate/gsub 配置的替换部分。

mutate {
  gsub => ["message","(?<=SERVICEPERFDATA::)procs=(\d+);\S+", "\1"]
}

The (?<=SERVICEPERFDATA::) make it so the regex only apply to SERVICEPERFDATA:: . (?<=SERVICEPERFDATA::)使正则表达式仅适用于SERVICEPERFDATA:: The procs=(\d+);\S+ regex will put all the numbers between procs= and the next ; procs=(\d+);\S+正则表达式会将所有数字放在procs=和下一个; in a group, which is then used in the replace part of the configuration ( "\1" ).在一个组中,然后用于配置的替换部分( "\1" )。

See a regex explanation .请参阅正则表达式解释

Result of the filter: SERVICEPERFDATA::59过滤结果: SERVICEPERFDATA::59

Another option would be to use two mutate/gsub filters, which would have each a simpler configuration.另一种选择是使用两个 mutate/gsub 过滤器,每个过滤器都有一个更简单的配置。

Regex : procs=([^;]+)\S+ or (?<=SERVICEPERFDATA::)procs=([^;]+)\S+ Substutution : \1正则表达式procs=([^;]+)\S+(?<=SERVICEPERFDATA::)procs=([^;]+)\S+替代\1

Details:细节:

  • () Capturing group ()捕获组
  • [^] Match a single character not present in the list [^]匹配列表中不存在的单个字符
  • + Matches between one and unlimited times +一次和无限次之间的匹配
  • \S Matches any non-whitespace character (equal to [^\r\n\t\f\v ] ) \S匹配任何非空白字符(等于[^\r\n\t\f\v ]
  • \1 Group 1. \1组 1。

Code:代码:

mutate {
    gsub => [
      "fieldname", "procs=([^;]+)\S+", "\1",
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM