Splunk Search

how to get the event time and _time difference in alert triggering time delay

harishsplunk7
Explorer

how to show the how long alert took triggered from the time the event occurred. 
To calculate the "diff" in times, to subtract either (_time - event_time) or, if event_time is null, (_time - orig_time), and then calculate the average time it took for each rule to fire, over time. 

i have tried to calculate the diff but event_time and orig_time is present in same event and some doest have.  Please help me to identify the difference in event time and alert triggering time delay. 

index=notable
| eval diff = _time - event_time
| convert ctime(diff), ctime(orig_time)
| table event_time orig_time _time diff search_name

 

Labels (6)
0 Karma

PickleRick
SplunkTrust
SplunkTrust

Please don't duplicate threads. You already asked about the "lag" in another thread.

0 Karma

dtburrows3
Builder

Since it sounds like event_time is preferred over orig_time and it is possible for them to exist in the same event then I would suggest using a coalesce() function. The inputs in that function go from highest precedence on the leftmost side and each entry after is the next step lower precedence. 
So the first non-null field from left to right is what will be used.

And to find avg diff over time for each rule can probably be done with a simple timechart.

I don't have access to ES or a notable index at the moment so I will just use fields described in your original question in the example. 


Example:

 

index=notable
    | eval
        event_time_standardized=coalesce(event_time, orig_time),
        diff_seconds='_time'-'event_time_standardized',
        diff_minutes='diff_seconds'/60
    | timechart span=1h
        avg(diff_seconds) as avg_diff_in_seconds,
        avg(diff_minutes) as avg_diff_in_minutes
            by search_name

 

 

harishsplunk7
Explorer

Hi,

thank you for the update, i have the above query but getting the result for few  events not all. please see the attached screenshot. 

0 Karma

dtburrows3
Builder

From the looks of the screenshot it appears that event_time probably isn't in epoch format so the diff isn't being properly evaluated. 

How does it look when you try this?

index=notable
    | eval
        event_epoch=if(
            NOT isnum(event_time),
                strptime(event_time, "%m/%d/%Y %H:%M:%S"),
                'event_time'
            ),
        orig_epoch=if(
            NOT isnum(orig_time),
                strptime(orig_time, "%m/%d/%Y %H:%M:%S"),
                'orig_time'
            )
    | eval
        event_epoch_standardized=coalesce(event_epoch, orig_epoch),
        diff_seconds='_time'-'event_epoch_standardized',
        diff=tostring(diff_seconds, "duration")
    | table _time, search_name, event_time, diff

 

0 Karma

harishsplunk7
Explorer

Thank you for your kind response, I am getting 10 detections if there are10 rows in the result But the average time to detect should be an average of all the time differences from 1 alert mean time.  Please find the attached screenshot for more information. 
Splunk alert splunk_attack_1 triggered 2 times, i want to take the avg of time and display only one result with difference. 

Sample result 

_timesearch_name   event timeHour at Source Mean Time to Detect
2/5/202419:47:10      Splunk_Attack_12/5/202417:47:10      2Hr3Min19Secs.000000 
2/5/202419:20:10      Splunk_Attack_12/5/202417:20:10      2Hr7Min18Secs.000000 
2/5/202419:30:35      Splunk_Attack_22/5/202418:30:35      1Hr37Min12Secs.000000 
2/5/202418:20:15      Splunk_Attack_22/5/202418:20:15      1Hr26Min15Secs.000000 
2/6/202418:05:15      Splunk_Attack_22/6/202418:05:15      1Hr26Min15Secs.000000 
2/7/202416:55:15      Splunk_Attack_32/7/202414:55:15      2Hr0Min18Secs.000000 
2/8/202416:35:15      Splunk_Attack_32/8/202414:35:15      2Hr20Min18Secs.000000 
2/9/202416:10:15      Splunk_Attack_32/9/202414:10:15      2Hr40Min18Secs.000000 

 Expected Result 

_timesearch_name   event timeHour at Source Mean Time to Detect
2/5/202419:47:10      Splunk_Attack_12/5/202417:47:10      2Hr3Min19Secs.000000 
2/5/202419:20:10      Splunk_Attack_22/5/202417:20:10      2Hr7Min18Secs.000000 
2/5/202419:30:35      Splunk_Attack_32/5/202418:30:35      1Hr37Min12Secs.000000 

 

0 Karma

dtburrows3
Builder

You would utilize the stats command to find an average of the  diff_seconds field using a by-field of search_name.

Something like this (following the search I shared before)

index=notable
    | eval
        event_epoch=if(
            NOT isnum(event_time),
                strptime(event_time, "%m/%d/%Y %H:%M:%S"),
                'event_time'
            ),
        orig_epoch=if(
            NOT isnum(orig_time),
                strptime(orig_time, "%m/%d/%Y %H:%M:%S"),
                'orig_time'
            )
    | eval
        event_epoch_standardized=coalesce(event_epoch, orig_epoch),
        diff_seconds='_time'-'event_epoch_standardized'
    | fields + _time, search_name, event_time, diff_seconds
    | stats
        count as sample_size,
        min(diff_seconds) as min_diff_seconds,
        max(diff_seconds) as max_diff_seconds,
        avg(diff_seconds) as avg_diff_seconds
            by search_name
    | eval
        avg_diff=tostring(avg_diff_seconds, "duration")

 

0 Karma
Get Updates on the Splunk Community!

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

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

Combine Multiline Logs into a Single Event with SOCK: a Step-by-Step Guide for ...

Combine multiline logs into a single event with SOCK - a step-by-step guide for newbies Olga Malita The ...