Splunk Search

How To Get First and Last Events within a Set of Events

Traer001
Path Finder

Hi, I'm having trouble grabbing the first event of a specific type and the last consecutive event after that with the same type. For instance, my events for a user might look like this:

2021-04-02 14:45:11  User: 1 Network Error Happens

2021-04-02 14:42:57  User: 1 Error Recover

2021-04-02 14:41:33  User: 1 error id: 02 Happens at Location 1

2021-04-02 14:41:21  User: 1 error id: 02 Happens at Location 1

2021-04-02 14:41:12  User: 1 Error Happens 

2021-04-02 14:40:52  User: 1 Software Error Recover

2021-04-02 14:40:24  User: 1 Software Error Happens

 

In this case, I am trying to grab the 2nd and 5th events for where the latest recovered event happens and recovers so that I can later calculate the duration of the error. Currently, my query looks like this:

 

index=INDEX host=HOSTNAME sourcetype=SOURCETYPE
| rex field=_raw "User:\s(?<user_id>\d+)"
| rex field=_raw "(?<error_type>\w+)\sError"
| rex field=_raw "(?<error_type>\w+)\serror\sid:\s(?<error_id>\d+)"
| rex field=_raw "\d+\s(?<error_type>\w+)\sRecover"
| rex field=_raw "\d+\s(?<error_type>\w+)\sHappen"
| eval action=if((like(_raw, "%Happen%")), "Happen", (if(like(_raw, "%Recover%"), "Recover", null)))
| where isnotnull(action)
| eval latest_recover=if(action="Recover", _time, null)
| streamstats latest(error_type) as latest_error_type latest(latest_recover) as _time earliest(_time) as early_time values(action) as actions by user_id
| where mvcount(actions)=2
| stats latest(error_type) as last_error_type latest(_time) as recovered latest(early_time) as happened by user_id
| eval error_duration=tostring((recovered - happened), "duration")
| eval happened_time=strftime(happened, "%Y-%m-%d %H:%M:%S")
| eval recovered_time=strftime(recovered, "%Y-%m-%d %H:%M:%S")
| fields - recovered, happened

 

However, this query does not appear to be retrieving the appropriate events. Is there something I can do to get the two events (or the range of events --like the 2nd through the 5th events)?

Labels (4)
0 Karma
1 Solution

scelikok
SplunkTrust
SplunkTrust

Hi @Traer001,

You can try with transaction command;

index=INDEX host=HOSTNAME sourcetype=SOURCETYPE
| rex field=_raw "User:\s(?<user_id>\d+)"
| rex field=_raw "(?<error_type>\w+)\sError"
| rex field=_raw "(?<error_type>\w+)\serror\sid:\s(?<error_id>\d+)"
| sort - _time
| transation user_id startswith="Happens" endswith="Recover"
| table _time duration user_id error_type error_id 

 

If this reply helps you an upvote and "Accept as Solution" is appreciated.

View solution in original post

scelikok
SplunkTrust
SplunkTrust

Hi @Traer001,

You can try with transaction command;

index=INDEX host=HOSTNAME sourcetype=SOURCETYPE
| rex field=_raw "User:\s(?<user_id>\d+)"
| rex field=_raw "(?<error_type>\w+)\sError"
| rex field=_raw "(?<error_type>\w+)\serror\sid:\s(?<error_id>\d+)"
| sort - _time
| transation user_id startswith="Happens" endswith="Recover"
| table _time duration user_id error_type error_id 

 

If this reply helps you an upvote and "Accept as Solution" is appreciated.
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...