Dashboards & Visualizations

set simple xml token from java script file

ssujin
Explorer

How to set a panel's depends tag value from java script with simple XML?

  <row>
    <panel>
      <chart depends="$check_status$">
        <title>Check Status</title>
        <searchString>......</searchString>
        <option name="charting.chart">line</option>
        <option name="charting.chart.nullValueMode">connect</option>
        <option name="charting.axisTitleX.text">Time</option>
        <option name="charting.axisTitleY.text">check status</option>
        <option name="charting.axisY.scale">linear</option>
        <option name="charting.axisY.minimumNumber">-1</option>
        <option name="charting.axisY.maximumNumber">3</option>
        <option name="charting.axisLabelsY.majorUnit">1</option>
        <!--<option name="link.visible">false</option>-->
      </chart>
    </panel>
  </row>

in the above example value for check_status has to set from java script.
Kindly provide example java script code.

0 Karma
1 Solution

kbarker302
Communicator

Below is a sample of JavaScript I use to set a token:

require([
    "splunkjs/mvc",
    "splunkjs/mvc/simplexml/ready!"
], function(mvc) {

    var tokens = mvc.Components.get("default");
    tokens.set("check_status", "true");              
});

Presumably your JavaScript would have some code to check whether or not to set the token. Also, depending on your logic, you may want to unset the token as well like this:

tokens.unset("check_status");    

Not sure how familiar you are with using JavaScript in Splunk, but your JavaScript file would go here (create the folders if they don't already exist):

$SPLUNK_HOME\etc\apps\<yourapp>\appserver\static

and you would reference it in your XML like this, by adding a script attribute to your form or dashboard tag:

<form script="yourjavascript.js">

View solution in original post

kbarker302
Communicator

Below is a sample of JavaScript I use to set a token:

require([
    "splunkjs/mvc",
    "splunkjs/mvc/simplexml/ready!"
], function(mvc) {

    var tokens = mvc.Components.get("default");
    tokens.set("check_status", "true");              
});

Presumably your JavaScript would have some code to check whether or not to set the token. Also, depending on your logic, you may want to unset the token as well like this:

tokens.unset("check_status");    

Not sure how familiar you are with using JavaScript in Splunk, but your JavaScript file would go here (create the folders if they don't already exist):

$SPLUNK_HOME\etc\apps\<yourapp>\appserver\static

and you would reference it in your XML like this, by adding a script attribute to your form or dashboard tag:

<form script="yourjavascript.js">

ssujin
Explorer

Is it possible to read the data from text file using javascript? Because while i was trying i got error.

0 Karma
Get Updates on the Splunk Community!

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...