Hello, I am a new user to Kapacitor, and I am trying to develop a counter for a measurement based off of the measurement incrementing over time. To do this, I decided to use batch queries. I made two queries for the same measurement with a time offset applied to one of the queries, which in theory would allow for a comparison statement to be made between the two queries, as a ‘!=’ statement would trigger an alert if the two query values are different, due to the time offset. I’m sure I am just missing some minor syntax queues, as I am new to the service, but the script currently sends out alerts every 30s rather than every time the measurement increments. Does anyone have any ideas on how to implement this counter functionality?
// Template Author: Benji Smith
// // Kapacitor Notifications for doing alerts for counting mnemonics Template ////
// This script has been developed to work with only one counter mnemonic in order to get the SCENG team comfortable with using the TICKscript syntax
// Name the script, this will help identify errors in log messages if errors occur
var name = ‘_Counter_Mnemonic_Template_TEST’
var data = batch
// query the database for the mnemonic in a batch format, to keep the code latency to a minimum.
//
|query(‘SELECT “raw” AS “value” FROM “Random-Database”.“inf”.Measurement’)
.period(1m)
.every(30s)
.offset(360m)
.align()
.groupBy(*)
var pastdata = batch
// query the database for the mnemonic in a batch format, to keep the code latency to a minimum.
|query(‘SELECT “raw” AS “value” FROM “Random-Database”.“inf”.Measurement’)
.period(1m)
.every(30s)
.offset(362m)
.align()
.groupBy(*)
// This join command combines the query to make the two queries cross compatable
data
|join(pastdata)
.as(‘data’, ‘pastdata’)
// This alert command does the counter comparison and sends message to designated slack user/channel
data
|alert()
.crit(lambda: ‘data.value’ != ‘pastdata.value’)
.message('Measurement incremented to {{ index .Fields “data.value” }} ')
.slack()
.workspace(‘Direct To Me’)
I’ve made some updates to this script, but I am still not getting the script to work consistently:
// Template Author: Benji Smith
// // Kapacitor Notifications for doing alerts for counting mnemonics Template ////
// This script has been developed to work with only one counter mnemonic in order to get the SCENG team comfortable with using the TICKscript syntax
// Name the script, this will help identify errors in log messages if errors occur
var name = 'Name'
var data = batch
// query the database for the mnemonic in a batch format, to keep the code latency to a minimum.
|query('SELECT "raw" AS "value1" FROM "My-Database"."inf".Measurement')
.period(1m)
.every(1m)
.offset(-3m)
|last('value1')
.as('value1')
var pastdata = batch
// query the database for the mnemonic in a batch format, to keep the code latency to a minimum.
|query('SELECT "raw" AS "value2" FROM "My-Database"."inf".Measurement')
.period(1m)
.every(1m)
|last('value2')
.as('value2')
// This join command combines the query to make the two queries cross compatable
var joined_data = data
|join(pastdata)
.as('data', 'pastdata')
.fill(0.0)
.tolerance(15s)
// This alert command does the counter comparison and sends message to designated slack user/channel
// performance_error
joined_data
|alert()
.crit(lambda: "data.value1" != "pastdata.value2")
.message('The measurement has incrimented, value is now {{ index .Fields "data.value1" }} from {{ index .Fields "pastdata.value2" }} at {{ .Time }} ')
.slack()
.workspace('Direct To Benji')
` ` `
Join joins points with similar timestamps, here you seem to be trying to join new points with old points?
You need to use a shift node to either bring the timestamp forwards for the old data or move the timestamp backwards for the new data, before joining, I have made the example below.
// Name the script, this will help identify errors in log messages if errors occur
var name = 'Name'
var data = batch
// query the database for the mnemonic in a batch format, to keep the code latency to a minimum.
|query('SELECT "raw" AS "value1" FROM "My-Database"."inf".Measurement')
.period(1m)
.every(1m)
.offset(-3m)
|last('value1')
.as('value1')
|shift(3m) // this is what you need.
var pastdata = batch
// query the database for the mnemonic in a batch format, to keep the code latency to a minimum.
|query('SELECT "raw" AS "value2" FROM "My-Database"."inf".Measurement')
.period(1m)
.every(1m)
|last('value2')
.as('value2')
// This join command combines the query to make the two queries cross compatable
var joined_data = data
|join(pastdata)
.as('data', 'pastdata')
.fill(0.0)
.tolerance(15s)
// This alert command does the counter comparison and sends message to designated slack user/channel
// performance_error
joined_data
|alert()
.crit(lambda: "data.value1" != "pastdata.value2")
.message('The measurement has incrimented, value is now {{ index .Fields "data.value1" }} from {{ index .Fields "pastdata.value2" }} at {{ .Time }} ')
.slack()
.workspace('Direct To Benji')
Awesome! thanks. Do you know if the offset must have the ‘-’ in front of the time, or is that redundant?
the sign on it should affect the offset.
I see that now, I have the script working in a way that works! thanks for your help