Kapacitor: Include field in message

Hi,

I use the following tick script. Alerting works fine, but I want to include a field “info” which is part of the triggering data point in the measurement. It’s filled with some text and it’s not printed in my alert message, it’s always empty. Seems like I can only include fields and tags which are part of the query. I thought I can access it with {{ index .Fields “info” }}, but it does not work.

How can I include fields in my message details which belong to the datapoint?

var db = 'influx_db'

var rp = 'autogen'

var measurement = 'monitoring'

var groupBy = ['myid', 'environment']

var whereFilter = lambda: TRUE

var name = 'monitoring_email'

var idVar = name + '-{{.Group}}'

var message = '*** {{.Level}} ***  -  {{ index .Tags "mylocation" }}-{{ index .Tags "environment" }}-{{ index .Tags "myid" }}'

var idTag = 'alertID'

var levelTag = 'level'

var messageField = 'message'

var durationField = 'duration'

var outputDB = 'chronograf'

var outputRP = 'autogen'

var outputMeasurement = 'alerts'

var triggerType = 'threshold'

var details = '
<p>*** <b>{{.Level}}</b> *** </p>

<p> Info: {{ index .Fields "info" }} </p>

<p>
<table>
  <tr>
    <td>ID:</td>
    <td>{{ index .Tags "myid" }}</td>
  </tr>
  <tr>
    <td>Location:</td>
    <td>{{ index .Fields "mylocation" }}</td>
  </tr>
  <tr>
    <td>Environment:</td>
    <td>{{ index .Tags "environment" }}</td>
  </tr>
</table>
</p>

<p> Time: {{ .Time.Local.Format "Mon, Jan 2 2006 at 15:04:05 UTC" }} </p>
'

var crit = 0

var data = stream
    |from()
        .database(db)
        .retentionPolicy(rp)
        .measurement(measurement)
        .groupBy(groupBy)
        .where(whereFilter)
    |eval(lambda: "result_code")
        .as('value')

var trigger = data
    |alert()
        .crit(lambda: "value" > crit)
        .message(message)
        .id(idVar)
        .idTag(idTag)
        .levelTag(levelTag)
        .messageField(messageField)
        .durationField(durationField)
        .details(details)
        .stateChangesOnly()
        .email()
        .to('me@company.com')

trigger
    |eval(lambda: float("value"))
        .as('value')
        .keep()
    |influxDBOut()
        .create()
        .database(outputDB)
        .retentionPolicy(outputRP)
        .measurement(outputMeasurement)
        .tag('alertName', name)
        .tag('triggerType', triggerType)

trigger
    |httpOut('output')

If ‘info’ appears in your measurement you might need to evaluate it first.

swap 
 |eval(lambda: "result_code")
        .as('value')

to

 |eval(lambda: "result_code",
       lambda: "info")
      .as('value','info')

That did the trick. Thank you!

1 Like

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