Splunk Search

How to use external variables in search command?

kjycls
Engager

application.js
value = Splunk.util.getParameter("name");
localStorage.setItem("name",value);

I saved parameter value by using html5 localStorage.(similar session value control)

in application.js file, I can get name value -> localStorage.getItem("name")

in this case, I want use this value to splunk advancedXML search variable.

How can I do??

Tags (3)
0 Karma

sideview
SplunkTrust
SplunkTrust

You could use a NullModule and then write a tricky patch from application.js, or you could make life a bit easier and use Sideview Utils and its "CustomBehavior" module.

Step 1) download Sideview Utils 2.1.X from http://sideviewapps.com/apps/sideview-utils

Step 2) put the SideviewUtils module on your view. Put it at the top next to AccountBar and AppBar. It's invisible and it'll be out of the way up there.
ie:

<module name="AccountBar" layoutPanel="appHeader" />
<module name="AppBar" layoutPanel="appHeader" />
<module name="SideviewUtils" layoutPanel="appHeader" />

Step 3) Put a CustomBehavior module into the page, at a point in the Advanced XML downstream from which you want the value to be available. Given it a "customBehavior" param of "getNameFromLocalStorage"

ie:

<module name="CustomBehavior">
  <param name="customBehavior">getNameFromLocalStorage</param>

  <module name="HTML">
    <param name="html"><![CDATA[
      The name from localStorage is $name$
    ]]></param>
  </module>      
</module>

Step 4) in application.js, you define the customBehavior like so.

Sideview.utils.declareCustomBehavior("getNameFromLocalStorage", function(customBehaviorModule) {
    customBehaviorModule.getModifiedContext = function() {
        var context = this.getContext();
        context.set("name", localStorage.getItem("name"));
        return context;
    }
});

Step 5) downstream from the CustomBehavior module you can use the Sideview Search module or HTML module to reference $name$, and at runtime it will be the value from localStorage. (See the example I listed in Step 3, above)

If you want to use the trickier way with the NullModule patch, it's not that bad in this case, and I can spell that out for you too..

0 Karma

sideview
SplunkTrust
SplunkTrust

I added more code samples to walk you through it, and I also fixed an embarassing typo in the customBehavior javascript. Sorry about that.

0 Karma

kjycls
Engager

Thank you~ Can I know Stop3 syntax?

0 Karma
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

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 ...