Splunk Search

How to join two consecutive events and their _time fields?

PrisonMike
Explorer

 

I have a log file with events that indicate activities in a server. I am interested in the Login and Logout activities - I need to create a report of active sessions.

I managed to order the events so that I can get Login-Logout events consecutively for each user.

 

 

Labels (2)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Assuming your events are already sorted in chronological order (earliest first), try something like this

| streamstats global=f window=1 current=f latest(event) as previous_event latest(time) as previous_time by user
| reverse
| streamstats global=f count(eval(previous_event="Login")) as occurrence by user
| where event="Logout" and previous_event="Login"

View solution in original post

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Assuming your events are already sorted in chronological order (earliest first), try something like this

| streamstats global=f window=1 current=f latest(event) as previous_event latest(time) as previous_time by user
| reverse
| streamstats global=f count(eval(previous_event="Login")) as occurrence by user
| where event="Logout" and previous_event="Login"
0 Karma

PrisonMike
Explorer

I don't really understand what we need the occurrence for?

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Strictly speaking you don't if you just want the times - I have used occurrence in some of my solutions to "tag" the correlated events

0 Karma

PrisonMike
Explorer

Got it

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

_time is a bunch of numbers too - it is just auto-formatted when you display it! If you want to display latest(_time) as a datetime string, use either fieldformat or eval with the strftime() function

0 Karma

PrisonMike
Explorer

Okay the solution works perfectly except for the part where i have to include even "half-sessions" where i only have the login or logout event within my time frame. In such case it would be something like 

 

      Login Time       |   User  |     Logout Time    

             null               | user1 |   yy-mm-dd h:m:s

yy-mm-dd h:m:s   | user2 |          null

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| eval start=if(event="Login",time,null())
| eval end=if(event="Logout",time,null())
| streamstats global=f count(eval(event="Login")) as session by user
| stats values(start) as start values(end) as end by user session
0 Karma
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!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...