Splunk Search

How to convert my lookup field value to an executable formula when i use "foreach"?

Muller
Explorer

Hi, i'm using Splunk since two month and i love it. But i need help.

I have a lot of sensors, sampling per minute. I have a lookup where I can fill in formulas in text format like 'if (sensor1> 200,0, sensor1 / 10)'. Over the period concerned, I want to apply the formula for each sensor every minute.

Here is an expample with four sensors  (row* are the values) :

id_sensorrow 1row 2row 3row 4formula
ID177.225077.579.4=if(value<200,value/10,false)
ID2227.29227.18226.59227.1=value/10
ID334.134.835.936.1=(value*9/5)+32

 

And the results needed

id_sensorrow 1row 2row 3row 4
ID17.7207.757.94
ID222.72922.71822.65922.71
ID393.3894.6496.6296.98

 

I can have a lot of rows and colums ... 
I saw quite a few "close" answers on this forum, but none that I managed to apply.
In particular, I wish to avoid going through a subsearch which would limit the number of exploitable results.

Thank you very much in advance.

Labels (3)
0 Karma
1 Solution

to4kawa
Ultra Champion
| tstats count where index=_internal (earliest=-2d@d latest=@d) by sourcetype
| streamstats count as Id
| fillnull col1 col2 col3 col4
| foreach col* [ eval <<FIELD>> = count * (random() % 100 + 1) * <<MATCHSTR>>]
| table Id col*
| rex "(?<comment>(?# this is sample data, from here, foreach and case sample logic.))"
| foreach col* [ eval <<FIELD>> = case(Id=1,if(<<FIELD>> < 200,<<FIELD>>/10,<<FIELD>>),
Id=2,<<FIELD>> / 10,
Id=3,(<<FIELD>> * 9 / 5 ) + 32,
true(), <<FIELD>> )]

Here's how it goes.

View solution in original post

to4kawa
Ultra Champion

How many different formulas are there?
If it's about 10 or so, it's easier to identify it by CASE.

0 Karma

Muller
Explorer

Hi To4kawa, and thank you 🙂

The number of formulas will grow over time.
We measure a large number of different equipment and different brands. We must therefore each time imagine the conversion factors necessary to standardize our data.

I will even watch how "Case" works

0 Karma

to4kawa
Ultra Champion
| tstats count where index=_internal (earliest=-2d@d latest=@d) by sourcetype
| streamstats count as Id
| fillnull col1 col2 col3 col4
| foreach col* [ eval <<FIELD>> = count * (random() % 100 + 1) * <<MATCHSTR>>]
| table Id col*
| rex "(?<comment>(?# this is sample data, from here, foreach and case sample logic.))"
| foreach col* [ eval <<FIELD>> = case(Id=1,if(<<FIELD>> < 200,<<FIELD>>/10,<<FIELD>>),
Id=2,<<FIELD>> / 10,
Id=3,(<<FIELD>> * 9 / 5 ) + 32,
true(), <<FIELD>> )]

Here's how it goes.

Muller
Explorer

What I did with "case".

In my lookup, I created a "case" field and several other fields with different values.
"target" "coef1" "coef2" "coef3" "correction"

For case "0":   <<FIELD>> = '<<FIELD>>'
For case "1":   <<FIELD>> = ('<<FIELD>>' * coef1 + coef2) * coef3
For case ">":      if ('<<FIELD>>'> target, ('<<FIELD>>' * coef1 + coef2) * coef3, correction)
For case "<":      if ('<<FIELD>>' <target, ('<<FIELD>>' * coef1 + coef2) * coef3, correction)
...
...
And it does what I want :).

0 Karma
Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...