Convert or truncate values with kapacitor when sending alert message

Hello,

I have a measurement “Exchange_RoundTrip” that records the time taken to deliver an email message from my exchange server to a remote email account. The value should be in milliseconds, however when the data is recorded it is done so in nanoseconds.

When the alert is fired off, the value shows as nano seconds. Is there a way i can conver this in my TICK script and only output the value in MS?

I tried the following: {{ index .Fields “deliveryTime” | printf “%.2f”}}

this just adds zero’s on the end of the value. The value is stored as an integer (for some reason unknown to me), so i convert it to a float.

My script:

var db = 'REMOVED'

var rp = 'REMOVED'

var measurement = 'Exchange_Roundtrip'

var groupBy = ['host']

var whereFilter = lambda: TRUE

var period = 30s

var every = 30s

var name = 'Exchange RoundTrip	'

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

var message = ''

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 crit = 150.0

var data = stream
    |from()
        .database(db)
        .retentionPolicy(rp)
        .measurement(measurement)
        .groupBy(groupBy)
        .where(whereFilter)
    |window()
        .period(period)
        .every(every)
        .align()
    |mean('delivery_time_seconds')
        .as('delivery_Time')
    |eval(lambda: float("delivery_Time"))
        .as('deliveryTime')

var trigger = data
    |alert()
        .crit(lambda: "deliveryTime" > crit)
        // .stateChangesOnly()
        .message(message)
        .details(details)
        .id(idVar)
        .idTag(idTag)
        .levelTag(levelTag)
        .messageField(messageField)
        .durationField(durationField)
        .email('pb@email.co.uk')

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

trigger
    |httpOut('output')

Delivery Time: 10719370.00MS - i know that the time is actually 107MS so even if i can’t convert the value if there is a way to truncate it. I found a truncate option in the documentation but it looks as though this applies to the time stamp.

Appreciate i’d be better off recording the data in ms anyway but i’m just working with the data i have.

Thanks,

Phil

Hi,

I guess this should work:

|eval(lambda: float(“delivery_Time”) / 1000000.0)
.as(‘deliveryTime’)

Did you try like so?

Hope it helps!

Hi Arturo,

I hadn’t tried that, but i’ve just given it ago. Looking at the data in Grafana and the values I’m getting into my alerts it looks to have worked! Grafana converts it to MS for me and the results match.

Thank you Arturo, it’s much appreciated.

Phil

Nice!! Yeah, Grafana make that for us…Although in Kapacitor we need to make the math!!

Nice to hear that the answer works!!!