<?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: How can I catch the both the first and last occurance of an event? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95989#M24765</link>
    <description>&lt;P&gt;Better would simply be:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=proxy ip=192.168.1.1 
| stats earliest(_time) as start, latest(_time) as stop
  by user,ip
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This will also run faster (basically twice as fast) as a solution that has to get earliest and latest, since it does a single pass through the data. If you really want to split out separate rows for start and stop (instead of just having them be separate columns), you could do:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=proxy ip=192.168.1.1 
| stats earliest(_time) as start, latest(_time) as stop
  by user,ip
| eval uip=user+";"+ip
| fields uip start stop
| untable uip action _time
| eval uip=split(uip,";") | eval user=mvindex(uip,0) | eval ip=mvindex(uip,1)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But you might be better off with it as above.&lt;/P&gt;</description>
    <pubDate>Sun, 18 Mar 2012 23:37:08 GMT</pubDate>
    <dc:creator>gkanapathy</dc:creator>
    <dc:date>2012-03-18T23:37:08Z</dc:date>
    <item>
      <title>How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95981#M24757</link>
      <description>&lt;P&gt;Scenario: figure out what user is using a given IP at a given point in time by using proxy logs, which captures the user's ID each time they visit a web page.&lt;/P&gt;

&lt;P&gt;Right now I do two separate searches. This one catches the &lt;EM&gt;last&lt;/EM&gt; time the user's ID shows in the log for the IP in the time period being searched:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;192.168.1.1 sourcetype=proxy | dedup user | sort-_time | table _time, user,&lt;BR /&gt;
IP&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;This gives me a result of:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;_time   user    IP&lt;/P&gt;

&lt;P&gt;1 18/03/2012 18:57:27.000 Alice   192.168.1.1&lt;/P&gt;

&lt;P&gt;2 18/03/2012 12:47:41.000 Bob 192.168.1.1&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;So there's the last time they used the IP for the range searched. I then run a similar search that gives me the &lt;EM&gt;first&lt;/EM&gt; time the user's ID shows:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;192.168.1.1 sourcetype=proxy | dedup user sortby +_time | sort-_time |&lt;BR /&gt;
table _time, user, IP&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;This gives me a result of:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;_time   user    IP&lt;/P&gt;

&lt;P&gt;1 18/03/2012 13:44:42.000 Alice 192.168.1.1&lt;/P&gt;

&lt;P&gt;2 18/03/2012 07:44:40.000 Bob 192.168.1.1&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;So there's the first time that they used the IP. Looking at the two, I know that Alice first used the IP at 7:44 AM and last used it at 18:57 PM.&lt;/P&gt;

&lt;P&gt;However, I have to run both searches and notate the results elsewhere in order to calculate that. Easy enough in this example, but when I have a few dozen people sharing dynamic IPs (think a VPN type of situation), it gets pretty hairy pretty fast.&lt;/P&gt;

&lt;P&gt;So, what I want is a search that combines both of of the above and gives me an output like this:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;_time   user    IP  Start/Stop&lt;/P&gt;

&lt;P&gt;1  18/03/2012 18:57:27.000 Alice   192.168.1.1 Stop&lt;/P&gt;

&lt;P&gt;2  18/03/2012 13:44:42.000 Alice   192.168.1.1 Start&lt;/P&gt;

&lt;P&gt;3  18/03/2012 12:47:41.000 Bob 192.168.1.1 Stop&lt;/P&gt;

&lt;P&gt;4  18/03/2012 07:44:40.000 Bob 192.168.1.1 Start&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;Thoughts?&lt;/P&gt;

&lt;P&gt;Thank you.&lt;/P&gt;

&lt;P&gt;Peter&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 11:32:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95981#M24757</guid>
      <dc:creator>PHRaymond</dc:creator>
      <dc:date>2020-09-28T11:32:11Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95982#M24758</link>
      <description>&lt;P&gt;I don't know quite what you're trying to do with the end search, but I would be inclined to do something like"&lt;/P&gt;

&lt;P&gt;192.168.1.1 sourcetype=proxy |transaction user IP |  table _time, user, IP&lt;/P&gt;

&lt;P&gt;It is quite possible that I will end up on the same IP in different sesisons, so your first and last search might not be completely accurate.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2012 22:31:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95982#M24758</guid>
      <dc:creator>GKC_DavidAnso</dc:creator>
      <dc:date>2012-03-18T22:31:40Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95983#M24759</link>
      <description>&lt;P&gt;Hmmm. You got me to thinking with your comment "don't know quite what you're trying to do with the end search." I was so focused on the first/last time aspect that perhaps I was missing what I'm really after. If I just do my search against the first occurance in a time frame, the simple existence of a new user showing up means the prior user had to have logged off.&lt;/P&gt;

&lt;P&gt;And I hadn't thought about the possibility of one user pulling the same IP in two sessions across a period of time! &lt;/P&gt;

&lt;P&gt;So, transaction. Interesting. What precisely does that get me?&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2012 22:51:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95983#M24759</guid>
      <dc:creator>PHRaymond</dc:creator>
      <dc:date>2012-03-18T22:51:59Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95984#M24760</link>
      <description>&lt;P&gt;For example, on my prod box, running a 30 day query against an IP gives me 8 results using the transaction string you list, and 3 users. Removing it gives me 5,962 results, still 3 users. Why those 8 in the first and so many in the second? I need a confidence level that I'm not missing something.&lt;/P&gt;

&lt;P&gt;I must go poke at this some more....&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2012 22:55:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95984#M24760</guid>
      <dc:creator>PHRaymond</dc:creator>
      <dc:date>2012-03-18T22:55:00Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95985#M24761</link>
      <description>&lt;P&gt;As GKC suggested , perhaps a transaction might be better.&lt;/P&gt;

&lt;P&gt;But anyhow to try and do what you asked for , try something like :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=proxy| stats first(_time) as _time by user,IP | eval action="Stop" | append [search sourcetype=proxy | stats last(_time) as _time by user,IP |eval action="Start"] | eval time=strftime(_time, "%d/%m/%y %H:%M:%S") | table time user IP action
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Should produce the results in the format you specified for any IP used by the users within your chosen search time range.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2012 22:57:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95985#M24761</guid>
      <dc:creator>Damien_Dallimor</dc:creator>
      <dc:date>2012-03-18T22:57:22Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95986#M24762</link>
      <description>&lt;P&gt;I'm poking at GKC's answer now. I'm certainly open to better searches. That's why I'm here!&lt;/P&gt;

&lt;P&gt;On yours, I try to run it and I get:&lt;/P&gt;

&lt;P&gt;"Search operation 'sourcetype' is unknown. You might not have permission to run this operation."&lt;/P&gt;

&lt;P&gt;That seems odd to me...&lt;/P&gt;

&lt;P&gt;Unrelated, your " eval time=strftime(_time, "%d/%m/%y %H:%M:%S") " intrigues me. I've been using " convert ctime(_time) as timestamp " for what I think is the same purpose. Why might one use one over the other?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 11:32:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95986#M24762</guid>
      <dc:creator>PHRaymond</dc:creator>
      <dc:date>2020-09-28T11:32:13Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95987#M24763</link>
      <description>&lt;P&gt;edited , had a missing "search" function.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2012 23:21:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95987#M24763</guid>
      <dc:creator>Damien_Dallimor</dc:creator>
      <dc:date>2012-03-18T23:21:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95988#M24764</link>
      <description>&lt;P&gt;from the Convert docs:&lt;/P&gt;

&lt;P&gt;Note: This command is mostly deprecated, and its functionality has been re-worked as functions of the eval command such as strftime(), strptime(), or tostring().&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2012 23:23:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95988#M24764</guid>
      <dc:creator>Damien_Dallimor</dc:creator>
      <dc:date>2012-03-18T23:23:04Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95989#M24765</link>
      <description>&lt;P&gt;Better would simply be:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=proxy ip=192.168.1.1 
| stats earliest(_time) as start, latest(_time) as stop
  by user,ip
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This will also run faster (basically twice as fast) as a solution that has to get earliest and latest, since it does a single pass through the data. If you really want to split out separate rows for start and stop (instead of just having them be separate columns), you could do:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=proxy ip=192.168.1.1 
| stats earliest(_time) as start, latest(_time) as stop
  by user,ip
| eval uip=user+";"+ip
| fields uip start stop
| untable uip action _time
| eval uip=split(uip,";") | eval user=mvindex(uip,0) | eval ip=mvindex(uip,1)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But you might be better off with it as above.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2012 23:37:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95989#M24765</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2012-03-18T23:37:08Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95990#M24766</link>
      <description>&lt;P&gt;Ok, now we're cooking with gas! I like "twice as fast" and "single pass" quite a bit, and I'm not wedded to the separate line idea; in fact, the columns makes even more sense.&lt;/P&gt;

&lt;P&gt;So, combining this with Damien's time conversion, I've got:&lt;/P&gt;

&lt;P&gt;sourcetype=proxy ip=192.168.1.1 | eval time=strftime(_time, "%d/%m/%y %H:%M:%S") | stats earliest(time) as start, latest(time) as stop by user,ip&lt;/P&gt;

&lt;P&gt;I like it. Now, question, would this work as efficiently:&lt;/P&gt;

&lt;P&gt;192.168.1.1 sourcetype=proxy | eval time=strftime(_time, "%d/%m/%y %H:%M:%S") | stats earliest(time) as start, latest(time) as stop by user,ip&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2012 23:53:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95990#M24766</guid>
      <dc:creator>PHRaymond</dc:creator>
      <dc:date>2012-03-18T23:53:29Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95991#M24767</link>
      <description>&lt;P&gt;(As for the why on that last, I use a macro that fills in everything after the IP for me automatically, so my methodology is to type/paste in the IP I'm looking for and then kick off the macro.)&lt;/P&gt;</description>
      <pubDate>Sun, 18 Mar 2012 23:54:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95991#M24767</guid>
      <dc:creator>PHRaymond</dc:creator>
      <dc:date>2012-03-18T23:54:33Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95992#M24768</link>
      <description>&lt;P&gt;I'm running some test scenarios and the first question that pops up is: how can I get the results to sort so that the "stop" column is ordered latest to earliest? (This is coming up as I experiment with using multiple sourcetypes to build a bigger picture; some proxy logs don't have a user but, say, a VPN log for the same IP might.)&lt;/P&gt;</description>
      <pubDate>Mon, 19 Mar 2012 00:30:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95992#M24768</guid>
      <dc:creator>PHRaymond</dc:creator>
      <dc:date>2012-03-19T00:30:23Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95993#M24769</link>
      <description>&lt;P&gt;You can find out more about transactions here:  &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/transaction"&gt;http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/transaction&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Try this:  192.168.1.1 sourcetype=proxy |transaction user IP startswith=Start endswith=Stop |  table _time, user, IP, duration&lt;/P&gt;

&lt;P&gt;The transaction joins like events together.  user and IP are the fields that we are using to join.  The transaction calculates the duration between the first and last events for you and saves it into a value called duration.&lt;/P&gt;

&lt;P&gt;I can see you've already got some other quite nice solutions too.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Mar 2012 03:53:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95993#M24769</guid>
      <dc:creator>GKC_DavidAnso</dc:creator>
      <dc:date>2012-03-19T03:53:06Z</dc:date>
    </item>
    <item>
      <title>Re: How can I catch the both the first and last occurance of an event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95994#M24770</link>
      <description>&lt;P&gt;sort &lt;EM&gt;before&lt;/EM&gt; you convert: &lt;CODE&gt;192.168.1.1 sourcetype=proxy | stats earliest(_time) as start, latest(_time) as stop by user,ip | sort - stop | fieldformat start=(start, "%d/%m/%y %H:%M:%S") | fieldformat stop=(stop, "%d/%m/%y %H:%M:%S")&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Mar 2012 08:51:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-catch-the-both-the-first-and-last-occurance-of-an/m-p/95994#M24770</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2012-03-19T08:51:07Z</dc:date>
    </item>
  </channel>
</rss>

