首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >logstash -使用Ruby FIlter解析日志中的数字并转换其中的存储单元

logstash -使用Ruby FIlter解析日志中的数字并转换其中的存储单元
EN

Stack Overflow用户
提问于 2020-02-08 04:33:19
回答 1查看 154关注 0票数 0

从日志中提取数字并转换存储单元。

示例日志-

2020-02-04 16:18:56,783信息Log4jFactory$Log4jLogger 10.xxx.xxx.xxx:5701 Dry-PROD-XC6已收到Connectionid=26876的身份验证,/10.xxx.xxx.xxx:5701->/10.xxx.xxx.xxx:56584,endpoint=null,alive=true,type=CSHARP_CLIENT,身份验证成功,主体:Log4jLogger所有者连接: true,客户端版本: null

2020-02-04 16:15:27,519信息Log4jFactory$Log4jLogger 10.xxx.xxx.xxx:5701 Dry-PROD-XC6 processors=8,physical.memory.total=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.ememy.used/总数=89.51%,heap.memory y.used/最大=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,executor.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.count=1,proxy.count=0,clientEndpoint.count=231,connection.active.count=232,client.connection.count=231,connection.count=1

尝试提取内存字段(如native.memory.used=8.2M、native.memory.free=3.5G、native.memory.total=64.0M、native.memory.max=3.5G)的数字,并使用Ruby Filter转换单位。

首先使用Logstash KV过滤器获取KV对,然后尝试以下代码(我是Ruby编码新手)

我的Ruby代码-

代码语言:javascript
复制
               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,603logstash.filters.ruby发生异常: nil:NilClass的未定义方法‘`scan’

感谢任何帮助或指导。谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-02-08 06:30:01

可以做到这一点:

如果您想要匹配该单位:

\d*?\.\d*?(M|G)

如果您只想匹配数字(整数或小数):

\d*?(\.)\d*

\d代表数字

*站了x次

这里使用?是为了在*选项(很好的实践)之后不贪婪(最大限度地减少解析器的工作量)

( | )代表OR并在两个字符M或G之间设置选项(这可能会根据执行解析器所使用的语言而有所不同

如果您有任何疑问,可以测试常规表达式here

如果使用{}控制小数位数,则可以优化正则表达式;如果不确定小数是否始终存在,则可以添加可选的.

据我所知,您可以在logstash中选择使用regex进行解析,并使用grok或内置筛选器正确输出,然后根据需要直接输出。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60120656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档