Splunk:查找没有特定属性记录为不同日志行的事件

问题描述

我们有 Splunk 日志,例如:

ts=20:10:01 id=1 state=first foo=bar
ts=20:10:05 id=1 state=second foo=bar
ts=20:10:06 id=1 state=third foo=bar

ts=20:10:03 id=2 state=first foo=bar

ts=20:11:01 id=3 state=first foo=bar
ts=20:11:03 id=3 state=second foo=bar
ts=20:11:05 id=3 state=third foo=bar

我想找到所有没有其他 2 个状态的 id在这个例子中,所有 id=2 都记录了第一个状态,而不是其他 2。我正在阅读 JOIN 并发现它只能查看同时发生的事件,但我们不能排除这些事件。

index=my-idx foo=bar 
| join id type=outer
  [search index=my-idx foo=bar NOT (state=second OR state=third) | table id]
| table id

我想到的查询应该返回一个没有 state=secondstate=third 的 id 列表,在上面的例子中应该返回 id=2

解决方法

这是一个可以随时随地运行的示例查询。查询中的注释解释了它的作用。它假设任何 id 的第一个状态总是“第一”。

| makeresults 
| eval data="ts=20:10:01 id=1 state=first foo=bar;
ts=20:10:05 id=1 state=second foo=bar;
ts=20:10:06 id=1 state=third foo=bar;
ts=20:10:03 id=2 state=first foo=bar;
ts=20:11:01 id=3 state=first foo=bar;
ts=20:11:03 id=3 state=second foo=bar;
ts=20:11:05 id=3 state=third foo=bar"
| eval data=split(data,";")
| mvexpand data
| eval _raw=data
| extract kvdelim=" ",pairdelim="="
| fields ts,id,state,foo
```Above just sets up test data```
```Count how many different states each id has```
| streamstats dc(state) as count by id
```Find the highest count for each id```
| eventstats max(count) as max by id
```Select only those with a single state```
| where max=1
| table ts id state foo
,
stats values(state) as all_states dc(state) as state_count by id
| search all_states="first" AND state_count=1
| table id