Using variables in the whereFilter in TICK Script


Is it possible to achieve something like the following?

Currently i have a web console that will let me define alerts in Kapacitor (it not chronograf, its custom) I’m using templates and setting some template variables.

We’ve built our console in a way so that when we select the hosts a new alert is defined for each host. When we define an alert we apply it to a group of hosts. An example being I have 3 SQL servers. I monitor these for memory consumption. I group these 3 servers together and apply my alert template. so we would end up with

What happens now is an alert is generated for EVERY server that is sending in memory usage statistics whether it was selected or not - not so good when i only need those 3 hosts to use the script.

What I’d like to know is how i can include the “host” variable as part of my whereFilter ? i know i can include the variables and values using string concatination but I’m not sure how to use them in the whereFilter.

Normally we would manually add the host names to the whereFilter however we’re trying to build a dynamic way to define and manage our alerts so would like to avoid having to type in the host names into each alert.

I’d want something like this

var host_variable lambda
var whereFilter = lambda: (“vmname” == host_variable)

Is it possible to include variables in this way?

Hello @philb,
Yes. You can use variables like that var name = "my_host" var example = stream |from(.wherever) |where(lambda: "host" == name)

Hi @Anaisdg thanks for getting back to me.We weren’t able to get this working. It turns out the issues was with how we were passing the information back to the script. We’ve got it now :smiley:

1 Like

Would you mind sharing your solution for future community users to access?

Hi @Anaisdg, the issue wasn’t with Kapacitor but with the web front end we’re building to create and manage our alert templates. I can’t really give much info about that.

The syntax you mentioned above is the right one, we were just missing the value for the “host” variable.

1 Like