Splunk Search

How to add if clause in a foreach eval [ statement for the following query?

djoobbani
Path Finder

Dear splunk community:

I have the following search query which basically shows the number of counts and percentage of url (Y-Axis) http status code (X-Axis):

<basic search> | chart count by url, http_status_code | addtotals fieldname=total
| foreach 2* 3* 4* 5* [ eval "percent_<<FIELD>>"=round(100*'<<FIELD>>'/total), "<<FIELD>>"='<<FIELD>>'." (".'percent_<<FIELD>>'."%)" ] | fields - percent_* total

Here is a sample of the above query result:

Screen Shot 2022-08-18 at 1.14.23 PM.png

Now, i need to insert an if clause so that if the percentage is either 0 OR 100, then do NOT display the percentage. How would i do that to the above query to get this result?

Thank you very much for your help!

 

Labels (4)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Untested, but perhaps this will do.

| foreach 2* 3* 4* 5* [ 
  eval "percent_<<FIELD>>"=round(100*'<<FIELD>>'/total),
    "<<FIELD>>"=if('<<FIELD>>'=0 OR '<<FIELD>>'=100, '<<FIELD>>','<<FIELD>>'." (".'percent_<<FIELD>>'."%)")
  ] | 
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

Untested, but perhaps this will do.

| foreach 2* 3* 4* 5* [ 
  eval "percent_<<FIELD>>"=round(100*'<<FIELD>>'/total),
    "<<FIELD>>"=if('<<FIELD>>'=0 OR '<<FIELD>>'=100, '<<FIELD>>','<<FIELD>>'." (".'percent_<<FIELD>>'."%)")
  ] | 
---
If this reply helps you, Karma would be appreciated.

djoobbani
Path Finder

Thanks richgalloway for your quick reply.

This worked half-way. So now i don't see any 0% percent anymore, but it still shows 100%.

Thanks!

0 Karma
Get Updates on the Splunk Community!

Now Available: Cisco Talos Threat Intelligence Integrations for Splunk Security Cloud ...

At .conf24, we shared that we were in the process of integrating Cisco Talos threat intelligence into Splunk ...

Preparing your Splunk Environment for OpenSSL3

The Splunk platform will transition to OpenSSL version 3 in a future release. Actions are required to prepare ...

Easily Improve Agent Saturation with the Splunk Add-on for OpenTelemetry Collector

Agent Saturation What and Whys In application performance monitoring, saturation is defined as the total load ...