<?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: eval and range in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77196#M19527</link>
    <description>&lt;P&gt;instead of the final chart command, put in &lt;BR /&gt;
&lt;CODE&gt;&lt;BR /&gt;
stats count c(eval(category=="in") AS in_count c(eval(category=="out") AS out_count | eval ratio = in_count/out_count&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;The stats command gives you the total count as well in the field 'count' if you want to use that for your ratio. &lt;/P&gt;

&lt;P&gt;You could also have a look at the &lt;CODE&gt;top&lt;/CODE&gt; command;&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| top category&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;at the end instead. A little more basic, but may still be useful.&lt;/P&gt;

&lt;P&gt;/k&lt;/P&gt;</description>
    <pubDate>Mon, 28 Sep 2020 13:02:48 GMT</pubDate>
    <dc:creator>kristian_kolb</dc:creator>
    <dc:date>2020-09-28T13:02:48Z</dc:date>
    <item>
      <title>eval and range</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77192#M19523</link>
      <description>&lt;P&gt;I have a search which gives me a whole range of timestamps (the usual date _ hour, date _ minute and date_second)&lt;/P&gt;

&lt;P&gt;I want to populate a stacked bar chart with the those time stamps by re-arranging them in the following * bins *&lt;BR /&gt;
&lt;BR /&gt;&lt;BR /&gt;
i) 12-2 PM&lt;BR /&gt;
&lt;BR /&gt;&lt;BR /&gt;
ii) outside 12-2PM&lt;BR /&gt;
&lt;BR /&gt;&lt;BR /&gt;
iii) no timestamp found &lt;/P&gt;

&lt;P&gt;The search I devised which is not working looks like this &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype="syslog" | eval timestamp=strftime(_time,"%H:%M:%S") | eval range=case(date_hour&amp;gt;=23 AND date_hour&amp;lt;=24, "in", 
 date_hour&amp;lt;23 OR date_hour&amp;lt;=24, "out", 
timestamp==null, "rest") | timechart count(timestamp) by range
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any idea so as to why this is not working ? &lt;/P&gt;

&lt;P&gt;Any help is appreciated !&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jan 2013 17:50:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77192#M19523</guid>
      <dc:creator>asarolkar</dc:creator>
      <dc:date>2013-01-03T17:50:16Z</dc:date>
    </item>
    <item>
      <title>Re: eval and range</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77193#M19524</link>
      <description>&lt;P&gt;Hmm, a bit unclear. You are using the &lt;CODE&gt;_time&lt;/CODE&gt; field, which will always be present, even if there is no timestamp in the event itself (if all else fails, &lt;CODE&gt;_time&lt;/CODE&gt; will be set to the local time on the indexer).&lt;/P&gt;

&lt;P&gt;What type of events do you have? Also, there seems to be some confusion regarding AM/PM. 12-2PM would be the hours 12-14, i.e. late lunch time.&lt;/P&gt;

&lt;P&gt;So therefore I suggest that you use &lt;CODE&gt;rex&lt;/CODE&gt; instead of &lt;CODE&gt;eval&lt;/CODE&gt; to determine if there is a timestamp in the event itself. Assuming that you have events that have typical syslog formatting, e.g.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;2013-01-04T13:14:15,231 blah blah blah
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;you could have the following regex to find it;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your search | rex "^\d+-\d+-\d+T(?&amp;lt;time_raw&amp;gt;[0-9:]+)" | rest of search here
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;which should capture the hour-minute-second part of the timestamp into to new field &lt;CODE&gt;time_raw&lt;/CODE&gt;. You need to change the regex to fit your particular type of timestamp.&lt;/P&gt;

&lt;P&gt;To set your own date_hour etc fields, use &lt;CODE&gt;rex&lt;/CODE&gt; again;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...| rex field=time_raw "(?&amp;lt;my_hr&amp;gt;\d+):(?&amp;lt;my_min&amp;gt;\d+):(?&amp;lt;my_sec&amp;gt;\d+)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now you have your own home made date_* fields that only exist if there is a valid timestamp in the original event.&lt;/P&gt;

&lt;P&gt;After that you can build your categories;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...| eval category = if(my_hr == 12 OR my_hr == 13, "in", if (my_hr &amp;lt; 12 OR my_hr &amp;gt; 13, "out", "no_timestamp"))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;then you can add your charting commands, and I'm not sure &lt;CODE&gt;timechart&lt;/CODE&gt; is necessarily what you want. I'll leave that as an exercise though, just including a basic &lt;CODE&gt;chart&lt;/CODE&gt; below.&lt;/P&gt;

&lt;P&gt;so putting it all together:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; sourcetype=syslog | rex "^\d+-\d+-\d+T(?&amp;lt;time_raw&amp;gt;[0-9:]+)" | rex field=time_raw "(?&amp;lt;my_hr&amp;gt;\d+):(?&amp;lt;my_min&amp;gt;\d+):(?&amp;lt;my_sec&amp;gt;\d+)" | eval category = if(my_hr == 12 OR my_hr == 13, "in", if (my_hr &amp;lt; 12 OR my_hr &amp;gt; 13, "out", "no_timestamp")) | chart c by category
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope this helps.&lt;/P&gt;

&lt;P&gt;/Kristian&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jan 2013 22:24:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77193#M19524</guid>
      <dc:creator>kristian_kolb</dc:creator>
      <dc:date>2013-01-03T22:24:46Z</dc:date>
    </item>
    <item>
      <title>Re: eval and range</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77194#M19525</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;That was the most useful response ever ! &lt;/P&gt;

&lt;P&gt;Just a quick followup.&lt;/P&gt;

&lt;P&gt;What if I wanted to create a chart of the ratio of in/out ?&lt;/P&gt;

&lt;P&gt;How would I go about that ?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jan 2013 22:27:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77194#M19525</guid>
      <dc:creator>asarolkar</dc:creator>
      <dc:date>2013-01-03T22:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: eval and range</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77195#M19526</link>
      <description>&lt;P&gt;i.e. consider this filter -&amp;gt; &lt;/P&gt;

&lt;P&gt;eval category = if(my_hr == 12 OR my_hr == 13, "in", if (my_hr &amp;lt; 12 OR my_hr &amp;gt; 13, "out", "no_timestamp")) &lt;/P&gt;

&lt;P&gt;Basically I want to apply a filter here to print a ratio&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 13:02:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77195#M19526</guid>
      <dc:creator>asarolkar</dc:creator>
      <dc:date>2020-09-28T13:02:45Z</dc:date>
    </item>
    <item>
      <title>Re: eval and range</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77196#M19527</link>
      <description>&lt;P&gt;instead of the final chart command, put in &lt;BR /&gt;
&lt;CODE&gt;&lt;BR /&gt;
stats count c(eval(category=="in") AS in_count c(eval(category=="out") AS out_count | eval ratio = in_count/out_count&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;The stats command gives you the total count as well in the field 'count' if you want to use that for your ratio. &lt;/P&gt;

&lt;P&gt;You could also have a look at the &lt;CODE&gt;top&lt;/CODE&gt; command;&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| top category&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;at the end instead. A little more basic, but may still be useful.&lt;/P&gt;

&lt;P&gt;/k&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 13:02:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77196#M19527</guid>
      <dc:creator>kristian_kolb</dc:creator>
      <dc:date>2020-09-28T13:02:48Z</dc:date>
    </item>
    <item>
      <title>Re: eval and range</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77197#M19528</link>
      <description>&lt;P&gt;That was extremely helpful.&lt;/P&gt;

&lt;P&gt;One more FINAL question. Its ok if you cant answer it.&lt;/P&gt;

&lt;P&gt;lets just say I want to introduce a constant that is read from a lookup file and add it to the stats at the end.&lt;/P&gt;

&lt;P&gt;How would i do that ?&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jan 2013 00:07:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-and-range/m-p/77197#M19528</guid>
      <dc:creator>asarolkar</dc:creator>
      <dc:date>2013-01-04T00:07:06Z</dc:date>
    </item>
  </channel>
</rss>

