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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

What Is Splunk? Here’s What You Can Do with Splunk

Hey Splunk Community, we know you know Splunk. You likely leverage its unparalleled ability to ingest, index, ...

Level Up Your .conf25: Splunk Arcade Comes to Boston

With .conf25 right around the corner in Boston, there’s a lot to look forward to — inspiring keynotes, ...

Manual Instrumentation with Splunk Observability Cloud: How to Instrument Frontend ...

Although it might seem daunting, as we’ve seen in this series, manual instrumentation can be straightforward ...