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!

Why You Can't Miss .conf25: Unleashing the Power of Agentic AI with Splunk & Cisco

The Defining Technology Movement of Our Lifetime The advent of agentic AI is arguably the defining technology ...

Deep Dive into Federated Analytics: Unlocking the Full Power of Your Security Data

In today’s complex digital landscape, security teams face increasing pressure to protect sprawling data across ...

Your summer travels continue with new course releases

Summer in the Northern hemisphere is in full swing, and is often a time to travel and explore. If your summer ...