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!

Join Us for Splunk University and Get Your Bootcamp Game On!

If you know, you know! Splunk University is the vibe this summer so register today for bootcamps galore ...

.conf24 | Learning Tracks for Security, Observability, Platform, and Developers!

.conf24 is taking place at The Venetian in Las Vegas from June 11 - 14. Continue reading to learn about the ...

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...