Hello,
I’m uisng Kapacitor templates to make my alerts and know i can use syntax like {{if eq .Level “WARNING”}} show something {{else if eq .Level “CRITICAL”}} show something else {{end}} I also know i can include the values of my variables in my scripts with string concatination, however the two don’t seem to work in conjunction.
I need to send a message out based on the level and then show the value for the variable.
So i have a message like
var message = '{{index .Tags "host_name"}} Nutanix Alert: Memory Usage over the threshold ' + string(threshold) + '% for over {{if eq .Level "WARN"}} ' + string(warn) + '{{else if eq .Level "CRIT"}}' + string(crit) + ' Minutes. {{end}}'
I also tried changing it to + float(threshold) but it didn’t seem to do anything. Whenever i define the template i get the following error
invalid TICKscript: Failed to handle left node: Failed to handle right node: Failed to handle node: Given node type is not valid evaluation node: <nil>
Tick script:
var db = ‘Database’
var rp = 'autogen'
var measurement string
var groupBy = ['host_name','host_uuid','cluster_uuid','host','vm_name','vm_uuid']
var host string
var priority string
var threshold lambda
var whereFilter = lambda: ("host_name" == host OR "vm_name" == host)
// The threshold for the alert to begin processing and triggering the alert. An alert is generated when warning/critical time values are exceeded
// This value is a percentage: 98% should be entered as 98.0
var name = 'NutanixMemoryUsage' + priority
var idVar = name + '{{index .Tags "host_name"}} {{ index .Tags "vm_name"}}'
var idTag = 'alertID'
var levelTag = 'level'
var messageField = 'message'
var durationField = 'duration'
var outputDB = 'alerts'
var outputRP = 'autogen'
var outputMeasurement = 'NutanixAlerts'
var triggerType = 'threshold'
// Critical alert: time based, alert after the specified time (in 1 minute units).
// 10 Minutes should be entered as 10
var crit lambda
// Warning alert: time based, alert after the specified time (in 1 minute units).
// 5 Minutes should be entered as 5
var warn lambda
var message = '{{index .Tags "host_name"}} Nutanix Alert: Memory Usage over the threshold ' + string(threshold) + '% for over {{if eq .Level "WARN"}} ' + string(warn) + '{{else if eq .Level "CRIT"}}' + string(crit) + ' Minutes. {{end}}'
var data = stream
|from()
.database(db)
.retentionPolicy(rp)
.measurement(measurement)
.groupBy(groupBy)
.where(whereFilter)
|eval(lambda: ("memory_usage_ppm"),
lambda: ("memory_usage_ppm" / 10000))
.as('memory_usage_ppm','memory_used')
.keep('memory_usage_ppm', 'memory_used')
|stateDuration(lambda: "memory_used" >= threshold)
.unit(1m)
.as('warn_duration')
|stateDuration(lambda: "memory_used" >= threshold)
.unit(1m)
.as('crit_duration')
var trigger = data
|alert()
.crit(lambda: "crit_duration" >= crit)
.warn(lambda: "warn_duration" >= warn)
.stateChangesOnly()
.message(message)
.id(idVar)
.idTag(idTag)
.levelTag(levelTag)
.messageField(messageField)
.durationField(durationField)
.log('/tmp/' + name + '.log')
trigger
|eval(lambda: float("memory_used"),
lambda: float("memory_usage_ppm"))
.as('memory_used','memory_usage_ppm')
.keep()
|influxDBOut()
.create()
.database(outputDB)
.retentionPolicy(outputRP)
.measurement(outputMeasurement)
.tag('alertName', name)
.tag('Priority', priority)
.tag('triggerType', triggerType)
trigger
|httpOut('output')
I’m not sure whether I’ve got something wrong here or whether or not it’s possible to combine both the template functions and concatenation. Is there a way to do this?
Thanks
Phil