ruby-on-rails - logstash - 使用 Ruby 过滤器从日志中解析数字并转换存储单元
问题描述
从日志中提取 NUMBER 并转换存储单元。
样本日志 -
2020-02-04 16:18:56,783 INFO Log4jFactory$Log4jLogger [10.xxx.xxx.xxx]:5701 [Dry-PROD-XC6] [3.7.6] 从 Connection[id=26876, /10.xxx .xxx.xxx:5701->/10.xxx.xxx.xxx:56584, endpoint=null, alive=true, type=CSHARP_CLIENT], 认证成功, principal : ClientPrincipal{uuid='d7d8b718-ed75-4cc3-b51a- c620bb082255',ownerUuid='058720ad-7b35-40f6-8978-bd9cf7e286ec'},所有者连接:true,客户端版本:null
2020-02-04 16:15:27,519 信息 Log4jFactory$Log4jLogger [10.xxx.xxx.xxx]:5701 [Dry-PROD-XC6] [3.7.6] 处理器 = 8,物理内存总 = 31.4G, physical.memory.free=18.8G, swap.space.total=7.8G, swap.space.free=7.8G, heap.memory.used=4.5G, heap.memory.free=536.5M, heap.memory.total =5.0G, heap.memory.max=5.0G, heap.memory.used/total=89.51%, heap.memory.used/max=89.51%, native.memory.used=8.2M, native.memory.free= 3.5G, native.memory.total=64.0M, native.memory.max=3.5G, native.meta.memory.used=80.0M, native.meta.memory.free=432.0M, native.meta.memory.percentage =90.75%,minor.gc.count=18605,minor.gc.time=121136ms,major.gc.count=0,major.gc.time=0ms,load.process=0.00%,load.system=0.01%, load.systemAverage=1.00%, thread.count=69, thread.peakCount=229, cluster.timeDiff=4083, event.q.size=0, executor.q.async.size=0, executor.q.client.size =0,执行者.q。query.size=0,executor.q.scheduled.size=0,executor.q.io.size=0,executor.q.system.size=0,executor.q.operations.size=0,executor.q。 priorityOperation.size=0,operations.completed.count=351751533,executor.q.mapLoad.size=0,executor.q.mapLoadAllKeys.size=0,executor.q.cluster.size=0,executor.q.response。 size=0,operations.running.count=0,operations.pending.invocations.percentage=0.00%,operations.pending.invocations.count=1,proxy.count=0,clientEndpoint.count=231,connection.active.count =232,client.connection.count=231,connection.count=1Operations.running.count=0,operations.pending.invocations.percentage=0.00%,operations.pending.invocations.count=1,proxy.count=0,clientEndpoint.count=231,connection.active.count=232,客户端.connection.count=231,connection.count=1Operations.running.count=0,operations.pending.invocations.percentage=0.00%,operations.pending.invocations.count=1,proxy.count=0,clientEndpoint.count=231,connection.active.count=232,客户端.connection.count=231,connection.count=1
尝试提取内存字段的 NUMBER(如 native.memory.used=8.2M、native.memory.free=3.5G、native.memory.total=64.0M、native.memory.max=3.5G)并转换单位使用红宝石过滤器。
首先使用 Logstash KV Filter 获取 KV 对,然后尝试以下代码(我是 Ruby Coding 新手)
我的红宝石代码 -
ruby {
code => '
event.to_hash.keys.each { |k,v|
matches = v.scan(/(\d*\.\d*?i)([KMG])$/)
if matches[2] == nil
event.set(k,event.get(v))
elsif matches[2] == "K"
multiplyBy = 1024
event.set(k, matches[1].to_f * multiplyBy)
elsif matches[2] == "M"
multiplyBy = 1024 * 1024
event.set(k, matches[1].to_f * multiplyBy)
elsif matches[2] == "G"
multiplyBy = 1024 * 1024 * 1024
event.set(k, matches[1].to_f * multiplyBy)
else
event.set(k, event.get(v))
end
}
'
}
在日志中看到错误 -
[2020-02-11T17:01:09,603][ERROR][logstash.filters.ruby][main] 发生 Ruby 异常:nil:NilClass 的未定义方法“扫描”
感谢任何帮助或指导。谢谢
解决方案
正则表达式可以解决问题:
如果要匹配单位:
\d*?\.\d*?(M|G)
如果您只想匹配数字(整数或小数):
\d*?(\.)\d*
\d
代表数字
*
站 x 次
?
在 * 选项之后使用这里是非贪婪的(最小化解析器的工作量)(好的做法)
( | )
代表 OR 并在 2 个字符 M 或 G 之间设置选项,(
这可能会因执行解析器的语言而异
如果您有任何疑问,可以在这里测试正则表达式。
如果您控制小数位数,则可以优化您的正则表达式,如果您不确定是否始终使用小数{}
,则添加可选的。.
据我记得,您可以在 logstash 中选择使用正则表达式进行解析,并使用 grok 或内置过滤器正确输出,然后根据需要直接输出。
推荐阅读
- python - 在 TensorFlow 中面临有关 OwnedIteror 属性错误的错误
- npm - 当我尝试将镭包安装到 Reactjs 时,会发生此错误,我该怎么办?
- javascript - 无法在选择更改和代码性能问题 ReactJS 中控制事件
- c# - 匹配 excel 函数名称的正则表达式/将 excel 函数名称替换为另一个函数名称
- python-3.x - 如何将熊猫列中的变量范围与另一列相加
- javascript - iFrame 中的 Pannellum 突然停止工作
- c# - 如何在 LINQ 查询中命名中间实体列表?
- tomcat - kubernetes redinessProbe httpGet on tomcat image 报告连接被拒绝
- android - GSON 阵列去
- google-apps-script - 具有多个范围和多个条件的用于条件格式的 Google 脚本