Dashboards & Visualizations

Global Variable in splunk dashboard

reney44
Engager

how to set and get Global variable across splunk dashboard

there is no possible way to access variable in multiple search,
scope of variable include inside a search or macro

Tags (2)
0 Karma
1 Solution

nick405060
Motivator

To set, e.g:

<search>
  <query>
| makeresults | eval myvar="this is my variable" | table myvar
  </query>
  <preview>
    <set token="access_me">$result.myvar$</set>
  </preview>
</search>

To get:

... | eval this_is_somewhere_else_in_my_code="$access_me$"

However, remember that SimpleXML replaces the token with its value and THEN evaluates the SimpleXML. So if you were to not wrap your token in quotes like ... | eval this_is_somewhere_else_in_my_code=$access_me$, it would be evaluated as ... | eval this_is_somewhere_else_in_my_code=this is my variable instead of ... | eval this_is_somewhere_else_in_my_code="this is my variable" and therefore break your SimpleXML code.

Not wrapping your token in quotes is sometimes desirable, e.g. if you want to dynamically modify your SPL or SimpleXML code itself. However most of the time especially in a <query> stanza you are going to want to wrap your token in quotes.

View solution in original post

nick405060
Motivator

To set, e.g:

<search>
  <query>
| makeresults | eval myvar="this is my variable" | table myvar
  </query>
  <preview>
    <set token="access_me">$result.myvar$</set>
  </preview>
</search>

To get:

... | eval this_is_somewhere_else_in_my_code="$access_me$"

However, remember that SimpleXML replaces the token with its value and THEN evaluates the SimpleXML. So if you were to not wrap your token in quotes like ... | eval this_is_somewhere_else_in_my_code=$access_me$, it would be evaluated as ... | eval this_is_somewhere_else_in_my_code=this is my variable instead of ... | eval this_is_somewhere_else_in_my_code="this is my variable" and therefore break your SimpleXML code.

Not wrapping your token in quotes is sometimes desirable, e.g. if you want to dynamically modify your SPL or SimpleXML code itself. However most of the time especially in a <query> stanza you are going to want to wrap your token in quotes.

CarbonCriterium
Path Finder

@nick405060

This works for me, but my global variable is the sum of a very lengthy search.  

For as long as the search that creates the global variable is running, the search that uses starts, cancels itself, and then starts again, over and over and over.  All searches eventually run as expected, but its not pretty to look at.  

Is there a way to hold the dependent search from starting until the global variable has been created?

0 Karma

jpolvino
Builder

One "sneaky" way to do this is to provide the value in the dashboard URL:

https://your.splunk.url/en-US/app/search/dashboardname?yourtoken=yourtokenval

Then in your dashboard, you can use $yourtoken$ in searches and titles. Just be sure to URL encode youtokenval so it will parse properly.

This works great is you bookmark the dashboard, or want to test it out. If you just let people navigate to the dashboards, they won't get this additional key/val.

niketn
Legend

@reney44 tokens once they are set in a dashboard are available across the dashboard. So for the community to assist you better please elaborate as to what you are trying to do? How is the token being set? Is it in <init> section, or through search event handler, or through visualization drilldown? Which all places do you want to use the token in the dashboard? If you have already set the token what is the issue while using it in macro? Does your macro have a parameter so that it can accept the token as input?

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
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!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...