Dashboards & Visualizations

Button to run splunk query

astatrial
Contributor

Hello

I'm pretty new to the Dashboard creating in splunk, and to xml in general.

I want to know if there is a way to create a button that will run a splunk search on a click.

I'm trying to create a button that if is clicked, it will create a lookup file by the next search :

| inputlookup "template.csv" | outputlookup "newlookup"."csv" 

I know that there is an option to use java script through onclick but the problem is that the query is in splunk syntax and i don't think java script can parse it.

Also can i create this button to appear only if there is a value in other input ?

Can anyone help?

1 Solution

chrisyounger
SplunkTrust
SplunkTrust

Setup your dashboard like this:

<dashboard script="run_action.js">
  <label>Test Action</label>
  <row>
    <panel>
      <html>
        <button class="btn btn-primary button1">Run search!</button>
      </html>
    </panel>
  </row>
</dashboard>

the add run_action.js to <app_folder>/appserver/static of the

         require([
            "jquery",
            "splunkjs/mvc/searchmanager",
            "splunkjs/mvc/simplexml/ready!"
          ], function(
              $,
              SearchManager
          ) {
              var mysearch = new SearchManager({
                  id: "mysearch",
                  autostart: "false",
                  search: "|makeresults| outputlookup myfile" 
              });
              $(".button1").on("click", function (){
                  var ok = confirm("Are you sure?");
                  if (ok){
                      mysearch.startSearch();
                      alert('attempted restart!');
                  } //else {
                  //    alert('user did not click ok!');
                  //}
              });
         });

The dashboard needs to be in the same splunk "app" as the javascript file.

View solution in original post

michael_vi
Path Finder

The dashboard executes when the page refreshes + when the button is pressed.

How to disable the "refresh" page?

0 Karma

chrisyounger
SplunkTrust
SplunkTrust

Setup your dashboard like this:

<dashboard script="run_action.js">
  <label>Test Action</label>
  <row>
    <panel>
      <html>
        <button class="btn btn-primary button1">Run search!</button>
      </html>
    </panel>
  </row>
</dashboard>

the add run_action.js to <app_folder>/appserver/static of the

         require([
            "jquery",
            "splunkjs/mvc/searchmanager",
            "splunkjs/mvc/simplexml/ready!"
          ], function(
              $,
              SearchManager
          ) {
              var mysearch = new SearchManager({
                  id: "mysearch",
                  autostart: "false",
                  search: "|makeresults| outputlookup myfile" 
              });
              $(".button1").on("click", function (){
                  var ok = confirm("Are you sure?");
                  if (ok){
                      mysearch.startSearch();
                      alert('attempted restart!');
                  } //else {
                  //    alert('user did not click ok!');
                  //}
              });
         });

The dashboard needs to be in the same splunk "app" as the javascript file.

robertlynch2020
Influencer

HI 

I have done below and replaced the SPL with the following.

BUt the .sh scripts run when I load the page the first time, how can I stop that from happening.

I would like it to work just when I press the button.

Regards

Robert

A simple | runshellscript Test_Script123.sh 1 1 1 1 1 1 1 1 . 

 

 

 

 

 

require([
            "jquery",
            "splunkjs/mvc/searchmanager",
            "splunkjs/mvc/simplexml/ready!"
          ], function(
              $,
              SearchManager
          ) {
              var mysearch = new SearchManager({
                  id: "mysearch",
                  autostart: "false",
                  search: "| runshellscript Test_Script123.sh 1 1 1 1 1 1 1 1" 
              });
              $(".button1").on("click", function (){
                  var ok = confirm("Are you sure?");
                  if (ok){
                      mysearch.startSearch();
                      alert('attempted restart!');
                  } //else {
                  //    alert('user did not click ok!');
                  //}
              });
         });

 

 

0 Karma

rodrigrc
Explorer

I have also the same problem.. I want search particular parameter inside the host file with the selectable date drop down

Below source code only shows one field and also not giving any output.

++++++++++

<form>
<label>ODSEE Backend SEARCH</label>
<fieldset autoRun="false" submitButton="true">
<input type="text" token="twguid">
<label>Search ID</label>
<default>put your search here</default>
<prefix>'twguid'="</prefix>
<suffix>"</suffix>
</input>
</fieldset>
<row>
<panel>
<table>
<search>
<query>
index=auth_odsee* host="*" conn=* twguid=*
| search $twguid$
| table '_date' 'twguid' 'host' </query>
<earliest>-10m</earliest>
<latest>now</latest>
</search>
</table>
</panel>
</row>
</form>

+++++++++++++++++++++

0 Karma

astatrial
Contributor

I did it but for some reason it doesn't work.
Nothing happens when i press the button.

Also is there a way to send a token (of one of the other inputs in the dashboard) to that java script ?

Thank you very much!!

0 Karma

astatrial
Contributor

I managed to get it work, based on your answer.
Thanks !

0 Karma
Get Updates on the Splunk Community!

Splunk is Nurturing Tomorrow’s Cybersecurity Leaders Today

Meet Carol Wright. She leads the Splunk Academic Alliance program at Splunk. The Splunk Academic Alliance ...

Part 2: A Guide to Maximizing Splunk IT Service Intelligence

Welcome to the second segment of our guide. In Part 1, we covered the essentials of getting started with ITSI ...

Part 1: A Guide to Maximizing Splunk IT Service Intelligence

As modern IT environments continue to grow in complexity and speed, the ability to efficiently manage and ...