<?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 calculate last 1 hr events for last one week in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194509#M187299</link>
    <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;I want to calculate the last 1 hour events for the past week at any given point of time. Support now the time is 11.30a.m. I want to events from 10.am to 11.30am for past 1 week. &lt;/P&gt;

&lt;P&gt;I'm trying the below search query, but it is returning zero. Since the below query runs on _internal index, you can try on ur system too. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal source=*metrics.log splunk_server="*" group="per_index_thruput" earliest=-7d@d  
| eval MB=kb/1024 | eval duration=60*60 
| eval current_hour=strftime(now(),"%H")
| eval last_hour=strftime((now()-duration),"%H") 
| eval current_Min=strftime(now(),"%M") 
| where (date_hour=last_hour AND date_minute&amp;gt;current_Min) AND 
        (date_hour&amp;lt;=current_hour AND date_minute&amp;lt;=current_Min) 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Whats wrong here and what is the right query?  &lt;/P&gt;</description>
    <pubDate>Tue, 10 Jun 2014 14:27:55 GMT</pubDate>
    <dc:creator>jayannah</dc:creator>
    <dc:date>2014-06-10T14:27:55Z</dc:date>
    <item>
      <title>calculate last 1 hr events for last one week</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194509#M187299</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;I want to calculate the last 1 hour events for the past week at any given point of time. Support now the time is 11.30a.m. I want to events from 10.am to 11.30am for past 1 week. &lt;/P&gt;

&lt;P&gt;I'm trying the below search query, but it is returning zero. Since the below query runs on _internal index, you can try on ur system too. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal source=*metrics.log splunk_server="*" group="per_index_thruput" earliest=-7d@d  
| eval MB=kb/1024 | eval duration=60*60 
| eval current_hour=strftime(now(),"%H")
| eval last_hour=strftime((now()-duration),"%H") 
| eval current_Min=strftime(now(),"%M") 
| where (date_hour=last_hour AND date_minute&amp;gt;current_Min) AND 
        (date_hour&amp;lt;=current_hour AND date_minute&amp;lt;=current_Min) 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Whats wrong here and what is the right query?  &lt;/P&gt;</description>
      <pubDate>Tue, 10 Jun 2014 14:27:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194509#M187299</guid>
      <dc:creator>jayannah</dc:creator>
      <dc:date>2014-06-10T14:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: calculate last 1 hr events for last one week</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194510#M187300</link>
      <description>&lt;P&gt;[Note: fixed search conditions] First, the &lt;CODE&gt;date_*&lt;/CODE&gt; fields do not exist in all events.&lt;/P&gt;

&lt;P&gt;Second, I suggest that you take a look at the &lt;A href="https://apps.splunk.com/app/1645/"&gt;Timewrap app&lt;/A&gt; - while it may not solve this problem exactly, this is the kind of problem that it is designed to solve. It's free.&lt;/P&gt;

&lt;P&gt;And here is my suggestion&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal source=*metrics.log group="per_index_thruput" earliest=-7d@d 
| eval MB=kb/1024 
| eval startTimeOfDay=strftime(relative_time(now(),"-1h@h"),"%H:%M")
| eval endTimeOfDay=strftime(now(),"%H:%M")
| eval eventDay=strftime(_time,"%Y-%m-%d ")
| eval eventEarliest=strptime(eventDay . startTimeOfDay,"%Y-%m-%d %H:%M")
| eval eventLatest=strptime(eventDay . endTimeOfDay,"%Y-%m-%d %H:%M")
| where _time &amp;gt;= eventEarliest AND _time &amp;lt;= eventLatest
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This solution does not depend on the presence of the &lt;CODE&gt;date_*&lt;/CODE&gt; fields, which sometimes don't exist and can have difficulties due to timezone.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jun 2014 15:45:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194510#M187300</guid>
      <dc:creator>lguinn2</dc:creator>
      <dc:date>2014-06-10T15:45:00Z</dc:date>
    </item>
    <item>
      <title>Re: calculate last 1 hr events for last one week</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194511#M187301</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal source=*metrics.log splunk_server="*" group="per_index_thruput" earliest=-7d@d  
| eval MB=kb/1024 | eval duration=60*60 
| eval current_hour=strftime(now(),"%H")
| eval last_hour=strftime((now()-duration),"%H") 
| eval current_Min=strftime(now(),"%M") 
| where (date_hour=last_hour AND date_minute&amp;gt;current_Min) OR 
        (date_hour=current_hour AND date_minute&amp;lt;=current_Min)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Try this one&lt;/STRONG&gt;&lt;BR /&gt;
Slightly modified @Iguinn's answer&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal source=*metrics.log group="per_index_thruput" earliest=-7d@d 
| eval MB=kb/1024 
| eval startTimeOfDay=strftime(now()-60*60*4,"%H:%M")
| eval endTimeOfDay=strftime(now(),"%H:%M")
| eval eventDay=strftime(_time,"%Y-%m-%d")
| eval eventEarliest=strptime(eventDay." ".startTimeOfDay,"%Y-%m-%d %H:%M")
| eval eventLatest=strptime(eventDay." ".endTimeOfDay,"%Y-%m-%d %H:%M")  | where _time&amp;gt;=eventEarliest AND _time&amp;lt;=eventLatest 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 10 Jun 2014 15:50:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194511#M187301</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2014-06-10T15:50:03Z</dc:date>
    </item>
    <item>
      <title>Re: calculate last 1 hr events for last one week</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194512#M187302</link>
      <description>&lt;P&gt;Thank you the response.&lt;/P&gt;

&lt;P&gt;The above query doesn't return events. I just copy pasted your query in my search and no events seen. But, there are events in the _internal for the selected time in the system.&lt;/P&gt;

&lt;P&gt;Is this query worked for you in your system?&lt;/P&gt;

&lt;P&gt;Note that, I tried by removing space around "." in strptime functional aswell. Still no events seen.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jun 2014 16:07:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194512#M187302</guid>
      <dc:creator>jayannah</dc:creator>
      <dc:date>2014-06-10T16:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: calculate last 1 hr events for last one week</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194513#M187303</link>
      <description>&lt;P&gt;Hi Soni, Thanks for response. If I change the duration to 4 Hours i.e 60*60*4, this query wont work as expected.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 16:49:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194513#M187303</guid>
      <dc:creator>jayannah</dc:creator>
      <dc:date>2020-09-28T16:49:38Z</dc:date>
    </item>
    <item>
      <title>Re: calculate last 1 hr events for last one week</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194514#M187304</link>
      <description>&lt;P&gt;Thanks. Works fine.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jun 2014 20:08:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194514#M187304</guid>
      <dc:creator>jayannah</dc:creator>
      <dc:date>2014-06-10T20:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: calculate last 1 hr events for last one week</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194515#M187305</link>
      <description>&lt;P&gt;I copy pasted the same your query on my laptop and tested on _internal index. It works well.&lt;/P&gt;

&lt;P&gt;But, on the production system where I logged in as NON-admin user with access to only SEARCH APP and Index name 'abc'.&lt;/P&gt;

&lt;P&gt;When I issue the same command&lt;BR /&gt;
 index=_abc earliest=-7d@d &lt;BR /&gt;
| eval period=60*60*4&lt;BR /&gt;
| eval startTimeOfDay=strftime(now()-period,"%H:%M")&lt;BR /&gt;
| eval endTimeOfDay=strftime(now(),"%H:%M")&lt;BR /&gt;
| eval eventDay=strftime(_time,"%Y-%m-%d")&lt;BR /&gt;
| eval eventEarliest=strptime(eventDay." ".startTimeOfDay,"%Y-%m-%d %H:%M")&lt;BR /&gt;
| eval eventLatest=strptime(eventDay." ".endTimeOfDay,"%Y-%m-%d %H:%M")&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 16:49:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194515#M187305</guid>
      <dc:creator>jayannah</dc:creator>
      <dc:date>2020-09-28T16:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: calculate last 1 hr events for last one week</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194516#M187306</link>
      <description>&lt;P&gt;The fields eventEarliest  and eventLatest are NOT getting created !!!  is there any permission required? &lt;/P&gt;

&lt;P&gt;When I logged in ADMIN user on my laptop these 2 fields get created, but NOT for non-admin user on production system.&lt;/P&gt;

&lt;P&gt;Please do the needful.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Jun 2014 20:44:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194516#M187306</guid>
      <dc:creator>jayannah</dc:creator>
      <dc:date>2014-06-10T20:44:54Z</dc:date>
    </item>
    <item>
      <title>Re: calculate last 1 hr events for last one week</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194517#M187307</link>
      <description>&lt;P&gt;Revised answer:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal source=*metrics.log group="per_index_thruput" earliest=-7d@d 
| eval MB=kb/1024 
| eval endTimeOfDay=strftime(now(),"%H:%M")
| eval eventDay=strftime(_time,"%Y-%m-%d ")
| eval eventLatest=strptime(eventDay . endTimeOfDay,"%Y-%m-%d %H:%M")
| eval eventEarliest = relative_time(eventLatest,"-1h@h")
| where _time &amp;gt;= eventEarliest AND _time &amp;lt;= eventLatest
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In the final line, I had reversed the test! Also I have updated the computations to work properly even across date boundaries.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jun 2014 07:14:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-last-1-hr-events-for-last-one-week/m-p/194517#M187307</guid>
      <dc:creator>lguinn2</dc:creator>
      <dc:date>2014-06-11T07:14:23Z</dc:date>
    </item>
  </channel>
</rss>

