<?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 to aggregate states that have a &amp;quot;new&amp;quot; and a &amp;quot;gone&amp;quot; event? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258225#M189583</link>
    <description>&lt;P&gt;If your events are time based, then try something like below&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your search
|sort _time|table _raw _time state_codes
|streamstats current=f window=2 list(state_codes) as list_codes|table _raw _time state_codes list_codes
|search NOT state_codes="*"|eval codes=mvjoin(list_codes,",")|table _raw codes
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 29 Jan 2016 13:41:39 GMT</pubDate>
    <dc:creator>renjith_nair</dc:creator>
    <dc:date>2016-01-29T13:41:39Z</dc:date>
    <item>
      <title>How to aggregate states that have a "new" and a "gone" event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258224#M189582</link>
      <description>&lt;P&gt;Hi, &lt;BR /&gt;
I have a sequence of data describing state changes of a device. Now this device can have multiple state_codes at once or even no state_code at some points in time.&lt;BR /&gt;
The data I have only implies if a new state_code is set or unset. It looks similar to this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;00:01 037 new
00:03 037 gone
00:05                  some other event
00:12 425 new
00:13 539 new
00:17                  some other event
00:18 539 gone
00:19 425 gone
00:21                  some other event
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Is it possible to describe the other events by listing the state_codes that were valid at this point in time?&lt;/P&gt;

&lt;P&gt;I mean somthing like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;00:05 some other event, state_codes=""
00:17 some other event, state_codes="425,539"
00:21 some other event, state_codes=""
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I thought of something like a streamstats command but I don't know how to use a stats function that deals with a "gone" event.&lt;/P&gt;

&lt;P&gt;P.S. One could easily map the same problem to some users logging in and out and you want to tell which users were logged in at the moment an event arrived.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:37:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258224#M189582</guid>
      <dc:creator>gschr</dc:creator>
      <dc:date>2020-09-29T08:37:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate states that have a "new" and a "gone" event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258225#M189583</link>
      <description>&lt;P&gt;If your events are time based, then try something like below&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your search
|sort _time|table _raw _time state_codes
|streamstats current=f window=2 list(state_codes) as list_codes|table _raw _time state_codes list_codes
|search NOT state_codes="*"|eval codes=mvjoin(list_codes,",")|table _raw codes
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jan 2016 13:41:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258225#M189583</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2016-01-29T13:41:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate states that have a "new" and a "gone" event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258226#M189584</link>
      <description>&lt;P&gt;OK so I did:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count | fields - count 
| eval events="
 00:01 037 new;
 00:03 037 gone;
 00:05                  some other event;
 00:12 425 new;
 00:13 539 new;
 00:17                  some other event;
 00:18 539 gone;
 00:19 425 gone;
 00:21                  some other event
"
| eval events=split(events,";") | mvexpand events | rex field=events "(?&amp;lt;_raw&amp;gt;(?&amp;lt;_time&amp;gt;\d{2}:\d{2}) ((?&amp;lt;state_codes&amp;gt;\d{3}) (?&amp;lt;new_or_gone&amp;gt;new|gone)|(?&amp;lt;eventtext&amp;gt;[^\d]*)))" | fields state_codes new_or_gone eventtext


| sort _time |table _raw _time state_codes 
| streamstats current=f window=2 list(state_codes) as list_codes | table _raw _time state_codes list_codes
| search NOT state_codes="*" |eval codes=mvjoin(list_codes,",") |table _raw codes
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;but that gives me&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;00:05 some other event  037,037
00:17 some other event  425,539
00:21 some other event  539,425 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jan 2016 14:12:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258226#M189584</guid>
      <dc:creator>gschr</dc:creator>
      <dc:date>2016-01-29T14:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate states that have a "new" and a "gone" event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258227#M189585</link>
      <description>&lt;P&gt;Try something like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count | fields - count 
 | eval events="
  00:01 037 new;
  00:03 037 gone;
  00:05                  some other event;
  00:12 425 new;
  00:13 539 new;
  00:17                  some other event;
  00:18 539 gone;
  00:19 425 gone;
  00:21                  some other event
 "
 | eval events=split(events,";") | mvexpand events | rex field=events "(?&amp;lt;_raw&amp;gt;(?&amp;lt;_time&amp;gt;\d{2}:\d{2}) ((?&amp;lt;state_codes&amp;gt;\d{3}) (?&amp;lt;new_or_gone&amp;gt;new|gone)|(?&amp;lt;eventtext&amp;gt;[^\d]*)))" | fields state_codes new_or_gone eventtext | sort -_time |table _raw _time state_codes new_or_gone eventtext  | eval active=if(new_or_gone="new",state_codes,null()) | eval rank=if(isnull(new_or_gone),1,0) | accum rank | eventstats values(active) as state_codes by rank delim="," | where isnull(new_or_gone) | table _time,eventtext , state_codes |  nomv state_codes
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jan 2016 15:52:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258227#M189585</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2016-01-29T15:52:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate states that have a "new" and a "gone" event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258228#M189586</link>
      <description>&lt;P&gt;Unfortunately that's not what I was looking for either.&lt;/P&gt;

&lt;P&gt;I get a state_code 037 at 00:05 even though by that time state_code 037 has already been marked as gone. This code seems to ignore the gone messages completely.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:37:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258228#M189586</guid>
      <dc:creator>gschr</dc:creator>
      <dc:date>2020-09-29T08:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate states that have a "new" and a "gone" event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258229#M189587</link>
      <description>&lt;P&gt;Isn't that what you wanted? what's the change from this output ? If it's about formatting just replace the last part with below&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | sort _time |table _raw _time state_codes 
 | streamstats current=f window=2 list(state_codes) as list_codes | table _raw _time state_codes list_codes
 | search NOT state_codes="*" |eval codes=mvjoin(list_codes,",") |eval result=_raw.","."state_codes=\"".codes."\""|table result
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Jan 2016 02:46:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258229#M189587</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2016-01-30T02:46:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate states that have a "new" and a "gone" event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258230#M189588</link>
      <description>&lt;P&gt;No it's not just about formatting the output.&lt;/P&gt;

&lt;P&gt;The third field (new or gone) describes whether the status code is set to active or not active.&lt;/P&gt;

&lt;P&gt;So let's assume that there were no active state codes at point 00:00&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; 00:00 some event     active_state_codes=""
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Starting at 00:01 the state code 037 gets active. If there was an event at 00:02 it would be&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; 00:02 some event     active_state_codes="037"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;at 00:03 the state code 037 is set to not active again. So there are no active state codes at this time.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; 00:04 some event     active_state_codes=""
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Feb 2016 08:11:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258230#M189588</guid>
      <dc:creator>gschr</dc:creator>
      <dc:date>2016-02-01T08:11:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate states that have a "new" and a "gone" event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258231#M189589</link>
      <description>&lt;P&gt;Alright, got it , try below&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count | fields - count 
 | eval events="
  00:01 037 new;
  00:03 037 gone;
  00:05                  some other event;
  00:12 425 new;
  00:13 539 new;
  00:17                  some other event;
  00:18 539 gone;
  00:19 425 gone;
  00:21                  some other event
 "
| eval events=split(events,";") | mvexpand events | rex field=events "(?&amp;lt;_raw&amp;gt;(?&amp;lt;_time&amp;gt;\d{2}:\d{2}) ((?\d{3}) (?new|gone)|(?[^\d]*)))" 
|fields state_codes new_or_gone eventtext
|sort _time 
|eval event_id=if(isnull(new_or_gone),1,0) | accum event_id
|eventstats last(new_or_gone) as Final by state_codes,event_id
|eval alive=if(Final=="new",state_codes,"")
|streamstats current=f window=2 list(alive) as list_codes
|eval codes=ltrim(rtrim(mvjoin(list_codes,","),","),",")
|table _raw _time eventtext state_codes new_or_gone Final codes
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;For Final formatted result add below as well,&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|search NOT state_codes="*"
|eval result=_raw.","."state_codes=\"".codes."\""|table result
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The above gives me&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;00:05 some other event,state_codes=""
00:17 some other event,state_codes="425,539"
00:21 some other event ,state_codes="" 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Feb 2016 10:12:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258231#M189589</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2016-02-01T10:12:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate states that have a "new" and a "gone" event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258232#M189590</link>
      <description>&lt;P&gt;Great answer! Thank you renjith. I wouldn't have made it to that point.&lt;/P&gt;

&lt;P&gt;Unfortunately it's still not 100% what I wanted. Let's say the event at 00:03 never happened. So state_code 037 is active all the time (because it's never marked as gone). Then at point 00:17 it should still be active but it isn't.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count | fields - count 
 | eval events="
  00:01 037 new;

  00:05                  some other event;
  00:12 425 new;
  00:13 539 new;
  00:17                  some other event;
  00:18 539 gone;
  00:19 425 gone;
  00:21                  some other event
 "
 | eval events=split(events,";") | mvexpand events | rex field=events "(?&amp;lt;_raw&amp;gt;(?&amp;lt;_time&amp;gt;\d{2}:\d{2}) ((?\d{3}) (?new|gone)|(?[^\d]*)))"
 |fields state_codes new_or_gone eventtext
 |sort _time
 |eval event_id=if(isnull(new_or_gone),1,0) | accum event_id
 |eventstats last(new_or_gone) as Final by state_codes,event_id
 |eval alive=if(Final=="new",state_codes,"")
 |streamstats current=f window=2 list(alive) as list_codes
 |eval codes=ltrim(rtrim(mvjoin(list_codes,","),","),",")
 |table _raw _time eventtext state_codes new_or_gone Final codes
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;gives me&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    00:17   some other event                425,539 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;instead of&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    00:17   some other event                037,425,539
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Feb 2016 18:38:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258232#M189590</guid>
      <dc:creator>gschr</dc:creator>
      <dc:date>2016-02-01T18:38:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to aggregate states that have a "new" and a "gone" event?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258233#M189591</link>
      <description>&lt;P&gt;The more you ask , the more streamstats comes in &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; . Try this. Please be aware that if there are any &lt;EM&gt;some other event&lt;/EM&gt;,it will aggregate to that row. So if you don't have  an event at 00:03 for gone,  it will display 037 at 00:05. Hope that's enough&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count | fields - count 
  | eval events="
   00:01 037 new;
   00:12 425 new;
   00:13 539 new;
   00:17                  some other event;
   00:18 539 gone;
   00:19 425 gone;
   00:21                  some other event
  "
  |eval events=split(events,";") | mvexpand events | rex field=events "(?&amp;lt;_raw&amp;gt;(?&amp;lt;_time&amp;gt;\d{2}:\d{2}) ((?&amp;lt;state_codes&amp;gt;\d{3}) (?&amp;lt;new_or_gone&amp;gt;new|gone)|(?&amp;lt;eventtext&amp;gt;[^\d]*)))"
  |fields state_codes new_or_gone eventtext
  |sort _time
  |eval event_id=if(isnull(new_or_gone),1,0) | accum event_id
  |eventstats last(new_or_gone) as Final by state_codes,event_id
  |eval alive=if(Final=="new",state_codes,"")
  |streamstats current=t  list(alive) as list_codes by event_id
  |eval codes=ltrim(rtrim(mvjoin(list_codes,","),","),",")
  |streamstats current=f window=1  first(codes) as final_codes
  |table _raw _time eventtext state_codes new_or_gone final_codes
  |search NOT state_codes="*"
  |eval result=_raw.","."state_codes=\"".final_codes."\""|table result
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is a quick and dirty one. You might be able to optimize this by reducing the eventstats and streamstats&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2016 06:33:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-aggregate-states-that-have-a-quot-new-quot-and-a-quot/m-p/258233#M189591</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2016-02-02T06:33:13Z</dc:date>
    </item>
  </channel>
</rss>

