<?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 How to change search query dynamically based on input in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305615#M160151</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;

&lt;P&gt;I have 2 queries for each dataservice,&lt;/P&gt;

&lt;P&gt;Query 1&lt;/P&gt;

&lt;P&gt;index=db_connect source = "db2_*.log"  earliest=-1d|dedup TBSP_NAME, DB_NAME | convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS Timestamp | stats sum(TBSP_SIZE_BYTES) as "TBSP_SIZE_BYTES" by Timestamp , DB_NAME  | stats first(Timestamp) as "as of", first(TBSP_SIZE_BYTES)  as "LATEST_TBSP_SIZE_BYTES" by DB_NAME | eval TBSP_SIZE_GB=round(LATEST_TBSP_SIZE_BYTES/(1024 * 1024 * 1024),2) |table  DB_NAME, TBSP_SIZE_GB, "as of"| rename DB_NAME as Database  |rename TBSP_SIZE_GB as "Database Size (GB)"&lt;/P&gt;

&lt;P&gt;Query 2&lt;/P&gt;

&lt;P&gt;index=db_connect  source = "mssql_*_dbgrowth.log" | dedup TBSP_NAME, DB_NAME |convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS Timestamp &lt;BR /&gt;
|stats sum(DATA_KB) as "DATA_KB" by Timestamp , DB_NAME&lt;BR /&gt;&lt;BR /&gt;
|stats first(Timestamp) as "as of", first(DATA_KB)  as "LATEST_DATA_KB" by DB_NAME &lt;BR /&gt;
|eval DATA_GB=round(LATEST_DATA_KB/(1024 * 1024 ),2) &lt;BR /&gt;
|table  DB_NAME, DATA_GB, "as of"| rename DB_NAME as Database  |rename DATA_GB as "Database Size (GB)"&lt;/P&gt;

&lt;P&gt;Is it possible to build search query based on a input string ? Say i have a input which will get value as "DB2" or MSSQL".&lt;/P&gt;

&lt;P&gt;If my input value is DB2,&lt;BR /&gt;
then Query 1 must run&lt;BR /&gt;
If my  input value is MSSQL&lt;BR /&gt;
then Query 2 must run&lt;/P&gt;

&lt;P&gt;Could you please let me know.&lt;/P&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 14:51:33 GMT</pubDate>
    <dc:creator>sangs8788</dc:creator>
    <dc:date>2020-09-29T14:51:33Z</dc:date>
    <item>
      <title>How to change search query dynamically based on input</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305615#M160151</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;

&lt;P&gt;I have 2 queries for each dataservice,&lt;/P&gt;

&lt;P&gt;Query 1&lt;/P&gt;

&lt;P&gt;index=db_connect source = "db2_*.log"  earliest=-1d|dedup TBSP_NAME, DB_NAME | convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS Timestamp | stats sum(TBSP_SIZE_BYTES) as "TBSP_SIZE_BYTES" by Timestamp , DB_NAME  | stats first(Timestamp) as "as of", first(TBSP_SIZE_BYTES)  as "LATEST_TBSP_SIZE_BYTES" by DB_NAME | eval TBSP_SIZE_GB=round(LATEST_TBSP_SIZE_BYTES/(1024 * 1024 * 1024),2) |table  DB_NAME, TBSP_SIZE_GB, "as of"| rename DB_NAME as Database  |rename TBSP_SIZE_GB as "Database Size (GB)"&lt;/P&gt;

&lt;P&gt;Query 2&lt;/P&gt;

&lt;P&gt;index=db_connect  source = "mssql_*_dbgrowth.log" | dedup TBSP_NAME, DB_NAME |convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS Timestamp &lt;BR /&gt;
|stats sum(DATA_KB) as "DATA_KB" by Timestamp , DB_NAME&lt;BR /&gt;&lt;BR /&gt;
|stats first(Timestamp) as "as of", first(DATA_KB)  as "LATEST_DATA_KB" by DB_NAME &lt;BR /&gt;
|eval DATA_GB=round(LATEST_DATA_KB/(1024 * 1024 ),2) &lt;BR /&gt;
|table  DB_NAME, DATA_GB, "as of"| rename DB_NAME as Database  |rename DATA_GB as "Database Size (GB)"&lt;/P&gt;

&lt;P&gt;Is it possible to build search query based on a input string ? Say i have a input which will get value as "DB2" or MSSQL".&lt;/P&gt;

&lt;P&gt;If my input value is DB2,&lt;BR /&gt;
then Query 1 must run&lt;BR /&gt;
If my  input value is MSSQL&lt;BR /&gt;
then Query 2 must run&lt;/P&gt;

&lt;P&gt;Could you please let me know.&lt;/P&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 14:51:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305615#M160151</guid>
      <dc:creator>sangs8788</dc:creator>
      <dc:date>2020-09-29T14:51:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to change search query dynamically based on input</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305616#M160152</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;try something like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;form&amp;gt;
  &amp;lt;label&amp;gt;test&amp;lt;/label&amp;gt;
  &amp;lt;fieldset submitButton="false"&amp;gt;
    &amp;lt;input type="radio" token="db"&amp;gt;
      &amp;lt;label&amp;gt;Choice DB&amp;lt;/label&amp;gt;
      &amp;lt;choice value="source = db2_.log earliest=-1d"&amp;gt;DB2&amp;lt;/choice&amp;gt;
      &amp;lt;choice value="source = mssql__dbgrowth.log | rename DATA_KB AS TBSP_SIZE_BYTES"&amp;gt;MSSQL&amp;lt;/choice&amp;gt;
      &amp;lt;default&amp;gt;source = db2_.log earliest=-1d&amp;lt;/default&amp;gt;
    &amp;lt;/input&amp;gt;
  &amp;lt;/fieldset&amp;gt;
  &amp;lt;row&amp;gt;
    &amp;lt;panel&amp;gt;
      &amp;lt;event&amp;gt;
        &amp;lt;search&amp;gt;
          &amp;lt;query&amp;gt;
              index=db_connect 
              $db$
             | dedup TBSP_NAME, DB_NAME 
             | convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS Timestamp 
             | stats sum(TBSP_SIZE_BYTES) as "TBSP_SIZE_BYTES" by Timestamp , DB_NAME 
             | stats first(Timestamp) as "as of", first(TBSP_SIZE_BYTES) as "LATEST_TBSP_SIZE_BYTES" by DB_NAME 
             | eval DATA_GB=round(LATEST_TBSP_SIZE_BYTES/(1024*1024),2) 
             | table DB_NAME, TBSP_SIZE_BYTES, "as of"
             | rename DB_NAME as Database TBSP_SIZE_GB as "Database Size (GB)"
          &amp;lt;/query&amp;gt;
          &amp;lt;earliest&amp;gt;$earliest$&amp;lt;/earliest&amp;gt;
          &amp;lt;latest&amp;gt;$latest$&amp;lt;/latest&amp;gt;
          &amp;lt;sampleRatio&amp;gt;1&amp;lt;/sampleRatio&amp;gt;
        &amp;lt;/search&amp;gt;
        &amp;lt;option name="count"&amp;gt;20&amp;lt;/option&amp;gt;
        &amp;lt;option name="list.drilldown"&amp;gt;full&amp;lt;/option&amp;gt;
        &amp;lt;option name="list.wrap"&amp;gt;1&amp;lt;/option&amp;gt;
        &amp;lt;option name="maxLines"&amp;gt;5&amp;lt;/option&amp;gt;
        &amp;lt;option name="raw.drilldown"&amp;gt;full&amp;lt;/option&amp;gt;
        &amp;lt;option name="rowNumbers"&amp;gt;0&amp;lt;/option&amp;gt;
        &amp;lt;option name="table.drilldown"&amp;gt;all&amp;lt;/option&amp;gt;
        &amp;lt;option name="table.sortDirection"&amp;gt;asc&amp;lt;/option&amp;gt;
        &amp;lt;option name="table.wrap"&amp;gt;1&amp;lt;/option&amp;gt;
        &amp;lt;option name="type"&amp;gt;list&amp;lt;/option&amp;gt;
      &amp;lt;/event&amp;gt;
    &amp;lt;/panel&amp;gt;
  &amp;lt;/row&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 07:07:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305616#M160152</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2017-07-13T07:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to change search query dynamically based on input</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305617#M160153</link>
      <description>&lt;P&gt;This wouldnt work because Query2 has a small change in dedup compared to Query 1.&lt;/P&gt;

&lt;P&gt;Query 1  - dedup TBSP_NAME, DB_NAME&lt;BR /&gt;
Query2 - dedup DB_NAME&lt;/P&gt;

&lt;P&gt;And also the Query 1 TBSP_SIZE_BYTES  is in bytes and in Query 2 DATA_KB is in kb.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 14:51:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305617#M160153</guid>
      <dc:creator>sangs8788</dc:creator>
      <dc:date>2020-09-29T14:51:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to change search query dynamically based on input</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305618#M160154</link>
      <description>&lt;P&gt;In your choices you can put also other, modify in this way your choices&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;form&amp;gt;
   &amp;lt;label&amp;gt;test&amp;lt;/label&amp;gt;
   &amp;lt;fieldset submitButton="false"&amp;gt;
     &amp;lt;input type="radio" token="db"&amp;gt;
       &amp;lt;label&amp;gt;Choice DB&amp;lt;/label&amp;gt;
       &amp;lt;choice value="source = db2_.log earliest=-1d | dedup TBSP_NAME, DB_NAME"&amp;gt;DB2&amp;lt;/choice&amp;gt;
       &amp;lt;choice value="source = mssql__dbgrowth.log | rename DATA_KB AS TBSP_SIZE_BYTES | dedup DB_NAME"&amp;gt;MSSQL&amp;lt;/choice&amp;gt;
       &amp;lt;default&amp;gt;source = db2_.log earliest=-1d dedup TBSP_NAME, DB_NAME&amp;lt;/default&amp;gt;
     &amp;lt;/input&amp;gt;
   &amp;lt;/fieldset&amp;gt;
   &amp;lt;row&amp;gt;
     &amp;lt;panel&amp;gt;
       &amp;lt;event&amp;gt;
         &amp;lt;search&amp;gt;
           &amp;lt;query&amp;gt;
               index=db_connect 
               $db$
              | convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS Timestamp 
              | stats sum(TBSP_SIZE_BYTES) as "TBSP_SIZE_BYTES" by Timestamp , DB_NAME 
              | stats first(Timestamp) as "as of", first(TBSP_SIZE_BYTES) as "LATEST_TBSP_SIZE_BYTES" by DB_NAME 
              | eval DATA_GB=round(LATEST_TBSP_SIZE_BYTES/(1024*1024),2) 
              | table DB_NAME, TBSP_SIZE_BYTES, "as of"
              | rename DB_NAME as Database TBSP_SIZE_GB as "Database Size (GB)"
           &amp;lt;/query&amp;gt;
           &amp;lt;earliest&amp;gt;$earliest$&amp;lt;/earliest&amp;gt;
           &amp;lt;latest&amp;gt;$latest$&amp;lt;/latest&amp;gt;
           &amp;lt;sampleRatio&amp;gt;1&amp;lt;/sampleRatio&amp;gt;
         &amp;lt;/search&amp;gt;
         &amp;lt;option name="count"&amp;gt;20&amp;lt;/option&amp;gt;
         &amp;lt;option name="list.drilldown"&amp;gt;full&amp;lt;/option&amp;gt;
         &amp;lt;option name="list.wrap"&amp;gt;1&amp;lt;/option&amp;gt;
         &amp;lt;option name="maxLines"&amp;gt;5&amp;lt;/option&amp;gt;
         &amp;lt;option name="raw.drilldown"&amp;gt;full&amp;lt;/option&amp;gt;
         &amp;lt;option name="rowNumbers"&amp;gt;0&amp;lt;/option&amp;gt;
         &amp;lt;option name="table.drilldown"&amp;gt;all&amp;lt;/option&amp;gt;
         &amp;lt;option name="table.sortDirection"&amp;gt;asc&amp;lt;/option&amp;gt;
         &amp;lt;option name="table.wrap"&amp;gt;1&amp;lt;/option&amp;gt;
         &amp;lt;option name="type"&amp;gt;list&amp;lt;/option&amp;gt;
       &amp;lt;/event&amp;gt;
     &amp;lt;/panel&amp;gt;
   &amp;lt;/row&amp;gt;
 &amp;lt;/form&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 08:05:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305618#M160154</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2017-07-13T08:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to change search query dynamically based on input</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305619#M160155</link>
      <description>&lt;P&gt;Thanks. Let me try that out.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 08:49:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305619#M160155</guid>
      <dc:creator>sangs8788</dc:creator>
      <dc:date>2017-07-13T08:49:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to change search query dynamically based on input</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305620#M160156</link>
      <description>&lt;P&gt;@sangs8788, you can code the &lt;CODE&gt;&amp;lt;change&amp;gt;&lt;/CODE&gt; event of the input to set whatever tokens you need&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    &amp;lt;fieldset submitButton="false"&amp;gt;
      &amp;lt;input type="radio" token="db"&amp;gt;
        &amp;lt;label&amp;gt;Choice DB&amp;lt;/label&amp;gt;
        &amp;lt;choice value="db2"&amp;gt;DB2&amp;lt;/choice&amp;gt;
        &amp;lt;choice value="mssql"&amp;gt;MSSQL&amp;lt;/choice&amp;gt;
        &amp;lt;default&amp;gt;db2&amp;lt;/default&amp;gt;
        &amp;lt;change&amp;gt;
           &amp;lt;condition value="db2"&amp;gt;
               &amp;lt;set token="queryString"&amp;gt;
                   index=db_connect source = "db2_.log"
| dedup TBSP_NAME, DB_NAME 
| convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS Timestamp 
| stats sum(TBSP_SIZE_BYTES) as "TBSP_SIZE_BYTES" by Timestamp , DB_NAME 
| stats first(Timestamp) as "as of", first(TBSP_SIZE_BYTES) as "LATEST_TBSP_SIZE_BYTES" by DB_NAME 
| eval TBSP_SIZE_GB=round(LATEST_TBSP_SIZE_BYTES/(1024 1024 * 1024),2) 
| table DB_NAME, TBSP_SIZE_GB, "as of"
| rename DB_NAME as Database 
| rename TBSP_SIZE_GB as "Database Size (GB)"
               &amp;lt;/set&amp;gt;
           &amp;lt;/condition&amp;gt;
           &amp;lt;condition value="mssql"&amp;gt;
               &amp;lt;set token="queryString"&amp;gt;
                     index=db_connect source = "mssql__dbgrowth.log" 
| dedup TBSP_NAME, DB_NAME 
| convert timeformat="%Y-%m-%d %H:%M:%S" ctime(_time) AS Timestamp 
| stats sum(DATA_KB) as "DATA_KB" by Timestamp , DB_NAME 
| stats first(Timestamp) as "as of", first(DATA_KB) as "LATEST_DATA_KB" by DB_NAME 
| eval DATA_GB=round(LATEST_DATA_KB/(1024 1024 ),2) 
| table DB_NAME, DATA_GB, "as of"
| rename DB_NAME as Database 
| rename DATA_GB as "Database Size (GB)"
               &amp;lt;/set&amp;gt;
           &amp;lt;/condition&amp;gt;
        &amp;lt;/change&amp;gt;
      &amp;lt;/input&amp;gt;
    &amp;lt;/fieldset&amp;gt;
    &amp;lt;row&amp;gt;
      &amp;lt;panel&amp;gt;
        &amp;lt;table&amp;gt;
          &amp;lt;search&amp;gt;
            &amp;lt;query&amp;gt;$queryString$&amp;lt;/query&amp;gt;
            &amp;lt;earliest&amp;gt;-1d@d&amp;lt;/earliest&amp;gt;
            &amp;lt;latest&amp;gt;now&amp;lt;/latest&amp;gt;
          &amp;lt;/search&amp;gt;
        ...
        ...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Ideally you should create a macro for search with various parameters (so that you can handle situations like different DBs, size, time range, fields etc.) and then set only the parameters through the change event. I have just used complete search for a example of change event. You can set several tokens using &lt;STRONG&gt;set&lt;/STRONG&gt; tag (PS: &lt;STRONG&gt;eval&lt;/STRONG&gt; tag is also available which will allow you to set tokens based on some conditions.) Splunk documentation for reference: &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/Viz/EventHandlerReference#Event_handler_element"&gt;https://docs.splunk.com/Documentation/Splunk/latest/Viz/EventHandlerReference#Event_handler_element&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 09:08:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305620#M160156</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-07-13T09:08:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to change search query dynamically based on input</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305621#M160157</link>
      <description>&lt;P&gt;If this answer satiesfies your request, please accept it.&lt;BR /&gt;
Bye and good luck.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 09:19:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305621#M160157</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2017-07-13T09:19:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to change search query dynamically based on input</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305622#M160158</link>
      <description>&lt;P&gt;This does work though it cannot work in my scenario since i have other dataservice with different queries. So it is not possible to have a common query for all. Below posted query seems to work for my usecase. Thanks for your time.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 09:58:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-change-search-query-dynamically-based-on-input/m-p/305622#M160158</guid>
      <dc:creator>sangs8788</dc:creator>
      <dc:date>2017-07-13T09:58:25Z</dc:date>
    </item>
  </channel>
</rss>

