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!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...