Splunk Search

Can I divide by zero in splunk?

HattrickNZ
Motivator

I was having trouble evaluating a field and I think it was because I was dividing by zero.

This is my solution.

Hope fully it is self explainatory.

Have I understodd this correctly? Have you got a better one/solution?

| makeresults 
      | eval data = "
 1-Sep    657 34 0;
 2-Sep    434 34 0;
     " 
      | makemv delim=";" data
      | mvexpand data
      | rex field=data "(?<Date>\d+-\w+)\s+(?<kpi1>\d+)\s+(?<kpi2>\d+)\s+(?<kpi3>\d+)"
      | fields + Date kpi1 kpi2 kpi3 | fields - _time 
      | eval kpi4=kpi2/kpi3 | rename comment as "kpi4 column will not show up because kpi3 = zero" 
      | eval kpi5=if(kpi3=0,"",kpi2/kpi3) | rename comment  as "kpi5 column will show up because I handle the kpi3=zero and return blank"  |
Tags (2)
0 Karma

koshyk
Super Champion

Your understanding is correct. But I'm not a big fan of "blank" or null, so may be better to evaluate it to a "NotDefined" or a negative integer to make it easier for outcome searches.
And also good to have fillnull for each fields you extract just in case.

| makeresults 
       | eval data = "
  1-Sep    657 34 0;
  2-Sep    434 34 0;
      " 
       | makemv delim=";" data
       | mvexpand data
       | rex field=data "(?<Date>\d+-\w+)\s+(?<kpi1>\d+)\s+(?<kpi2>\d+)\s+(?<kpi3>\d+)"
       | fields + Date kpi1 kpi2 kpi3 | fields - _time 
       | eval kpi4=kpi2/kpi3 | rename comment as "kpi4 column will not show up because kpi3 = zero" 
       | eval kpi5=if(kpi3=0,"NotDefined",kpi2/kpi3) | rename comment  as "kpi5 column will show up because I handle the kpi3=zero and return NotDefined"
0 Karma

DavidHourani
Super Champion

Looks good to me 🙂

0 Karma

goodair
New Member

You have understood it correctly, if the eval fails, it will return null for that evaluation. If all the evals return null for a field, then that field doesn't exist. Your idea for KPI5 is a good way of handling it.

This docs page explains eval, and under the General heading it confirms that division by zero results in a null value:
https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Eval#General

0 Karma
Get Updates on the Splunk Community!

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

April 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...