Splunk Enterprise

Trying to sort month in chronological order and not getting the desired output?

chandankr
Path Finder

was using this below Search, 

***| rex field=_raw "<measResults>\d+\s\d+\s\d+\s\d+\s\d+\s\d+\s\d+\s\d+\s(?<active_state>\d{0,3})\s\d+\s(?<idle_state>\d{0,3})"
| eval date_month=upper(date_month)
| eventstats avg(active_state) as Active_UEs avg(idle_state) as Idle_UEs by date_month
| eval Active_UEs=round(Active_UEs,0), Idle_UEs=round(Idle_UEs,0)
| stats count by date_month,Active_UEs,Idle_UEs
| table date_month,Active_UEs,Idle_UEs

chandankr_1-1673354333310.png

but now  i was trying to sort month in chronological order i used the below Search

***| eventstats avg(active_state) as Active_UEs avg(idle_state) as Idle_UEs by date_month
| eval Active_UEs=round(Active_UEs,0), Idle_UEs=round(Idle_UEs,0)
| eval Month=date_month
| eval orden = if(Month="january",1,if(Month="february",2,if(Month="march",3,if(Month="april",4,if(Month="may",5,if(Month="june",6,if(Month="july",7,if(Month="august",8,if(Month="september",9,if(Month="october",10,if(Month="november",11,12)))))))))))
| sort num(Month)
| stats count by Month,Active_UEs,Idle_UEs
| table Month,Active_UEs,Idle_UEs

chandankr_0-1673354157862.png

here also month are sorted in alphabetically order not in chronilogical order.

 

 

 

 

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

chandankr
Path Finder

Finally got this done using below

 

| eval month=strftime(_time,"%m")
| eval month_name=date_month
| eval monthyear=date_year
| stats count by month,month_name,monthyear,AvgCPU,MaxCPU
| sort num(monthyear), num(month)
| table month_name,AvgCPU,MaxCPU

View solution in original post

Manasa_401
Communicator

Hello @chandankr 

The efficient way is to create a new field to get the month number from _time field as below

|strftime month_num=strftime(_time,"%m")

| stats count by Month,month_num,Active_UEs,Idle_UEs

|sort month_num

If this helps, karma would be appreciated.

Thanks,

Manasa

0 Karma

chandankr
Path Finder

this is not working in sort month in chronical order

 

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Why go through the bother of converting Month into a number and then not use it?  Sorting by orden would solve the problem, except the stats command re-sorts the data.  Try this query.

***| eventstats avg(active_state) as Active_UEs avg(idle_state) as Idle_UEs by date_month
| eval Active_UEs=round(Active_UEs,0), Idle_UEs=round(Idle_UEs,0)
| eval Month=date_month
| eval orden = case(Month="january",1, Month="february",2, Month="march",3, Month="april",4, Month="may",5, Month="june",6, Month="july",7, Month="august",8, Month="september",9, Month="october",10, Month="november",11, 1==1, 12)
| stats count, first(Month) by orden,Active_UEs,Idle_UEs
| table Month,Active_UEs,Idle_UEs

 BTW, the num function will not convert text ("january") into digits (1).

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

chandankr
Path Finder

@richgalloway  also i was going through eval case and show this 

 

 eval error_msg = case(error == 404, "Not found", error == 500, "Internal Server Error", error == 200, "OK")

eval command examples - Splunk Documentation

so i tried the below Queary 

| eval date_month=upper(date_month)
| eventstats avg(active_state) as Active_UEs avg(idle_state) as Idle_UEs by date_month
| eval Active_UEs=round(Active_UEs,0), Idle_UEs=round(Idle_UEs,0)
| eval Month=date_month
| eval month_seq=case( Month == january, "1", Month == february, "2", Month == march, "3", Month == april, "4" , Month == may, "5" , Month == june, "6" ,
Month == july, "7" , Month == august, "8" , Month == september, "9" , Month == october, "10" , Month == november, "11" , Month == december, "12" )
| stats count, first(Month) by month_seq,Active_UEs,Idle_UEs
| table Month,Active_UEs,Idle_UEs

 

 

chandankr_0-1673368515451.png

 

 

0 Karma

richgalloway
SplunkTrust
SplunkTrust

In this query, the case statement is trying to compare the Month field to fields january, february, etc. rather than strings "january", "february", etc.  As a result, none of the cases match so month_seq is set to NULL.  When stats is told to group by a null field it always returns no results.

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

chandankr
Path Finder

got this 

0 Karma

chandankr
Path Finder

@richgalloway  tried that but no luck. Below is the screenshot for the same 

 

chandankr_0-1673366945820.png

 

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Oops!  I left out an as clause in the stats command.  Try this revision.

***| eventstats avg(active_state) as Active_UEs avg(idle_state) as Idle_UEs by date_month
| eval Active_UEs=round(Active_UEs,0), Idle_UEs=round(Idle_UEs,0)
| eval Month=date_month
| eval orden = case(Month="january",1, Month="february",2, Month="march",3, Month="april",4, Month="may",5, Month="june",6, Month="july",7, Month="august",8, Month="september",9, Month="october",10, Month="november",11, 1==1, 12)
| stats count, first(Month) as Month by orden,Active_UEs,Idle_UEs
| table Month,Active_UEs,Idle_UEs

 

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

chandankr
Path Finder

chandankr_0-1673368749739.png

still no luck

0 Karma

bowesmana
SplunkTrust
SplunkTrust

I am not sure why you are doing eventstats as well as stats. Just use bin _time to group by a monthly time span and then use _time in the stats. As stats sorts automatically it will sort by time and then you can just format time as the Month, so get rid of all the stuff from eventstats up to stats and replace with

| bin _time span=1mon
| stats avg(active_state) as Active_UEs avg(idle_state) as Idle_UEs by _time 
| eval Month=strftime(_time, "%B")
| table Month,Active_UEs,Idle_UEs
| eval Active_UEs=round(Active_UEs,0), Idle_UEs=round(Idle_UEs,0)

 

0 Karma

Manasa_401
Communicator

Hello @chandankr 

Try using sort after stats.

|sort orden

Thanks

Manasa

0 Karma

chandankr
Path Finder

Finally got this done using below

 

| eval month=strftime(_time,"%m")
| eval month_name=date_month
| eval monthyear=date_year
| stats count by month,month_name,monthyear,AvgCPU,MaxCPU
| sort num(monthyear), num(month)
| table month_name,AvgCPU,MaxCPU

Get Updates on the Splunk Community!

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!

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

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...