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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...