go - 使用telegraf input.logparser.grok时用于过滤字符串的自定义模式

标签 go devops influxdb logparser telegraf

我正在尝试使用正则表达式过滤日志文件中的特定单词,目标是与custom_pattern中的正则表达式匹配的任何日志行都将进入influxdb,不匹配的日志行将被忽略。当我测试正则表达式时,即使在golang游乐场(https://play.golang.org/p/_apzOVwwgl2)中,它也可以工作。但是,当我在下面的telegraf conf文件中使用它时,它不起作用,没有输入到influxdb中。是否缺少我应该添加到配置中的内容?

我已经在http://grokdebug.herokuapp.com/https://play.golang.org/p/_apzOVwwgl2上测试了正则表达式,但它在[inputs.logparser.grok]下的custom_patterns中无法使用。

这是我的希腊配置

[[inputs.logparser]]
   files = ["/var/log/test1"]
   from_beginning = true

   [inputs.logparser.grok]
      patterns = ["%{FAIL_LOG}"]
      custom_patterns = '''FAIL_LOG ^.*?\b(multipathd?)\b.*?\b(failed|failing|(remaining active paths))\b.*?$'''

该模式应该与下面的前2条日志行匹配,并忽略第三行。
Oct 29 03:29:03 dc-as-5p multipath: checker failed interface 8:0 in map 150gb
Oct 29 03:29:03 dc-as-5p multipathd: checker failing interface 8:0 in map 150gb
Oct 29 03:26:03 dc-as-5p link: checker down remaining active paths interface 8:0 in map 150gb

我究竟做错了什么?

最佳答案

如果使用--debug标志运行telegraf,您将看到它在解析日志时遇到问题。

$ telegraf --debug --config ./telegraf.conf
...
2019-11-17T05:01:07Z D! Grok no match found for: "Oct 29 03:29:03 dc-as-5p multipath: checker failed interface 8:0 in map 150gb"
2019-11-17T05:01:07Z D! Grok no match found for: "Oct 29 03:29:03 dc-as-5p multipathd: checker failing interface 8:0 in map 150gb value=3"
2019-11-17T05:01:07Z D! Grok no match found for: "Oct 29 03:26:03 dc-as-5p link: checker down remaining active paths interface 8:0 in map 150gb"

此错误消息具有误导性,因为如您的测试所示,您的正则表达式模式正确。真正的问题是您尚未包含要在正则表达式中记录的值。

用于存储错误消息和时间戳的正则表达式版本可能是:
    custom_patterns = '''FAIL_LOG %{SYSLOGTIMESTAMP:timestamp}.*(multipath).?: %{GREEDYDATA:message:string}'''

值模式可以在${}之间找到。可以在here中找到其他预制样式。这将消除上面的前两个错误。使用--test标志可以看到这些结果。
$telegraf --test --config ./telegraf.conf
...
> logparser,host=pop-os,path=./test1 message="checker failed interface 8:0 in map 150gb",timestamp="Oct 29 03:29:03 " 1573968174161853621

由于某种原因,--test标志并不总是输出结果。在获得上述输出之前,我将不得不多次运行命令。

关于go - 使用telegraf input.logparser.grok时用于过滤字符串的自定义模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58614218/

相关文章:

go - 将 csv.Reader() 用于 "chan string"的有效方法

java - 在 Jetty 中为每个 ServerConnector 设置专用的执行器

java - 如何定义到 InfluxDB 的 Apache Camel 路由

java - 如何从 Apache Beam 作业正确写入 InfluxDB?

戈朗 : ssh: handshake failed: EOF

go - 当我调用 SendMessage(msg) 时,我总是得到 0 个分区。我通过命令行指定 12

go - 如何在接口(interface)上调用 len()?

amazon-web-services - 任何 SSL 证书都适用于节点

google-cloud-platform - 将 MQL 与 Google Cloud Monitoring (Stackdriver) 警报策略结合使用

influxdb - 当 select count 没有结果时返回 0 而不是 N/A (null)