Splunk Search

How do I create a token from a hidden search in SimpleXML

joxley
Path Finder

I want to have a hidden search in my simple XML dashboard

<search id="base">
   <query>index=_internal | stats count</query>
   <earliest>-60m</earliest>
   <latest>now</latest>
   <set token="event_count">count</set>
</search>

which populates the $event_count$ token for use in later on in the dashboard, e.g. as the title of a panel

<panel>
    <title>My events ($event_count$)</title>
    <table>
0 Karma
1 Solution

jeffland
SplunkTrust
SplunkTrust

Searches produce variable results (i.e. tables, single rows, events, ...). The fact that your particular search returns only one row and one column does not mean you can just use that result and put in in a token. How would you expect a search such as index=_internal to populate a token?

What you can do however is extract that search result in Javascript and edit your title accordingly. To do that, change your XML to contain the id of the panel for later reference with jquery:

<panel id="panel_id">
    <title>My events </title>
<table>

and add the following code to your .js:

var $ = require("jquery");
var search = splunkjs.mvc.Components.getInstance("base");
var myResults = search.data("results");
var panel = $("#panel_id").find("h2").first(); // If neccessary, adjust this to the right title
myResults.on("data", function() {
    resultArray = myResults.data().rows;
    panel.append(resultArray[0][0];);
});

View solution in original post

jeffland
SplunkTrust
SplunkTrust

Searches produce variable results (i.e. tables, single rows, events, ...). The fact that your particular search returns only one row and one column does not mean you can just use that result and put in in a token. How would you expect a search such as index=_internal to populate a token?

What you can do however is extract that search result in Javascript and edit your title accordingly. To do that, change your XML to contain the id of the panel for later reference with jquery:

<panel id="panel_id">
    <title>My events </title>
<table>

and add the following code to your .js:

var $ = require("jquery");
var search = splunkjs.mvc.Components.getInstance("base");
var myResults = search.data("results");
var panel = $("#panel_id").find("h2").first(); // If neccessary, adjust this to the right title
myResults.on("data", function() {
    resultArray = myResults.data().rows;
    panel.append(resultArray[0][0];);
});
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...