Splunk Search

Is it possible to do a conditional count using tstats?

cramasta
Builder

Is it possible to do a conditional count using tstats? I'm trying use the following which is the syntax that I would use for a non tstats search, but it doesn't work. count(eval("Performance.duration" > 2500)) AS violation

Complete search:

| tstats  count(eval("Performance.duration" > 2500)) AS violation exactperc99("Performance.duration") AS "perc99"  from datamodel=PerformanceMetrics where (nodename = PerformanceMetricBaseSearch)  (PerformanceMetricBaseSearch.duration="*")   groupby PerformanceMetric _time span=15m 

Kenshiro70
Path Finder

Here's how I did it without changing the data model.

My goal was to see what percentage of API calls returned in under 1 second and under 3 seconds.

| tstats summariesonly=t prestats=t count from datamodel=rest_api 
       BY object.apiName
| eval perfThreshold="Total"
| tstats summariesonly=t prestats=t append=t count from datamodel=rest_api 
       WHERE object.responseTime<=1000
       BY object.apiName
| eval perfThreshold=case(isnotnull(perfThreshold), perfThreshold, true(), "Below1sec")
| tstats summariesonly=t prestats=t append=t count from datamodel=rest_api 
       WHERE object.responseTime<=3000
       BY object.apiName
| eval perfThreshold=case(isnotnull(perfThreshold), perfThreshold, true(), "Below3sec")
| rename object.* as *
| chart count by apiName perfThreshold
| eval pctBelow1Sec=100 * Below1sec / Total
| eval pctBelow3Sec=100 * Below3sec / Total
| table apiName pctBelow1Sec pctBelow3Sec Total

Hope this helps.

0 Karma

cramasta
Builder

I have been able to do this with adding another tstats string using append=true, however curious if it can be done without having to do that

0 Karma

pwmcintyre
Explorer

@cramasta - do you think you could post an example?

0 Karma

sundareshr
Legend

One way you could try is add a new field to your datamodel. eval newfield=if("Performance.duration" > 2500, 1, 0) and in your search do | tstats sum(newfield) as violation

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!

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...

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 ...