<?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: Search and display surrounding context in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76738#M181490</link>
    <description>&lt;P&gt;Had similar question. Found answer in this article &lt;/P&gt;

&lt;P&gt;&lt;A href="https://answers.splunk.com/answers/122983/what-is-the-easiest-way-to-find-log-lines-preceding-an-error.html"&gt;https://answers.splunk.com/answers/122983/what-is-the-easiest-way-to-find-log-lines-preceding-an-error.html&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 03 Oct 2016 15:55:40 GMT</pubDate>
    <dc:creator>dasveruckte</dc:creator>
    <dc:date>2016-10-03T15:55:40Z</dc:date>
    <item>
      <title>Search and display surrounding context</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76733#M181485</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I am able to perform a search of some logs, but I would like to see the context surrounding a specific event.  &lt;/P&gt;

&lt;P&gt;For example, I run a search for "foo" and receive many results, but I would like to see the lines immediatly preceding the line with "foo" in it.&lt;/P&gt;

&lt;P&gt;Using a linux grep command, I could run "grep -B5 foo /var/log/messages" to find lines with "foo" in them and the 5 lines preceding it.  Is there a way in Splunk to perform the same type of search?  &lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2011 19:44:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76733#M181485</guid>
      <dc:creator>d3vino</dc:creator>
      <dc:date>2011-09-15T19:44:58Z</dc:date>
    </item>
    <item>
      <title>Re: Search and display surrounding context</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76734#M181486</link>
      <description>&lt;P&gt;The transaction command sounds like what we want here: &lt;A href="http://docs.splunk.com/Documentation/Splunk/4.2.3/SearchReference/Transaction"&gt;http://docs.splunk.com/Documentation/Splunk/4.2.3/SearchReference/Transaction&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;I'd try something like this.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | transaction endswith="foo" maxevents=6
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 15 Sep 2011 20:44:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76734#M181486</guid>
      <dc:creator>acdevlin</dc:creator>
      <dc:date>2011-09-15T20:44:20Z</dc:date>
    </item>
    <item>
      <title>Re: Search and display surrounding context</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76735#M181487</link>
      <description>&lt;P&gt;I'm not sure if this is what the OP wanted. They just want to extract the surrounding events for an event matching a criteria. the endswith field will only query for events with "foo", but the need is to just extract any and all events surrounding the matched event.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Oct 2014 21:51:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76735#M181487</guid>
      <dc:creator>l0pher</dc:creator>
      <dc:date>2014-10-21T21:51:04Z</dc:date>
    </item>
    <item>
      <title>Re: Search and display surrounding context</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76736#M181488</link>
      <description>&lt;P&gt;I would love to see this answer expanded as well. I have tried the following to return events that are not of category 'INFO' and are within one second before an 'Error' but its not quite right...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;message="Error" | localize timebefore=1s| map search="search category!=\"INFO\" starttimeu=$starttime$ endtimeu=$endtime$" | transaction maxspan=1s
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Apr 2015 19:13:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76736#M181488</guid>
      <dc:creator>eyecantell</dc:creator>
      <dc:date>2015-04-23T19:13:41Z</dc:date>
    </item>
    <item>
      <title>Re: Search and display surrounding context</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76737#M181489</link>
      <description>&lt;P&gt;I think what we are looking for here is a combination of &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Eval"&gt;eval&lt;/A&gt; and &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Map"&gt;map&lt;/A&gt;. Use &lt;STRONG&gt;eval&lt;/STRONG&gt; to set up a time window and &lt;STRONG&gt;map&lt;/STRONG&gt; to iterate over the original search results - grabbing the events surrounding them. &lt;/P&gt;

&lt;P&gt;To get events that happened surrounding the original set of events (to gain the desired context) we can use the following to give all events from 2.5 seconds before to 1.5 seconds after the original set:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;message="Error" |eval mystarttime=_time-2.5 | eval myendtime=_time+1.5 | map search="search _time&amp;lt;$myendtime$ _time&amp;gt;$mystarttime$"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Note that this may take a bit, and beware that you can get events listed more than once if the time window you set up overlaps more than one of the original events.&lt;/P&gt;

&lt;P&gt;If you want map to do more than the default limit of 10 searches, you will want to add the &lt;EM&gt;maxsearches&lt;/EM&gt; option&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;map search="search _time&amp;lt;$myendtime$ _time&amp;gt;$mystarttime$" maxsearches=99
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You can also make the results more intuitive for reading with the &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Transaction"&gt;transaction&lt;/A&gt; command:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;message="Error" |eval mystarttime=_time-2.5 | eval myendtime=_time+1.5 | map search="search _time&amp;lt;$myendtime$ _time&amp;gt;$mystarttime$" maxsearches=99 | transaction maxspan=4s
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;it will group each set of events into a single transaction on the return.  In this case we are saying group any events within 4 seconds of each other.&lt;/P&gt;

&lt;P&gt;I would love to have a more efficient way of doing this search. Unfortunately &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Localize"&gt;localize&lt;/A&gt; is not vey intuitive and is restricted to whole second increments, and the startimeu and endtimeu time functions for &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Search"&gt;search&lt;/A&gt; didn't seem to give any better results.&lt;/P&gt;

&lt;P&gt;Here is some very basic background info in case you are new to map and eval: &lt;/P&gt;

&lt;P&gt;The &lt;STRONG&gt;map&lt;/STRONG&gt; command runs a new search for each of the events passed to it. Its a typical looping operator. &lt;/P&gt;

&lt;P&gt;For example, if &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search message="Error"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;returns 5 results, then &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search message="Error" | map search="search message=\"$message$\"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;should return 25 results because it performs a search for the same message again as it loops over each of the original results (5x5=25). Indeed it does.&lt;/P&gt;

&lt;P&gt;We could return the original set of results by limiting them to the time they were encountered (assuming none of them happened at once, in which case there would still be duplicates on the corresponding times)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;message="Error" | eval mytime=_time | map search="search message=$message$ _time=$mytime$"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The &lt;STRONG&gt;eval&lt;/STRONG&gt; command gives us the ability to set up new variables based on those found in the previous result(s)&lt;/P&gt;

&lt;P&gt;Good luck!&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2015 00:46:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76737#M181489</guid>
      <dc:creator>eyecantell</dc:creator>
      <dc:date>2015-04-24T00:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: Search and display surrounding context</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76738#M181490</link>
      <description>&lt;P&gt;Had similar question. Found answer in this article &lt;/P&gt;

&lt;P&gt;&lt;A href="https://answers.splunk.com/answers/122983/what-is-the-easiest-way-to-find-log-lines-preceding-an-error.html"&gt;https://answers.splunk.com/answers/122983/what-is-the-easiest-way-to-find-log-lines-preceding-an-error.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2016 15:55:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-and-display-surrounding-context/m-p/76738#M181490</guid>
      <dc:creator>dasveruckte</dc:creator>
      <dc:date>2016-10-03T15:55:40Z</dc:date>
    </item>
  </channel>
</rss>

