如何在 Logstash 中将一个事件的字段引用到另一个事件?

2024-05-10

我目前正在处理日志,其中一些内容如下所示:

00:19:59.771 (07120/evtThread     ) TRC> Cem< [Core1] CALL_STATE... 
00:20:00.199 (05768/BCMApplicationThread) INF> 
#S#|Call stats, ongoing calls: 8, handled_calls: 7304
#S#+----------------------------+----------+----------+----------+----------+----------+
#S#|Peer                        |      From|        To|   MinTime|   MaxTime|   AvgTime|
#S#+----------------------------+----------+----------+----------+----------+----------+
#S#|        CallDispatcher:Core2|         0|         0|         0|         0|         0|
#S#|        CallDispatcher:Core3|         0|         0|         0|         0|         0|
#S#|                   Cem:Core1|      1632|      6207|         0|   5996522|    311685|

我像这样解析了包含时间的行:

grok {
    match => [ "message", "%{TIME:time} (?<bcm_comp>\(\d{5}\/\w{4,}\:*\ *\w*\)) (?<loglevel>\w{3}>{1}) %{GREEDYDATA:message}" ]
    overwrite => [ "message" ]
    add_field => [ "BCM_System", "PROD" ]
}

前面包含#S#的行是这样解析的。忽略包含 -------- 的行以及包含表标题和呼叫统计行的行。

grok {
    match => [ "message", "(?<start>\#\S\#\|)\s* (?<peer>\w*\:\w*)(?<div2>\|)\s* %{NUMBER:From}(?<div3>\|)\s* %{NUMBER:To}(?<div4>\|)\s* %{NUMBER:MinTime}(?<div5>\|)\s* %{NUMBER:MaxTime}(?<div6>\|)\s* %{NUMBER:AvgTime}(?<div7>\|)" ]        
    remove_field => [ "start", "div2", "div3", "div4", "div5", "div6", "div7" ]
    overwrite => [ "message"]       
    add_field => [ "reference_time", "%{@time}"]
}

我想做的是从上一行中获取时间并将其添加为我摸索 #s# 行的字段。我尝试使用 Logstash 中的 add_field 语法,如图所示,但它不起作用......它只是字面上打印出%{@time}。

有什么方法可以以某种方式从前一行中提取该时间并将其放入另一个事件的字段中吗?


据我所知,你必须编写一个过滤器插件才能执行类似的操作。这是我组合在一起做类似事情的一个简单插件 - 当它看到一个字段时它会记住它,然后使用它看到的最后一个值(如果它不存在)。

# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"
require "set"
#
# This filter will look for a field from an event and record the last value
# of it.  If it's not present, it will add the last value to the event
#
# The config looks like this:
#
#     filter {
#       memorize {
#         field => "time"
#         default => "00:00:00.000"
#       }
#     }
#
# The `field` is the name of the field that you want to memorize
# The `default` is the value to use for the field if you haven't seen it yet
#   in the file (this is optional)

class LogStash::Filters::Memorize < LogStash::Filters::Base

  config_name "memorize"
  milestone 1

  # The field to memorize
  config :field, :validate => :string, :required => true
  # the default value to use for the field if it's not seen before we need it
  config :default, :validate => :string, :required => false

  # The stream identity is how the multiline filter determines which stream an
  # event belongs to. See the multiline plugin if you want more details on how
  # this might work
  config :stream_identity , :validate => :string, :default => "%{host}.%{path}.%{type}"

  public
  def initialize(config = {})
    super

    @threadsafe = false

    # This filter needs to keep state.
    @memorized = Hash.new
  end # def initialize

  public
  def register
    # nothing needed
  end # def register

  public
  def filter(event)
    return unless filter?(event)

    if event[@field].nil?
      val = @memorized[@stream_identity]
      if val.nil?
        val = @default
      end
      event[@field] = val
      filter_matched(event)
    else
      @memorized[@stream_identity] = event[@field]
    end
  end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Logstash 中将一个事件的字段引用到另一个事件? 的相关文章

随机推荐

  • 自定义 Netbeans 平台配置应该在哪里?文件是这样让maven找到它吗?

    在 NetBeans 平台之上构建的应用程序具有
  • 将 UIImage 放入 UIButton 的简单方法

    我的 iPhone 应用程序中有一个 UIButton 我将其大小设置为 100x100 我有一个 400x200 的图像 我希望在按钮中显示它 按钮仍然需要保持在 100x100 并且我希望图像缩小以适应 但是 保持正确的纵横比 我想这就
  • 普罗米修斯警报中缺少标签

    我对 Prometheus 警报规则有疑问 我设置了各种 cAdvisor 特定警报 例如 alert ContainerCpuUsage expr sum rate container cpu usage seconds total 3m
  • dompdf 在文档末尾插入空白页

    我正在使用 dompdf 0 6 0 生成 pdf 文档 并且遇到一个奇怪的问题 即最后创建了一个空白页面 我的 简化的 html
  • 如何在两个不同帐户之间设置无密码身份验证

    我们可以在两台机器的两种不同用途之间设置无密码身份验证吗 例如 计算机A有用户A 计算机B有用户B 我们可以设置密码 ssh 以便计算机 A 上的用户 A 使用其用户帐户 A 登录计算机 B 谢谢你 如果我理解你的问题 你能设置一下吗ssh
  • 在 ios wifi 网络上查找对等点

    我试图弄清楚如何搜索登录到 wifi 网络且在特定端口上托管应用程序的其他设备 在不知道这些其他设备的地址甚至不知道它们托管的端口的情况下 如何检测它们的存在 一旦发现 我应该能够联系该设备并与其建立连接 最标准的 iOS方式 是使用Bon
  • 什么是 Java 8“视图”?

    我正在观看 Paul Philips 的演讲 http www youtube com watch v TS1lpKBMkgg http www youtube com watch v TS1lpKBMkgg 在 12 48 比较 Scal
  • C# 等价于 C++ 向量或双端队列

    我几乎可以肯定这应该是重复的 但我搜索了一段时间但找不到答案 我应该在 C 中使用什么来替换 C 向量和双端队列有效率的 也就是说 我需要一种有效支持直接索引的结构 并且还支持以有效的方式再次从一端或两端删除 取决于向量或双端队列的情况 在
  • 按下按钮时如何更改 Twitter Bootstrap 选项卡?

    我需要在按下按钮时更改选项卡 并且选项卡应由 id 标识 以下代码对我不起作用 只是重新加载页面 div class form actions div
  • 从 Gmail 获取 pdf 附件作为文本

    我在网络和 Stack Overflow 上搜索但没有找到解决方案 我尝试做的事情如下 我通过邮件收到某些附件 我希望将其作为 纯 文本进行进一步处理 我的脚本如下所示 function MyFunction var threads Gma
  • 在java中将StreamWriter转换为OutputStream?

    我正在尝试使用 System setOut 将 System out 重定向到字符串 它需要一个 PrintStream 有什么方法可以将 StringWriter 转换为 Stream 以便我可以将其传递给 setOut 吗 你不能完全这
  • 通过服务删除 Windows 登录屏幕

    我正在尝试从服务启动的可执行文件中删除 Windows 登录屏幕 winlogon 该服务将随 Windows 自动启动 并等待来自另一台计算机的命令 当它收到命令时 它将启动一个 exe 该 exe 将在特定用户名下启动 cmd exe
  • WPF DataGridTemplateColumn 标题绑定不起作用

    我将 DataGrid 与一些手动创建的 DataGridTemplateColumns 一起使用 这是工作中的代码片段
  • 扩展 Django 1.11 用户模型

    我正在尝试弄清楚如何扩展 Django 用户模型以向用户添加信息 我似乎无法让它发挥作用 我究竟做错了什么 我可以在扩展的同一模型中使用外键吗 如何创建超级用户 还是必须通过手动创建python manage py shell 到目前为止
  • 尝试使用 Eclipse 启动 Glassfish 服务器时出现 org.apache.catalina.LifecycleException

    我一直忙于使用 angularjs 前端构建一个 REST 应用程序 使用 MAVEN jersey quickstart webapp 使用 GLASSFISH Web 服务器在 Eclipse 上开发 今天 当我开始对项目进行一些开发时
  • UEFI LoadImage 挂起

    我正在考虑使用 Bootservices 函数 LoadImage 从内存加载 UEFI 应用程序映像 函数参数为 typedef EFI STATUS LoadImage IN BOOLEAN BootPolicy IN EFI HAND
  • 如果文件类型 == tex

    如果文件是乳胶文件 我想在 vimrc 中运行命令 我想我的语法有问题 但它不起作用 有什么线索吗 if filetype tex set spell endif 您可以使用自动命令来实现您想要的 autocmd BufNewFile Bu
  • 如何清除ember js中的表单数据

    嗨 我对 ember js 很陌生 我写了一个新员工入职表格 并通过路线发送数据 数据保存成功 但问题是提交表单后我的表单数据没有清除 代码如下 app js App Router map function this resource sa
  • Azure Web 部署“发布失败”

    各位开发人员大家好 我目前正在努力解决最新的 Visual Studio 2017 Professional 版本 15 8 1 中的问题 自从更新到版本 15 8 以来 我无法通过 Web 部署发布任何 Azure Functions 每
  • 如何在 Logstash 中将一个事件的字段引用到另一个事件?

    我目前正在处理日志 其中一些内容如下所示 00 19 59 771 07120 evtThread TRC gt Cem lt Core1 CALL STATE 00 20 00 199 05768 BCMApplicationThread