Solution: Create custom HTTP body and access .tags and values. using row-template

Main challenge was to access .tags and fields from template to create custom json body.
ANd with alert-template you cannot access fields outside {{.Message}}, atleast what I had noticed.

Check this out for more details:


or

Solution

Use |alert node & httpPost() node in your tickscript Batch/Stream it works with both:
Note you can also add custom tags… by using default() node.

|alert()
.details(‘details’)
.crit(lambda: “cpuusage” > crit)
.message(message)
.id(idVar)
.idTag(idTag)
.levelTag(‘level’)
.messageField(messageField)
.durationField(durationField)
|default()
.tag(‘folder_name’, ‘new_sd343f’)
.tag(‘dashboard_name’, ‘cpu_ds’)
.tag(‘metric_name’, ‘cpuusage’)
|httpPost()
.endpoint(‘vnexttopulse’)
.captureResponse()
2)
Use this in your kapacitor.conf

[[httppost]]
endpoint = “someendpoint”
url = “http://localhost:8081
headers = {Content-Type = “application/json”}
row-template = ‘{“records”:[{“source”: “from_my_app”, “event_class”: “Kapacitor”, “node”: “{{index .Tags “host”}}”, “type”: “{{.Name}}”, “metric_name”: “{{index .Tags “metric_name”}}”, “resource”: “{{index .Tags “instance”}}”, “additional_info”: “Host: {{index .Tags “host”}} Instance: {{index .Tags “instance”}}”, “description”: “Browse Deeplink: (https://testsite.com/{{index .Tags “folder_name”}}/{{index .Tags “dashboard_name”}}?refresh=1m&orgId=1&var-host={{index .Tags “host”}}) {{range .Values}} Alert_Time: {{index . “time”}} Last_Measure: {{index . “cpuusage”}}” {{end}}}]}’
[httppost.basic-auth]
username = “xxxxxxxxxx”
password = “yyyyyyyyyyyyy”

1 Like

@Ashish_Sikarwar,
Thanks for sharing! This is cool.

You are welcome @Anaisdg

The only piece left was to figure out how to access Severity or {{.Level}} , .levelTag(‘level’)

I tried like this as well

var get_severity = ‘{{.Level}}’

and then used it under
|default()
.tag(‘get_level’, get_severity)

But it prints nothing.

Hello @Ashish_Sikarwar,
Unfortunately I don’t think that’s possible with TICKscript.

Good News for accessing Level
In alert-template-file you can access it like this:
"severity": {{ if eq (.Level | print) "WARNING" }}"4"{{else if eq (.Level | print) "CRITICAL"}}"1" {{else if eq (.Level | print) "OK"}}"5"{{ end }}

Using row-template (inline)

"severity":{{range .Values}} {{ if (eq .message "WARNING")}}"3"{{else if (eq .message "CRITICAL")}}"1"{{else if (eq .message "OK")}}"0" {{ end }}{{end}}

Here you would wonder, why i have used .message… well i assigned .Level to .Message.

From tickscript:
var message = ‘{{.Level}}’

The I used it under alert node
var trigger = data
|alert()
.details(instangetag)
.crit(lambda: “value” > crit)
.warn(lambda: “value” < crit)
.message(message)

Also i am able to access tags in alert-template:

“my_tag”: “{{range .Data.Series}}{{index .Tags “instance”}}{{end}}”,

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.