Splunk Enterprise

help on time sorting

jip31
Motivator

hello

I dont succeed to sort the events by time

the format time field is for example :   1632218561

what is wrong please?

 

 

index="tutu" sourcetype="toto"  
| search statustext=TimedOut 
| sort - time 
| eval time = strftime(_time, "%d-%m-%y %H:%M") 
| stats last(time) as Heure, last(statustext) as statustext by desktop 

 

 

 

Labels (1)
Tags (1)
0 Karma
1 Solution

danielcj
Communicator

You can try the following @jip31 :

<YOUR_SEARCH>
| sort - _time 
| eval Heure=strftime(_time, "%d-%m-%y %H:%M") 
| stats last(Heure) as Heure

View solution in original post

0 Karma

isoutamo
SplunkTrust
SplunkTrust

Hi

probably you have too many events for sort without limits?

If the first argument to the sort command is a number, then at most that many results are returned, in order. If no number is specified, the default limit of 10000 is used. If the number 0 is specified, all of the results are returned. See the count argument for more information.

Can you try sort 0 - time instead of sort - time ?

r. Ismo

0 Karma

bowesmana
SplunkTrust
SplunkTrust

@jip31 

OK, lots of posts here, so hopefully you're not too confused...

You are trying to

 

index="tutu" sourcetype="toto"  
| search statustext=TimedOut 
| sort - time 
| eval time = strftime(_time, "%d-%m-%y %H:%M") 
| stats last(time) as Heure, last(statustext) as statustext by desktop 

 

  • Sort time, so that the LATEST time is at the top
  • Find the LAST time from the list, i.e. the EARLIEST time

Sorting is something to avoid, as it's expensive - and not necessary here, sorting should always be done as LATE as possible

All you therefore need is

 

index="tutu" sourcetype="toto" statustext=TimedOut 
| stats earliest(_time) as Heure, earliest(statustext) as statustext by desktop 
| eval time = strftime(_time, "%d-%m-%y %H:%M") 

 

Note that you do not need a separate search command for statustext, it can be combined with the first line.

At the end, you will have 4 fields

_time - the epoch time
time - your formatted time
statustext + desktop

If you then want to sort, you can sort by any of these fields

Hope this helps

 

0 Karma

jip31
Motivator

hi 

it doenst works too

0 Karma

richgalloway
SplunkTrust
SplunkTrust

The field to be sorted must exist before sorting.  IOW, put the eval command before the sort command or sort the _time field (which always exists).

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

PickleRick
SplunkTrust
SplunkTrust

Also remember that if you do strftime and convert your _time to some string representation, it will be sorted alphabeticaly, not date-wise!

So you'd better use _time.

0 Karma

jip31
Motivator

sorry but same also with _time....

| eval _time = strftime(_time, "%d-%m-%y %H:%M") 
| sort - _time 
| stats last(_time) as Heure
0 Karma

PickleRick
SplunkTrust
SplunkTrust

No, no, no. Leave _time alone (don't overwrite it, especially, not with string values). You want to have your original _time to sort by it.

So you want to

 

| sort - _time

 

As first step of your pipeline.

Besides, instead of sorting and chosing last value, you can use

 

| stats earliest(_raw)

Or even, in your case, since you're just interested in time

| stats earliest_time(_raw)

 

0 Karma

danielcj
Communicator

You can try the following @jip31 :

<YOUR_SEARCH>
| sort - _time 
| eval Heure=strftime(_time, "%d-%m-%y %H:%M") 
| stats last(Heure) as Heure
0 Karma

jip31
Motivator

sorry but it doesnt works

0 Karma
Get Updates on the Splunk Community!

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...

Tech Talk | Elevating Digital Service Excellence: The Synergy of Splunk RUM & APM

Elevating Digital Service Excellence: The Synergy of Real User Monitoring and Application Performance ...