stateChangesOnly does not work as expected

After creating an alert using Chronograf, I could not get stateChangesOnly to work as expected.

The alert (a host metric monitoring one) keeps alerting all the time with the same value, without changing at all.

Any ideas?

This is my current script:

var db = 'host_metrics'
var rp = 'metrics'
var measurement = 'disk'
var groupBy = []
var whereFilter = lambda: TRUE

var name = 'Disk Usage'

var idVar = name + ':{{.Group}}'
var message = 'Disk usage in host {{ index .Tags "host" }} is in {{.Level}} state because of value:  {{ index .Fields "value" }}%'
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 warn = 20
var crit = 30

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

var trigger = data
    |alert()
        .warn(lambda: "value" > warn)
        .crit(lambda: "value" > crit)
        .stateChangesOnly()
        .message(message)
        .id(idVar)
        .idTag(idTag)
        .levelTag(levelTag)
        .messageField(messageField)
        .durationField(durationField)
        .slack()

trigger
    |influxDBOut()
        .create()
        .database(outputDB)
        .retentionPolicy(outputRP)
        .measurement(outputMeasurement)
        .tag('alertName', name)
        .tag('triggerType', triggerType)

P.D: the low values are just for testing purposes :joy:

I tried your code. I modified it to monitor cpu usage since it’s easier to manually raise it for testing.

I added a group by host, an info criteria (it might not be required but I did not have time to test without it), a level field (to query the level and see it in a table) and removed slack since I do not have it setup. It’s working properly for me. I’m only getting a level change when it changes. Most of the time it’s not saving anything to InfluxDB. I’m on 1.4.

1 Like

Thanks for your response @samaust !

That was it, adding a group by host made the difference and it started behave correctly!