Splunk Search

Search for when "Event X" happens within x hours of "Event Y"

bravon
Communicator

I have this search:

("WARNING: ERROR Message" host=SERVER1) OR (EventCode=1074 Shutdown_Type="*")

This shows both the "Error Message" events and the "EventCode=1074 Shutdown_Type="*"" events.

However - I am interrested in only showing the "EventCode=1074 Shutdown_Type="*" events that occur within X hours of the "Error Message" events.*
My results should contain all the ("WARNING: ERROR Message" host=SERVER1) events and only the (EventCode=1074 Shutdown_Type="*") that occur within X hours of the ("WARNING: ERROR Message" host=SERVER1) events.

Can someone point me in the right direction? Thanks in advance.

Tags (2)
0 Karma
1 Solution

gcato
Contributor

Hi bravon,

The map command will work, though there may be more efficient ways to do it that avoids a subsearch. As an example, this will match completed search commands for 5 mins after a Splunk login.

index=_audit action="login attempt" | eval first=_time | eval last=relative_time(_time, "+5mins") 
| map search="search index=_audit action=\"login attempt\" OR (action=search info=completed) earliest=$first$ latest=$last$"|sort - _time 

Note: the map command needs action=\"login attempt\" (escape quotes, if used/needed), otherwise only the action=search... results would be returned, which would not meet your requirements.

Hope this helps.

View solution in original post

gcato
Contributor

Hi bravon,

The map command will work, though there may be more efficient ways to do it that avoids a subsearch. As an example, this will match completed search commands for 5 mins after a Splunk login.

index=_audit action="login attempt" | eval first=_time | eval last=relative_time(_time, "+5mins") 
| map search="search index=_audit action=\"login attempt\" OR (action=search info=completed) earliest=$first$ latest=$last$"|sort - _time 

Note: the map command needs action=\"login attempt\" (escape quotes, if used/needed), otherwise only the action=search... results would be returned, which would not meet your requirements.

Hope this helps.

gcato
Contributor

Hi bravon,

This search may prove more efficient, as it does not use a sub-search like map.

index=_audit action="login attempt" OR (action=search info=completed) 
 | eval matchtime = if(match(action,"login attempt"), _time, null) 
 | eval extendtime = if(isnotnull(matchtime), relative_time(matchtime,"+300secs"), null)
 | reverse | streamstats last(matchtime) as matched, last(extendtime) as lasttime 
 | where _time >= matched AND _time <= lasttime | reverse

The reverse is necessary for the streamstats command to work correctly, as it needs to work from earliest to latest, in this case. On a larger dataset the reverse may prove less efficient than the map command.

Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...