<?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 Re: Loop through lookuptable to produce averages in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266074#M80006</link>
    <description>&lt;P&gt;Try adding &lt;CODE&gt;by Classification&lt;/CODE&gt; to your eventstats. Eventstats works the same as stats in terms of what functions you can do. The main difference is the only fields available after the stats commands are the ones created as part of the functions or included in the 'by'. For example your &lt;CODE&gt;Date&lt;/CODE&gt; field isn't available after the stats command.&lt;/P&gt;</description>
    <pubDate>Thu, 10 Dec 2015 17:16:43 GMT</pubDate>
    <dc:creator>Runals</dc:creator>
    <dc:date>2015-12-10T17:16:43Z</dc:date>
    <item>
      <title>Loop through lookuptable to produce averages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266069#M80001</link>
      <description>&lt;P&gt;Hello Splunkers,&lt;/P&gt;

&lt;P&gt;I have static values (user groups) that I need to loop through to produce the results for each of the static values. We then use that to produce averages for the users within those groups and alert on any that are above the average by a small percent. I may be making this more difficult than necessary. While I can hard code the group info, I would prefer to not have 20 identical searches other than the group identifier. My averages are also not returning the desired results, it seems that the average is not carried through all of the data.  &lt;/P&gt;

&lt;P&gt;Input.csv:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Group,
grp1,
grp2,
grp3,
grp4,
etc...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hardcoded results:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;User       #access      total group      groupavg
Usera          20                45            10
Userb          15                                        
Userc           5
Userd           5
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In this case using the variable &lt;CODE&gt;groupavg&lt;/CODE&gt; it would only produce the result that &lt;CODE&gt;usera&lt;/CODE&gt; was above the average not &lt;CODE&gt;usera&lt;/CODE&gt; and &lt;CODE&gt;userb&lt;/CODE&gt;. Now if you hardcode the value of &lt;CODE&gt;10&lt;/CODE&gt; it does show both &lt;CODE&gt;usera&lt;/CODE&gt; and &lt;CODE&gt;userb&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;The search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="foo" [|inputlookup  input.csv |rename Group as query |fields query] Classification=query
|dedup fooKey
| eval Date=strftime(_time, "%D")
| stats count by User
|appendcols [search index="foo"  Classification=query
|dedup fooKey
| eval Date=strftime(_time, "%D")
| stats count as total dc(user) as totuser
|eval groupavg=round(total/totuser,0)]
 |where count &amp;gt; (groupavg*1.5)
|fields - total, totuser
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So to see if I was way off base, I tried a simple search using a lookup file against &lt;CODE&gt;_internal&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Inputs.csv:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Log,
INFO,
WARNING,
WARN,
ERROR,
error,
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="_internal" [|inputlookup testfile.csv | rename Log AS query | fields query] Log_Level=query |stats count by query
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This returned nothing as far as &lt;CODE&gt;stats&lt;/CODE&gt;, but it did return events: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;12/7/15
2:19:41.176 PM  
12-07-2015 14:19:41.176 -0700 INFO  StreamedSearch - Streamed search connection terminated: search_id=remote_indexmaster.localdomain_1449523206.47, server=indexmaster.localdomain, active_searches=0, elapsedTime=0.273, search='litsearch index="_internal" ( ( INFO ) OR ( WARNING) OR ( WARN ) OR ( ERROR ) OR ( error ) ) Log_Level=query | addinfo  type=count label=prereport_events | fields  keepcolorder=t "*" "_bkt" "_cd" "_si" "host" "index" "linecount" "prestats_reserved_*" "psrsvd_*" "query" "source" "sourcetype" "splunk_server"  | remotetl  nb=300 et=1449508800.000000 lt=1449523206.000000 max_count=1000 max_prefetch=100 | prestats  count by query', savedsearch_name=""
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So it is returning  the fields from the &lt;CODE&gt;input.csv&lt;/CODE&gt;, but in the Boolean format &lt;CODE&gt;( INFO ) OR ( WARNING) OR ( WARN ) OR ( ERROR ) OR ( error )&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;I need to be able to extract each field run in a search against those results as stated above.&lt;/P&gt;

&lt;P&gt;So what am I doing wrong?... Should I not use an input lookup to loop through the results?&lt;/P&gt;

&lt;P&gt;Ideally we would like to see that &lt;CODE&gt;Usera&lt;/CODE&gt; who is part of &lt;CODE&gt;grpd&lt;/CODE&gt; is 1.5 times higher than the rest of the users in &lt;CODE&gt;grpd&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Thank you in advance for any assistance you can provide.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Dec 2015 22:27:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266069#M80001</guid>
      <dc:creator>mikev</dc:creator>
      <dc:date>2015-12-07T22:27:09Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through lookuptable to produce averages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266070#M80002</link>
      <description>&lt;P&gt;query is a special field name.&lt;/P&gt;

&lt;P&gt;&lt;A href="https://community.splunk.com/INFO" target="_blank"&gt;|inputlookup testfile.csv | rename Log AS query | fields query&lt;/A&gt; OR ( WARNING) OR ( WARN ) OR ( ERROR ) OR ( error )&lt;/P&gt;

&lt;P&gt;&lt;A href="https://community.splunk.com/Log_Level=INFO" target="_blank"&gt;|inputlookup testfile.csv | rename Log AS Log_Level| fields Log_Level&lt;/A&gt; OR ( Log_Level=WARNING) OR ( Log_Level=WARN ) OR ( Log_Level=ERROR ) OR ( Log_Level=error )&lt;/P&gt;

&lt;P&gt;Is this okay.&lt;BR /&gt;
 index="_internal" [|inputlookup testfile.csv | rename Log AS query | fields query] Log_Level=query |stats count by query&lt;BR /&gt;
⇒ index="_internal" [|inputlookup testfile.csv | rename Log AS Log_Level| fields Log_Level]  |stats count by Log_Level&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:05:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266070#M80002</guid>
      <dc:creator>HiroshiSatoh</dc:creator>
      <dc:date>2020-09-29T08:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through lookuptable to produce averages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266071#M80003</link>
      <description>&lt;P&gt;LIke HiroshiSatoh posted though the key with subsearches is to have any output match fields that are going to be in the data from your parent search. One thing you can do (if you don't already know) is add &lt;CODE&gt;| format&lt;/CODE&gt; to the end of a search that will be come a subsearch. It will show you what will be passed to the parent.&lt;/P&gt;

&lt;P&gt;Additionally I think you will want to be introduced to the eventstats command.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="foo" [|inputlookup  input.csv |rename Group as Classification |fields Classification] 
 |dedup fooKey
 | stats count by User
 | eventstats sum(count) as total dc(User) as totuser
 |eval groupavg=round(total/totuser,0)]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've left off your eval as because it is before the stats command and isn't referenced in the stats command it will be dropped. I've also not included the where and fields command as you can always add those back in once you've seen the outcome. The big challenge (to me) is there isn't enough information in what you've provided for us to know if this query will return the results you are looking for. For example - comparing your hardcoded results to your query there isn't a field for &lt;CODE&gt;#access&lt;/CODE&gt;, &lt;CODE&gt;total&lt;/CODE&gt;, and &lt;CODE&gt;group&lt;/CODE&gt; let alone what those numbers mean. What is &lt;CODE&gt;fooKey&lt;/CODE&gt; and does it need to be de-duplicated? Perhaps I've not had enough caffeine &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Dec 2015 12:41:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266071#M80003</guid>
      <dc:creator>Runals</dc:creator>
      <dc:date>2015-12-08T12:41:37Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through lookuptable to produce averages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266072#M80004</link>
      <description>&lt;P&gt;OK, easy answer first, the dedup is a key value that the customer uses, not important for this issue &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;Combining HiroshiSatoh and yours I have this (working off of internal for now):&lt;BR /&gt;
index="_internal" [|inputlookup testfile.csv | rename Log AS log_level| fields log_level] &lt;BR /&gt;
|stats count by log_level &lt;BR /&gt;
|eventstats sum(count) as totlog dc(log_level) as totgrp&lt;BR /&gt;
|eval grpavg=round(totlog/totgrp,0)&lt;/P&gt;

&lt;P&gt;This returns the following:&lt;/P&gt;

&lt;P&gt;log_level   count   grpavg  totgrp  totlog&lt;BR /&gt;
ERROR         54    10758   3   32275&lt;BR /&gt;
INFO    31841   10758   3   32275&lt;BR /&gt;
WARN        380     10758   3   32275 &lt;/P&gt;

&lt;P&gt;Ok so this returns the information that we can work with, when adding in the where clause:&lt;BR /&gt;
index="_internal" [|inputlookup testfile.csv | rename Log AS log_level| fields log_level] |stats count by log_level |eventstats sum(count) as totlog dc(log_level) as totgrp|eval grpavg=round(totlog/totgrp,0)|where count &amp;gt; (grpavg*1.5)&lt;/P&gt;

&lt;P&gt;INFO is the only returned result - yeah!!! Now  to see if this works with the customer data, I'll let you know and accept if so&lt;/P&gt;

&lt;P&gt;Got word back from the customer, while it is returning results, it is combining all of the classifications into a group and doing the average on the total for every classification. &lt;/P&gt;

&lt;P&gt;The goal is to read the file for classification "A" gather all information from that classification and if a user in that classification is above the average for the group then we want to know about that user in that group, then move to the next classification and do it all over again.&lt;/P&gt;

&lt;P&gt;Here is the actual search we ran:&lt;/P&gt;

&lt;P&gt;index="usrindex" [|inputlookup classfile.csv | rename class AS Classification| fields Classification] &lt;BR /&gt;
|dedup UserKey &lt;BR /&gt;
| eval Date=strftime(_time, "%D") &lt;BR /&gt;
| stats count by UserID &lt;BR /&gt;
| eventstats sum(count) as total dc(UserID) as totuser &lt;BR /&gt;
|eval groupavg=round(total/totuser,0) &lt;BR /&gt;
|where count &amp;gt; (groupavg*1.5) &lt;/P&gt;

&lt;P&gt;So how would I ensure I am only working within each classification NOT all classifications for my averages&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:08:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266072#M80004</guid>
      <dc:creator>mikev</dc:creator>
      <dc:date>2020-09-29T08:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through lookuptable to produce averages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266073#M80005</link>
      <description>&lt;P&gt;Any other suggestions on how to have it loop and print only each classification not all classifications?&lt;/P&gt;</description>
      <pubDate>Thu, 10 Dec 2015 16:24:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266073#M80005</guid>
      <dc:creator>mikev</dc:creator>
      <dc:date>2015-12-10T16:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through lookuptable to produce averages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266074#M80006</link>
      <description>&lt;P&gt;Try adding &lt;CODE&gt;by Classification&lt;/CODE&gt; to your eventstats. Eventstats works the same as stats in terms of what functions you can do. The main difference is the only fields available after the stats commands are the ones created as part of the functions or included in the 'by'. For example your &lt;CODE&gt;Date&lt;/CODE&gt; field isn't available after the stats command.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Dec 2015 17:16:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266074#M80006</guid>
      <dc:creator>Runals</dc:creator>
      <dc:date>2015-12-10T17:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through lookuptable to produce averages</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266075#M80007</link>
      <description>&lt;P&gt;oh but make sure Classification is part of your stats command as well.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Dec 2015 17:17:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Loop-through-lookuptable-to-produce-averages/m-p/266075#M80007</guid>
      <dc:creator>Runals</dc:creator>
      <dc:date>2015-12-10T17:17:11Z</dc:date>
    </item>
  </channel>
</rss>

