如何使用rex从日志文件获取两个字段?

问题描述 投票:0回答:1

我是Splunk的新手。我的目标是从日志中获取两个或多个字段。我必须检查一个字段是否为true,因此请使用另一个字段进行计数。计数器关于客户端使用user-agent属性发出的请求数量。

我希望的逻辑:

int count1, count2;
count1 = 0;
count2 = 0;

if (GW == true) {
  if (UA == "user-agent1") count1++;
  if (UA == "user-agent2") count2++;
}

此刻,我只能得到一个字段,并在没有if-condition的情况下进行计数。

此查询工作正常,并返回正确的请求计数器:

source="logfile.log" | rex "UA=(?<ua>\w+)" | stats count(eval(ua="user-agent1")) as USER-AGENT1

但是,当我尝试获取第二个字段(GW)进行逻辑运算时,查询返回0

source="logsfile.log" | rex "UA=(?<ua>\w+) GW=(?<gw>\w+)" |stats count(eval(ua="user-agent1")) as USER-AGENT1

所以,我如何获得更多字段以及如何在查询中添加if-condition

示例日志:

2020-01-10 14:38:44,539 INFO  [http-nio-8080-exec-8] class:ControllerV1, UA=user-agent1, GW=true
2020-01-10 14:23:51,818 INFO  [http-nio-8080-exec-3] class:ControllerV1, UA=user-agent2, GW=true
splunk splunk-query
1个回答
0
投票

将会是这样:

source="logsfile.log" UA GW 
| rex "UA=(?<ua>\w+), GW=(?<gw>\w+)" 
| stats count(eval(gw="true" AND ua="user-agent1")) as AGENT1, 
        count(eval(gw="true" AND ua="user-agent2")) as AGENT2

例如,如果您不知道变量的顺序或有两个以上,则可以使用单独的rex语句:

source="logsfile.log" UA GW 
| rex "UA=(?<ua>\w+)"
| rex "GW=(?<gw>\w+)" 
| stats count(eval(gw="true" AND ua="user-agent1")) as AGENT1, 
        count(eval(gw="true" AND ua="user-agent2")) as AGENT2
© www.soinside.com 2019 - 2024. All rights reserved.