Variables in resources

Hello,

I’m currently migrating from sensu on VMs to sensu-go on k8s with some agents on VMs.
I try to group all resources yaml files (checks, assets…) and load/update/prune them via sensuctl create command.

I’m facing a problem with my http checks since resources are not load from server agent anymore.
With older sensu versions, checks where generated in /etc/sensu/conf.d/ on agent side, so I could get sites name directly according to server.

What is the best way with sensu-go to get specific data from agents and add them to resources ?
I think I need something like that with a variable :

---
type: CheckConfig
api_version: core/v2
metadata:
 name: http-<site.name>
spec:
 command: check-http.rb -h 127.0.0.1 -H 'Host: <site.url>' -p /ping  --response-code 200 --user-agent sensu_check -k -w --require-bytes 4 -P 443 -s
...

With a array variable, a check should be created by element.

I could do it with API from agent, but I would like have all my resources in the same place if possible.

Otherwise I must find a solution to get needed data and generate all resources, then play with subscriptions.

Thanks

Hey!

So what you can do is add annotations to your agent configuration and then reference those annotations as tokens substitutions in your check commands.

For example:
if in your agent.yml you have this

annotations:
  check_http_url: "http://www.sensu.io"

and then restart your agent, you’ll have access to the ‘.annotations.check_http_url’ token inside the check
Here’s an example of a check command that uses this token with a fallback default if the annotation doesn’t exist:

 check-http.rb --url {{ .annotations.check_http_url | default "http://localhost:80/" }}

We’re building up a collection of reusable templates for a lot of the existing Sensu checks using Bonsai assets in the github/sensu-community monitoring-checks repo

Take a look at the http check template and see if that helps with your migration.

One extra thing if you want to quickly check what annotations an agent has defined, you can query sensuctl entity info with a little jq magic:

sensuctl entity info centos-dev-agent --format json |jq .metadata.annotations
{
  "check_http_url": "http://www.sensu.io",
  "latte_flavor": "pumpkin_spice"
}

Thanks for your interesting answer ! I did not see tokens substitutions, it is nice.

Is all Go template package is available or just a part for the token substitutions implementation ?

So I’m not sure I fully understand the question, so let me re-state what I think you said.

When using token substitution do you have access to the full logic of the go template?

I think the answer is… yes. I personally haven’t tried to do complicated go template logic in the scope of the check command, so if you try it and run into problems please let us know.

-jef

that was the question, thx :slight_smile:

After some tests, there are too limitations to achieve what I want :

  • Not possible to use templating on other attribute than command
  • Not possible to set an array in the key’s value annotation

It would be great to support token registration to entire check resource, do you plan it ?

Anyway, I will still use token registration to manage thresholds !

so it might help if I had a better understanding of why you want to template outside of the check command? Since the check command is ultimately the thing you run on the agent and the rest is just sort of dressing around that to help sensu schedule and process the check result, maybe what you want here is a proxy request pattern.

There might have a good idea for a feature request, but i think we discuss a bit more about what you are specifically trying to accomplish by using the go templating so I can understand the scope of the functionality that would be needed in a feature request.

In fact I want simply use the same check multiple times on the same entity.

In my case, in current the Sensu configuration, we define a set of http checks in /etc/sensu/conf.d/ of each agent per vhost on the server. So just the name and the domain change in each resource. I’m trying to do something like that, but it is maybe not the good approach with Sensu Go.

My idea was to set an annotation :

annotations:
  'clients': ["client1","client2","client3"]

and something like that in a resource check (I don’t know how really works Go template) :

{{ range .annotations.clients  item }}
---
type: CheckConfig
api_version: core/v2
metadata:
  name: http-{{item}}
  namespace: {{ .namespace }}
spec:
  command: "check-http.rb -h 127.0.0.1 -H 'Host: {{ item }}.company.com' -p /ping  --response-code 200 --user-agent sensu_check -k -w --require-bytes 4 -P 443 -s"
...

As for the template outside of the check command, it maybe could be useful for namespace or also handlers to set different ones according entity criticality.

Hey!

So the pattern that may work best for you right now to accomplish the same goal is to use a proxy request and and create a different proxy entity each with the annotation value you want.

I think this guide as an example on using proxy entities to accomplish what you want:
https://docs.sensu.io/sensu-go/latest/guides/monitor-external-resources/