Dashboards & Visualizations

How do I run a script from a button on a dashboard?

Motivator

Hi

I want to run the script from a button on a dashboard.

Running the RUNTEST.sh from as an alert is working (Alerttest).

Do i call the alert that will then call the .sh or can i call the .sh directly?

Example of button. But i have no idea how to run the RUNTEST.sh (splunk/bin/scripts/RUNTEST.sh)

   <html>
        <button class="btn" data-token-json="{&quot;execute_save_test&quot;:&quot;     RUN_TEST.sh     }">RUN TEST SCRIPT</button>
      </html>

Thanks in advance
Rob

0 Karma

Contributor

Hi,

I will try to explain what I have done. Probably it will be helpful and you can customize according to your need.

  1. I am in windows so I try solve this issue using batch file. I think the same concept can be applied to .sh file as well.
  2. I saved my below batch file in a place where I have execute permission. In this case I saved in my desktop with name sid.bat

    @echo off
    color 0a
    mode 1000

    echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random% > C:\Users\s.d.chakraborty\Desktop\test.txt

  3. Then I created a custom command like below with name test.py and saved in my apps bin folder as usual.

    import subprocess
    subprocess.call([r'C:\Users\s.d.chakraborty\Desktop\sid.bat'])

  4. I configured commands.conf like below,

    [testcmd]
    filename = test.py
    enableheader = true
    outputheader = true
    requiressrinfo = true
    supports
    getinfo = true
    supportsmultivalues = true
    supports
    rawargs = true

  5. The I created a sample dashboard with checkbox to run the script. Here you can replace with your HTML button logic as well for token setting.

alt text

<form>
  <label>demo</label>
  <search>
    <query>| testcmd | eval ignore = $form.tokQuery$</query>
  </search>
  <fieldset submitButton="false">
    <input type="checkbox" token="dummy" searchWhenChanged="true">
      <label></label>
      <choice value="run">Run</choice>
      <delimiter> </delimiter>
        <change>
           <set token="form.tokQuery">nothing</set>
         </change>
    </input>
  </fieldset>
</form>

if everything goes file it should create a file called test.txt.

Please let me know hot it goes for you. I tested it once and it works for me.

Sid

Engager

Hi @sdchakraborty,

I tried to disable supports_getinfo in commands.conf and it works perfectly 🙂
Thank you for your detailed steps above. God bless you!

0 Karma

Contributor

No worries mate. If this resolves your issue please accept it as answer. Have a nice day.

Sid

Engager

Hi @sdchakraborty ,

Tried this and it works for me. However, there were multiple results for my script. The script inside the *.py will send an email to specific recipients, but there were 3 emails being sent upon single execution. Even if I only tried this on adhoc search command, the error is still the same. See below.

01-28-2019 15:04:31.435 ERROR script - Getinfo probe failed for external search command ''
01-28-2019 15:04:31.435 ERROR SearchPhaseGenerator - Fallback to two phase search failed:Error in 'script': Getinfo probe failed for external search command ''
01-28-2019 15:04:31.436 ERROR SearchOrchestrator - Error in 'script': Getinfo probe failed for external search command ''

This is my first time to use .py. Am I missing some config needed for python script? Thanks in advance.

0 Karma

SplunkTrust
SplunkTrust

@robertlynch2020 how about you set a token on HTML button click using Splunk JS and Simple XML JS extension (may be the token value can be shell script name itself). Then run the SPL with script command, which will execute only when the token is set (i.e. on HTML button click).

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

SplunkTrust
SplunkTrust

@robertlynch2020 Is your problem resolved? If so, please accept the answer to help future readers.

---
If this reply helps you, an upvote would be appreciated.
0 Karma