Splunk Search

calculate average of last 30 days

thomaap
New Member

below average function is not giving me the correct value for last 30 days.Kindly advise

| eval sTime=strptime(startTime,"%a %B %d %Y %H:%M:%S")
| eval eTime=strptime(endTime,"%a %B %d %Y %H:%M:%S")
| eval tTime=strptime(startTime,"%a %B %d %Y %H:%M:%S")
| eventstats latest(STATUS) AS STATUS BY JOB
| transaction JOB,startTime,endTime
| eval e_Time=if(STATUS="TERMINATED" OR eTime

0 Karma

richgalloway
SplunkTrust
SplunkTrust

The streamstats avg(diff) as average window=30 command will calculate the average diff over the previous 30 events, not necessarily 30 days. Try this, instead:

<compute diff>
| bucket span=30d _time
| stats avg(diff) as average by _time
| eval avrg = round(average, 2)
---
If this reply helps you, Karma would be appreciated.
0 Karma

thomaap
New Member

i tried updating to be above code.however it does not seem to give me the correct value .The existing values are not returning after the change .

index=MY_INDEX JOB=JOBNAME earliest= -30d@d latest= now()
|dedup JOB,STATUS
| eval startTime= case("0"!=(strftime(_time, "%a %B %d %Y %H:%M:%S")) AND STATUS="RUNNING",strftime(_time, "%a %B %d %Y %H:%M:%S")),endTime= case("0"!=(strftime(_time, "%a %B %d %Y %H:%M:%S")) AND STATUS="SUCCESS",strftime(_time, "%a %B %d %Y %H:%M:%S")), terminateTime= case("0"!=(strftime(_time, "%a %B %d %Y %H:%M:%S")) AND STATUS="TERMINATED",strftime(_time, "%a %B %d %Y %H:%M:%S"))
| eval sTime=strptime(startTime,"%a %B %d %Y %H:%M:%S")
| eval eTime=strptime(endTime,"%a %B %d %Y %H:%M:%S")
| eval tTime=strptime(startTime,"%a %B %d %Y %H:%M:%S")
| eventstats latest(STATUS) AS STATUS BY JOB
| transaction JOB,startTime,endTime
| eval e_Time=if(STATUS="TERMINATED" OR eTime

0 Karma

richgalloway
SplunkTrust
SplunkTrust

That's the nature of stats, which seemed fine since your question asked about calculating average and said nothing about preserving other values.

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

richgalloway
SplunkTrust
SplunkTrust

I see no attempt to calculate an average in that SPL. What field do you want to average?

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

thomaap
New Member

| eval stTime=strptime(startTime, "%a %B %d %Y %H:%M:%S")
| eval edTime=strptime(e_Time, "%a %B %d %Y %H:%M:%S")
| eval diff=edTime-stTime
| eval diff= round(diff/60,2)
| eval diff=edTime-stTime
| eval diff= round(diff/60,2)
| streamstats avg(diff) as average window=30
| eval avrg=round(average,2)
looks like the entire query was not posted

0 Karma
Get Updates on the Splunk Community!

Splunk Classroom Chronicles: Training Tales and Testimonials (Episode 2)

Welcome to the "Splunk Classroom Chronicles" series, created to help curious, career-minded learners get ...

Index This | I am a number but I am countless. What am I?

January 2025 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  Happy New Year! We’re ...

What’s New in Splunk Enterprise 9.4: Tools for Digital Resilience

PLATFORM TECH TALKS What’s New in Splunk Enterprise 9.4: Tools for Digital Resilience Thursday, February 27, ...