如何使用if条件将统计信息计数到日志上的特定值

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

我是Splunk的新手,我正在尝试进行查询以计算有多少请求具有确定值,但是如果请求上有特定属性,则必须增加此计数器。

示例:

2020-01-09 13:51:28,802 INFO  [http-nio-8080-exec-8] class:ControllerV1, UA=[tokyo], GW=[api-gateway-id]
2020-01-09 13:51:31,865 INFO  [http-nio-8080-exec-9] class:ControllerV1, UA=[tokyo], GW=[api-gateway-id]
2020-01-09 13:51:32,922 INFO  [http-nio-8080-exec-10] class:ControllerV1, UA=[tokyo], GW=[api-gateway-id]
2020-01-09 13:51:36,939 INFO  [http-nio-8080-exec-2] class:ControllerV1, UA=[tokyo], GW=null
2020-01-09 13:51:48,614 INFO  [http-nio-8080-exec-1] class:ControllerV1, UA=[new-york], GW=[api-gateway-id]
2020-01-09 13:51:49,266 INFO  [http-nio-8080-exec-3] class:ControllerV1, UA=[new-york], GW=[api-gateway-id]
2020-01-09 13:51:57,533 INFO  [http-nio-8080-exec-4] class:ControllerV1, UA=[helsing], GW=[api-gateway-id]

对于上面的示例,如果GW != null,我必须增加计数器,所以我有3个计数器,分别是tokyo纽约helsing。结果应该类似于:

tokyo | new-york | helsing
  3   |    2     |    1 

尝试:

source="/logfiles.log" | rex "UA=(?<user-agent>\w+)" | stats count(eval(user-agent="[tokyo]")) as TOKYO

但是返回错误:Error in 'rex' command: Encountered the following error while compiling the regex 'UA=(?<user-agent>\w+)': Regex: syntax error in subpattern name (missing terminator).

我知道不能使用-,但是我必须这样做,并且当我删除它时,结果保持为空(0个结果)。

splunk splunk-query
1个回答
0
投票

您可以在基础搜索中简单地添加NOT "GW=null",如果正在评估字段GW,则可以添加GW!=null

这是我所见过的regex语法(如果在某些字段中对消息进行了评估,则使用字段名,或使用raw),还将hiphen(-)更改为underscore(),将变量名更改为'-'不被接受。

source="/logfiles.log" 
| rex field=_raw "UA=(?<user_agent>\w+)" 
| stats count(eval(user-agent="[tokyo]")) as TOKYO

但是在上述正则表达式中,您也需要相当大的方括号'['所以下面的正则表达式应该工作

 source="/logfiles.log" 
| rex field=_raw "UA=(?<user_agent>\[\w+\])" 
| stats count(eval(user-agent="[tokyo]")) as TOKYO
© www.soinside.com 2019 - 2024. All rights reserved.