Splunk Search

How to count how many times a field value has changed for a stream of events over a period of time?

jedatt01
Builder

I want to count the number of times the value of a field called "Node_Group" has changed for a stream of events over a period of time and group it by a field called NetworkDeviceName.

I believe the streamstats command should accomplish this but I'm not confident in how to know when the change occurs. So far I just have | streamstats count(Node_Group) by NetworkDeviceName.

Any ideas?

Tags (3)
0 Karma
1 Solution

Runals
Motivator

I'd try some variation of

... | sort _time NetworkDeviceName | streamstats dc(Node_Group) by NetworkDeviceName

View solution in original post

Runals
Motivator

I'd try some variation of

... | sort _time NetworkDeviceName | streamstats dc(Node_Group) by NetworkDeviceName

jedatt01
Builder

With a slight variation this worked......add window=2. thanks

musskopf
Builder

Yes, I believe streamstats is the way to go. This command basically calculates stats for every event, based on the order the events are being returned in a search on Splunk... here an example, how you could use it:

index=bla "your search" fieldA=* | sort +_time
| streamstats window=1 current=false last(fieldA) AS previous_fieldA
| where fieldA!=last_fieldA | table *

Note that I've used sort to guarantee the events are processed on the right order, from the oldest to the newest. After that I use streamstats with the options window=1 current=false to grab the previous value from fieldA and "copy" to the current event as previous_fieldA. After that just make a search to see the ones which are different, so it'll indicate a change!

Does that any sense for your use case? Let me know if we're getting close to what you're after.

Cheers,

ramdaspr
Contributor

++
And you could replace the ending | table * by |stats count as CountofChanges if you are interested only in the count.

However, I am curious about the

and group it by a field called NetworkDeviceName.

@jedatt01 Are you interested in finding how many times it has changed from/to each Node_Group?

0 Karma

jedatt01
Builder

Yes, trying to count the number of changes in field Node_Group because this implies a failover. Here's what did and it seems to be working correctly.

streamstats window=2 dc(Node_Group) As NG_Count by NetworkDeviceName

The window=2 is what solved it for me

0 Karma
Get Updates on the Splunk Community!

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  🚀 Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Stronger Security with Federated Search for S3, GCP SQL & Australian Threat ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Accelerating Observability as Code with the Splunk AI Assistant

We’ve seen in previous posts what Observability as Code (OaC) is and how it’s now essential for managing ...