<?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 handle search query when json data has host field? in Splunk Enterprise</title>
    <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392291#M1248</link>
    <description>&lt;P&gt;I'm looking at a small number of events for testing, about 400.  Some have my_server_1 and some have my_server_2, so it would return values.&lt;/P&gt;

&lt;P&gt;The problem I have is that all the events come from the host (Splunk side) of prd-srv-008, but also have the other field set.&lt;BR /&gt;
In my graph, for the first minute it is showing:&lt;BR /&gt;
prd-srv-008 --COUNT: 432&lt;BR /&gt;
my_server_1 --COUNT: 320&lt;BR /&gt;
my_server_2 --COUNT: 112&lt;/P&gt;

&lt;P&gt;if you notice my_server_1 and my_server_2 always adds up to the total from prd-srv-008&lt;/P&gt;</description>
    <pubDate>Wed, 30 Sep 2020 00:03:40 GMT</pubDate>
    <dc:creator>evbtbw92</dc:creator>
    <dc:date>2020-09-30T00:03:40Z</dc:date>
    <item>
      <title>How to handle search query when JSON data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392281#M1238</link>
      <description>&lt;P&gt;I'm working on a corporate Splunk instance where we do not have access to rename fields when indexing, or make any similar modifications due to security and compliance requirements.&lt;BR /&gt;I'm trying to create a timechart based on the number of events per hour by &lt;CODE&gt;host&lt;/CODE&gt;. My issue is that the JSON data has a host field in addition to the Splunk built in &lt;CODE&gt;host&lt;/CODE&gt; field.&lt;BR /&gt;IE a sample event looks like:&lt;BR /&gt;&lt;CODE&gt;{"time":"2019-04-05T21:50:09.925Z","severity":"INFO","duration":25.02,"db":10.23,"view":14.79,"status":200,"method":"GET","path":"/api/v4/project/1","params":[],"host":"my_server_1","ip":"1.2.3.4, 4.5.6.7","ua":null,"route":"/api/:version/projects/:id","user_id":12,"username":"smithers","queue_duration":4.35,"magic_calls":0}&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;My search looks like: &lt;BR /&gt;&lt;CODE&gt;index="my_index" host="prd-srv-00*" source="/var/log/my_program/http_json*" | timechart span=1h count by host&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;When I do this it combines the hosts the logs came from (built in host field) and hosts listed in the data (host field in the json).&lt;/P&gt;
&lt;P&gt;If I try to filter out the hosts from the data, it removes the events from the built in host field as well. IE: &lt;BR /&gt;&lt;CODE&gt;index="my_index" host="prd-srv-00*" AND host !="0.0.0.0" source="/var/log/my_program/http_json*" | timechart span=1h count by host&lt;/CODE&gt;&lt;BR /&gt;I have also tried to use &lt;CODE&gt;...| where host !="0.0.0.0" | ...&lt;/CODE&gt; but this has the same result.&lt;/P&gt;
&lt;P&gt;Any advice on a solution or workaround to handle this at search time? IE, can I rename to column when searching, etc?&lt;/P&gt;
&lt;P&gt;Thanks in advance for any help.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 16:55:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392281#M1238</guid>
      <dc:creator>evbtbw92</dc:creator>
      <dc:date>2020-06-10T16:55:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392282#M1239</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;You can use regex to extract host from actual raw data in new field, try below query which will extract hostname from raw data into new field called &lt;CODE&gt;ext_host&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Based on sample event you have provided, below query will extract &lt;CODE&gt;my_server_1&lt;/CODE&gt; in &lt;CODE&gt;ext_host&lt;/CODE&gt; field.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;yourBaseSearcg&amp;gt;
| rex field=_raw "\"host\"\:\"(?&amp;lt;ext_host&amp;gt;[^\"]*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So your query will be like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="my_index" host="prd-srv-00*"  source="/var/log/my_program/http_json*"
| rex field=_raw "\"host\"\:\"(?&amp;lt;ext_host&amp;gt;[^\"]*)"
| search ext_host!="my_server_1"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 06 Apr 2019 17:30:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392282#M1239</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2019-04-06T17:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392283#M1240</link>
      <description>&lt;P&gt;I had a similar issue at &lt;A href="https://answers.splunk.com/answers/689551/how-can-i-search-for-the-field-host-with-multiple.html"&gt;How can I search for the field host with multiple values?&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Apr 2019 21:46:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392283#M1240</guid>
      <dc:creator>ddrillic</dc:creator>
      <dc:date>2019-04-06T21:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392284#M1241</link>
      <description>&lt;P&gt;@evbtbw92 could you explain the corporate requirement for not changing field name when there can be two two different values for host field where both are valid values? I think this is incorrect requirement. Is there any one host value that you need or both?&lt;/P&gt;

&lt;P&gt;If you need only the host value from JSON data, you should correct host metadata while indexing, so that searches work faster.&lt;/P&gt;

&lt;P&gt;If you need both the values, you should have one of the fields as host and other as something else, maybe &lt;CODE&gt;Host&lt;/CODE&gt;, since Splunk field names are case-sensitive (using &lt;CODE&gt;transforms.conf&lt;/CODE&gt;).&lt;/P&gt;</description>
      <pubDate>Sun, 07 Apr 2019 11:00:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392284#M1241</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2019-04-07T11:00:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392285#M1242</link>
      <description>&lt;P&gt;Here are some options.&lt;/P&gt;

&lt;P&gt;1: To ensure searching by the indexed host, you can use &lt;CODE&gt;::&lt;/CODE&gt; syntax like &lt;CODE&gt;index=foo host::bar&lt;/CODE&gt;.&lt;BR /&gt;
2: To ensure that the automatic KV_MODE extraction does not happen, polluting your &lt;CODE&gt;host&lt;/CODE&gt; value, run your search in &lt;CODE&gt;Fast mode&lt;/CODE&gt;.&lt;BR /&gt;
3: To keep the KV_MODE extractions, but not the &lt;CODE&gt;host&lt;/CODE&gt; one, add this to your search: &lt;CODE&gt;... | rex mode=sed "s/\"host\"/\"json_host\"/g"&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;I think #3 is your ticket.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 00:02:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392285#M1242</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2020-09-30T00:02:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392286#M1243</link>
      <description>&lt;P&gt;Thanks for the response, this seems close, but isn't exactly what I need.&lt;/P&gt;

&lt;P&gt;When I try this I have:&lt;BR /&gt;
&lt;CODE&gt;index="my_index" host=prd-srv-00* | rex mode=sed "s/\"host\"/\"json_host\"/g" | timechart span=1m count by host  | fillnull  value=0&lt;/CODE&gt;&lt;BR /&gt;
This returns the correct number of events under the Events tab, it still isn't right, see the data below, you can see the event shows it is updated with &lt;CODE&gt;json_host&lt;/CODE&gt; instead of &lt;CODE&gt;host&lt;/CODE&gt;, but if you look at the bottom, it appears the rex mode change doesn't take affect until after the search: &lt;BR /&gt;
&lt;CODE&gt;{"time":"2019-04-05T21:50:09.925Z","severity":"INFO","duration":25.02,"db":10.23,"view":14.79,"status":200,"method":"GET","path":"/api/v4/project/1","params":[],"host":"my_server_1","ip":"1.2.3.4, 4.5.6.7","ua":null,"route":"/api/:version/projects/:id","user_id":12,"username":"smithers","queue_duration":4.35,"magic_calls":0}&lt;BR /&gt;
Show syntax highlighted&lt;BR /&gt;
host =    prd-srv-008     host =    my_server_1 method =    GET&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;So I guess at this point I"m looking for one of two things:&lt;BR /&gt;
1. Do the rex mode before the search, like I said now it appears to happen after&lt;BR /&gt;
2. In the Visualization hide a data series (my_server_1 in this case)&lt;BR /&gt;
I prefer number 1 as it will require fewer manual updates in the future.&lt;/P&gt;

&lt;P&gt;I'm continuing to play with this and do some searches, but so far I haven't had any luck.&lt;/P&gt;

&lt;P&gt;Thanks again for any help!&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 00:03:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392286#M1243</guid>
      <dc:creator>evbtbw92</dc:creator>
      <dc:date>2020-09-30T00:03:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392287#M1244</link>
      <description>&lt;P&gt;Thank you very much for the reply!  I put more details in the other answer (I had to pick one), but neither seems to work for me.&lt;/P&gt;

&lt;P&gt;When I tried your approach:&lt;BR /&gt;
&lt;CODE&gt;index="my_index" host=prd-srv-00* AND ext_host!="my_server_1" | rex field=_raw "\"host\"\:\"(?&amp;lt;ext_host&amp;gt;[^\"]*)" | timechart span=1m count by host  | fillnull  value=0&lt;/CODE&gt;&lt;BR /&gt;
It didn't return any values.  This seems to be due to the rex field update not happening until after the search.&lt;BR /&gt;
Any other thoughts?&lt;/P&gt;

&lt;P&gt;Thanks again for any help!&lt;/P&gt;</description>
      <pubDate>Mon, 08 Apr 2019 18:22:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392287#M1244</guid>
      <dc:creator>evbtbw92</dc:creator>
      <dc:date>2019-04-08T18:22:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392288#M1245</link>
      <description>&lt;P&gt;Ah yes totally forgot that, you need to search after rex.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index="my_index" host="prd-srv-00*" source="/var/log/my_program/http_json*"
| rex field=_raw "\"host\"\:\"(?&amp;lt;ext_host&amp;gt;[^\"]*)"
| search ext_host!="my_server_1" 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Apr 2019 18:28:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392288#M1245</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2019-04-08T18:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392289#M1246</link>
      <description>&lt;P&gt;That still returns no results.  My guess is because &lt;CODE&gt;ext_host&lt;/CODE&gt; is still set for each event, therefore when the search happens it is excluding all events.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Apr 2019 18:40:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392289#M1246</guid>
      <dc:creator>evbtbw92</dc:creator>
      <dc:date>2019-04-08T18:40:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392290#M1247</link>
      <description>&lt;P&gt;So how many events do you have which does not contain &lt;CODE&gt;my_server_1&lt;/CODE&gt; in your raw data ? If you are playing with only 1 sample event which you have provided then it will not return any result because you are searching for ext_host &lt;STRONG&gt;does not equal&lt;/STRONG&gt; to &lt;CODE&gt;my_server_1&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 23:59:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392290#M1247</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2020-09-29T23:59:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392291#M1248</link>
      <description>&lt;P&gt;I'm looking at a small number of events for testing, about 400.  Some have my_server_1 and some have my_server_2, so it would return values.&lt;/P&gt;

&lt;P&gt;The problem I have is that all the events come from the host (Splunk side) of prd-srv-008, but also have the other field set.&lt;BR /&gt;
In my graph, for the first minute it is showing:&lt;BR /&gt;
prd-srv-008 --COUNT: 432&lt;BR /&gt;
my_server_1 --COUNT: 320&lt;BR /&gt;
my_server_2 --COUNT: 112&lt;/P&gt;

&lt;P&gt;if you notice my_server_1 and my_server_2 always adds up to the total from prd-srv-008&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 00:03:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392291#M1248</guid>
      <dc:creator>evbtbw92</dc:creator>
      <dc:date>2020-09-30T00:03:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392292#M1249</link>
      <description>&lt;P&gt;Something interesting I found, if you are not trying to visualize the data it works as woodcock expliaed in number three, IE: &lt;CODE&gt;index="my_index" host=prd-srv-00* | rex mode=sed "s/\"host\"/\"json_host\"/g"&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;But as soon as you add the timechart, either in the query or by right clicking on host it adds the host field from the data in.  Very odd.&lt;/P&gt;

&lt;P&gt;Any help is much appreciated!&lt;/P&gt;</description>
      <pubDate>Mon, 08 Apr 2019 21:13:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392292#M1249</guid>
      <dc:creator>evbtbw92</dc:creator>
      <dc:date>2019-04-08T21:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392293#M1250</link>
      <description>&lt;P&gt;After fighting with this of a day and a half I finally found a solution that will work.  It has a bit more hard coding with the server names than I wanted, but good enough for now:&lt;BR /&gt;
&lt;CODE&gt;index="my_index" host::prd-srv-001 OR host::prd-srv-002 OR host::prd-srv-003 OR host::prd-srv-004 source="/var/log/my_program/http_json*" | timechart usenull=f useother=f cont=true span=1m count by host  | fillnull  value=0 | table _time, prd-srv-001, prd-srv-002, prd-srv-003, prd-srv-004&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;As noted in my previous comments, the rex mode=sed stuff didn't work when I included a timechart.  &lt;/P&gt;</description>
      <pubDate>Mon, 08 Apr 2019 23:51:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392293#M1250</guid>
      <dc:creator>evbtbw92</dc:creator>
      <dc:date>2019-04-08T23:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to handle search query when json data has host field?</title>
      <link>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392294#M1251</link>
      <description>&lt;P&gt;Be sure to click &lt;CODE&gt;Accept&lt;/CODE&gt; on your answer and &lt;CODE&gt;UpVote&lt;/CODE&gt; any other answers or comments that helped you get to this.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Apr 2019 02:27:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Enterprise/How-to-handle-search-query-when-JSON-data-has-host-field/m-p/392294#M1251</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2019-04-23T02:27:35Z</dc:date>
    </item>
  </channel>
</rss>

