我们的 Splunk 中存在两种类型的日志记录:
第一条记录格式:
Transfer money { "accountNo" : "123" , "transaction-id":"1234567890" }
第二记录格式:
[1234567890] Transfer failed, mainframe is offline
第一条记录只是交易的详细信息,其中 1234567890 是交易 ID。第二条记录只是事务中发生的异常详细信息的日志。 []内的值为发生异常的交易的transaction-id。
在我的 splunk 查询中,我想列出由 account = 123 发起且导致失败的所有转账交易,详细信息为“转账失败,主机离线”
这可能吗?我做了一些研究,发现联接可以用于此目的,但是如何提取事务 ID 并将其用作联接的公共字段值?
您的开发人员通过创建如此不同格式的日志消息并没有为您提供任何服务。不过,仍然可以做到。
是的,可以使用
join
,但应避免使用。
我将使用示例代码进行解释。首先阅读感兴趣的事件:“转账”和“转账失败,主机离线”。
index=foo ("Transfer money" OR "Transfer failed, mainframe is offline")
``` Extract the account number field ```
| rex "accountNo\\\" : \\\"(?<accountNo>[^\"]+)"
``` Extract the transaction ID from "Transfer money" events ```
| rex "transaction-id\\\":\\\"(?<transID>[^\"]+)"
``` Extract the transaction ID from "Transfer failed..." events ```
| rex "\[(?<transID>[^\]]+)"
``` Extract the error message from "Transfer failed..." events ```
| rex "] (?<msg>.*)"
``` Group the results by transaction ID ```
| stats values(*) as * by transID
``` Keep only the events with the requested error message ```
| where msg="Transfer failed, mainframe is offline"