<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Performing basic calculations, eval statement and wildcard in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Performing-basic-calculations-eval-statement-and-wildcard/m-p/130086#M35400</link>
    <description>&lt;P&gt;I need some assistance in calculating latency values. I have many hosts, logging transformation latency in different ways. Some of these events contain a field called transformation_latency (host=barfoo1, host=barfoo2), the others contain total_latency and backend_latency (host=foobar01, host=foobar02, foobar03))&lt;/P&gt;

&lt;P&gt;The logic works as follows: If host=foobar*, transformation_latency=(total_latency - backend_latency)&lt;/P&gt;

&lt;P&gt;my stats function looks like |stats avg(transformation_latency)&lt;/P&gt;

&lt;P&gt;I have found using eval statements, I cannot use a wildcard. I am seeking a way to perform this calculation and assign a transformation_latency if one is not present in the event. &lt;/P&gt;

&lt;P&gt;I have tried &amp;lt;pre&amp;gt;eval field=if(isnull(field),elsefield,field) &amp;lt;/pre&amp;gt;&lt;BR /&gt;
But, as we continue to add hosts, logs will differ and I must more implicitly state that this specific calculation applies to hosts like %foobar0% only&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Mon, 28 Sep 2020 20:05:41 GMT</pubDate>
    <dc:creator>bcatwork</dc:creator>
    <dc:date>2020-09-28T20:05:41Z</dc:date>
    <item>
      <title>Performing basic calculations, eval statement and wildcard</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Performing-basic-calculations-eval-statement-and-wildcard/m-p/130086#M35400</link>
      <description>&lt;P&gt;I need some assistance in calculating latency values. I have many hosts, logging transformation latency in different ways. Some of these events contain a field called transformation_latency (host=barfoo1, host=barfoo2), the others contain total_latency and backend_latency (host=foobar01, host=foobar02, foobar03))&lt;/P&gt;

&lt;P&gt;The logic works as follows: If host=foobar*, transformation_latency=(total_latency - backend_latency)&lt;/P&gt;

&lt;P&gt;my stats function looks like |stats avg(transformation_latency)&lt;/P&gt;

&lt;P&gt;I have found using eval statements, I cannot use a wildcard. I am seeking a way to perform this calculation and assign a transformation_latency if one is not present in the event. &lt;/P&gt;

&lt;P&gt;I have tried &amp;lt;pre&amp;gt;eval field=if(isnull(field),elsefield,field) &amp;lt;/pre&amp;gt;&lt;BR /&gt;
But, as we continue to add hosts, logs will differ and I must more implicitly state that this specific calculation applies to hosts like %foobar0% only&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 20:05:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Performing-basic-calculations-eval-statement-and-wildcard/m-p/130086#M35400</guid>
      <dc:creator>bcatwork</dc:creator>
      <dc:date>2020-09-28T20:05:41Z</dc:date>
    </item>
    <item>
      <title>Re: Performing basic calculations, eval statement and wildcard</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Performing-basic-calculations-eval-statement-and-wildcard/m-p/130087#M35401</link>
      <description>&lt;P&gt;I have checked the examples. This seems like it will help. How can I explicitly state the host value using a wildcard rather than just a wildcard field? &lt;/P&gt;

&lt;P&gt;This does not work, &lt;BR /&gt;
foreach host=foobar* [eval transformation_latency=total_latency-backend] &lt;/P&gt;

&lt;P&gt;I have already initialized all variables by the time of the foreach statement. I need to perform this calculation dependent on host value.&lt;/P&gt;

&lt;P&gt;Thank your for your time and prompt assistance!&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 07:54:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Performing-basic-calculations-eval-statement-and-wildcard/m-p/130087#M35401</guid>
      <dc:creator>bcatwork</dc:creator>
      <dc:date>2020-09-29T07:54:09Z</dc:date>
    </item>
    <item>
      <title>Re: Performing basic calculations, eval statement and wildcard</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Performing-basic-calculations-eval-statement-and-wildcard/m-p/130088#M35402</link>
      <description>&lt;P&gt;This is not a &lt;CODE&gt;foreach&lt;/CODE&gt; usecase.&lt;/P&gt;

&lt;P&gt;This is actually much simpler. Before your stats command, do this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval transformation_latency=coalesce(transformation_latency, total_latency-backend)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The name of the host doesn't matter. The presence or absence your transformation_latency field is all that matters. If it is there, use it. If it isn't, calculate it.&lt;/P&gt;</description>
      <pubDate>Thu, 28 May 2015 20:50:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Performing-basic-calculations-eval-statement-and-wildcard/m-p/130088#M35402</guid>
      <dc:creator>jacobwilkins</dc:creator>
      <dc:date>2015-05-28T20:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: Performing basic calculations, eval statement and wildcard</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Performing-basic-calculations-eval-statement-and-wildcard/m-p/130089#M35403</link>
      <description>&lt;P&gt;So this works for my current state. However, I will need to perform a different calculation based on host parameters in the near future that will not handle a null value for transformation_latency the same way as above.&lt;/P&gt;

&lt;P&gt;To be more specific, I calculate backend_latency using the addtotal commands for iterative backend calls, but I will need the max backend_latency* for parallel calls. Ive found I need to use the coalesce function prior to the addtotal command to have null values handled properly. So how can I state, use (total_latency-backend_latency) if host=xyz, use &lt;BR /&gt;
(total_latency-max_backend_latency)) if host=abc. Any suggestions? Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 20:06:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Performing-basic-calculations-eval-statement-and-wildcard/m-p/130089#M35403</guid>
      <dc:creator>bcatwork</dc:creator>
      <dc:date>2020-09-28T20:06:18Z</dc:date>
    </item>
    <item>
      <title>Re: Performing basic calculations, eval statement and wildcard</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Performing-basic-calculations-eval-statement-and-wildcard/m-p/130090#M35404</link>
      <description>&lt;P&gt;Well, once again, is &lt;CODE&gt;max_backend_latency&lt;/CODE&gt; null in cases where you need to use &lt;CODE&gt;backend_latency&lt;/CODE&gt;? If so, same trick. For clarity, we'll converge them to a 3rd field.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | eval my_backend_latency=coalesce(max_backend_latency,backend_latency)
 | eval transformation_latency=coalesce(transformation_latency, total_latency-my_backend_latency)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you &lt;EM&gt;really&lt;/EM&gt; have your heart set on doing conditionals based on host, which I'm steering you away from because that kind of procedural-think usually leads to unnecessary complexity, I'll show you how.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval my_backend_latency=case(searchmatch(host=xyz*) , backend_latency, searchmatch(host=abc*), max_backend_latency)
| eval transformation_latency=coalesce(transformation_latency, total_latency-my_backend_latency)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You could also use &lt;CODE&gt;if&lt;/CODE&gt; instead of &lt;CODE&gt;case&lt;/CODE&gt; if you only had 2 conditions. You could also use &lt;CODE&gt;match&lt;/CODE&gt; instead of &lt;CODE&gt;searchmatch&lt;/CODE&gt; if you like regex better.&lt;/P&gt;

&lt;P&gt;You should study this: &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.2.3/SearchReference/CommonEvalFunctions"&gt;http://docs.splunk.com/Documentation/Splunk/6.2.3/SearchReference/CommonEvalFunctions&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 29 May 2015 18:14:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Performing-basic-calculations-eval-statement-and-wildcard/m-p/130090#M35404</guid>
      <dc:creator>jacobwilkins</dc:creator>
      <dc:date>2015-05-29T18:14:17Z</dc:date>
    </item>
  </channel>
</rss>

