I’m having trouble getting a Kapacitor script working that monitors disk space. I need the disk path in the .id field of the alert node. However, this is going to Sensu, and Sensu doesn’t like slashes in the alert names. So I need to replace slashes in the disk path with underscores as a workaround.
I nearly have this working. I managed to use strReplace to do the character replacement. I also verified in the Kapacitor log that I could see all expected tags and fields, including the new “pathsub” field that contains the file path with the substituted underscores for the slashes.
However, I’m now getting hung up where the .id property is getting set for the alert node. Here’s the error I’m seeing.
[task_master:main] 2017/10/06 14:17:30 E! Stopped task: testdisk-alert alert4: template: id:1:32: executing "id" at <.Fields>: can't evaluate field Fields in type kapacitor.idInfo
I’m guessing that it’s complaining about the way I’m attempting to use the new “pathsub” field, though the message doesn’t explicitly say this. Hoping someone has some ideas on where to go from here. Below is the complete TICK script as it currently stands. (The warn and crit values are set artificially low just so I can actually generate some alerts once I finally get this working.) Thanks.
testdisk.tick:
//parameters
var warn = 1
var crit = 2
var unit = 30m
//Dataframe
var data = stream
|from()
.measurement('disk')
.groupBy('host', 'path')
|eval(lambda: strReplace("path", '/', '_', -1))
.as('pathsub')
.keep()
|log()
var alert = data
|alert()
.id('disk-space.TEST_IGNORE.{{ index .Fields "pathsub" }}.{{ index .Tags "host" }}')
.message('{{ .Level }}: the disk usage for path {{ index .Tags "path" }} of {{ index .Tags "cloud_application_name" }}-{{ index .Tags "instance_id" }} with ip address of {{ index .Tags "host"}} is currently at {{ index .Fields "used_percent" }}%')
.info(lambda: "used_percent" < warn)
.warn(lambda: "used_percent" > warn)
.crit(lambda: "used_percent" > crit)
.stateChangesOnly(unit)
//alert
alert
.log('/tmp/disk_alert_log.txt')
.sensu()
.source('{{ index .Tags "cloud_application_name" }}-{{ index .Tags "instance_id" }}')