Splunk Search

How to select only one event?

disasters
Explorer

My query is this.

 

index=log AND 1378

 

There are two event

 

20230112, 1378, error A/B/C, duration 100

20230112, 1378, error A/B, duration 2

 

I want select only one event that duration greater than another event.

0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Assuming the duration field is already extracted, use eventstats to find the greatest duration then the where command can select the event with that value.

index=log AND 1378
| eventstats max(duration) as maxDuration
| where duration = maxDuration

 

---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

Assuming the duration field is already extracted, use eventstats to find the greatest duration then the where command can select the event with that value.

index=log AND 1378
| eventstats max(duration) as maxDuration
| where duration = maxDuration

 

---
If this reply helps you, Karma would be appreciated.

disasters
Explorer

It works. Thank you!!

 

more question...

 

index=log

 

There are four event

 

1. 20230112, 1378, error A/B/C, duration 100

2. 20230112, 1378, error A/B, duration 2

3. 20230112, 1379, error A/B/D, duration 300

4. 20230112, 1379, error A/B, duration 4

I want select 1,3

0 Karma

richgalloway
SplunkTrust
SplunkTrust

How should Splunk know which events to display?  When does it choose the highest duration and when does it choose the first and third?  Computers need rules to follow.

---
If this reply helps you, Karma would be appreciated.

disasters
Explorer

query 

index=ddos
| rex field=_raw "(?<time>.*),(<alert_num>.*),(<error>.*),(<duration>.*)"

 

event

20230112, 1378, error A/B/C, duration 100

20230112, 1378, error A/B, duration 2

20230112, 1379, error A/B/D, duration 300

20230112, 1379, error A/B, duration 4

 

and then query 

index=ddos
| rex field=_raw "(?<time>.*),(<alert_num>.*),(<error>.*),(<duration>.*)"

| eventstats max(duration) as maxDuration

| where duration=maxDuration

event (only 1)

20230112, 1379, error A/B/D, duration 300

 

 

I want to display two event that different alert_num

20230112, 1378, error A/B/C, duration 100

20230112, 1379, error A/B/D, duration 300

0 Karma

bowesmana
SplunkTrust
SplunkTrust

Your rex statement is wrong and even when fixed, it extracts duration as the string

 duration 300

i.e. the full text, so you should use this rex

| rex field=_raw "(?<time>.*),(?<alert_num>.*),(?<error>.*),\s?duration\s+(?<duration>\d+)"
| eventstats max(duration) as maxDuration by alert_num
| where duration=maxDuration

so your duration field is extracted as a number rather than a string. Then simply add the by alert_num onto your eventstats.

Note that you should still make your regex more robust. Using a greedy .* wildcard selection can easily cause your regex to break. For example as you know your field delimiter is a comma, use

| rex field=_raw "(?<time>[^,]*),(?<alert_num>[^,]),(?<error>[^,]),\s?duration\s+(?<duration>\d+)"

 

gcusello
SplunkTrust
SplunkTrust

Hi @disasters,

I suppose that you already extracted fields, so, please confirm:

you want to extract all the events for each event_id (1378) where there are more than one event and you want the one with the max duration, is it correct?

if this is your need, please try something like this:

index=log 1378
| stats 
   earliest(timestamp) AS timestamp 
   values(error) AS error
   max(duration) AS duration
   count 
   BY event_id
| where count>1

in addition, you don't need to use the AND operator because if you don't use a boolean operator is like AND.

Ciao.

Giuseppe

disasters
Explorer

It works. Thank you.

 

but same problem i have. Please refer to above reply.

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

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

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...