Tick script unable to output data into the influxdb and raise alert

kapacitor

#1

Hi,

I am trying to create this logic

(1 hour average - 7 day minimum) > 1.5 * (7 day average - 7 day minimum)

After going through many tutorials , I manages to write some tick script

var hourlyAverage = batch
    |query('''SELECT mean(usage_user) AS mean_usage_user FROM "telegraf"."autogen"."cpu"  WHERE cpu = 'cpu-total' ''')
        .period(1h)
        .every(30s)

var weeklyAverage = batch
    |query('''SELECT mean(usage_user) AS mean_usage_user FROM "telegraf"."autogen"."cpu"  WHERE cpu = 'cpu-total' ''')
        .period(7d)
        .every(30s)

var weeklyMinimum = batch
    |query('''SELECT min(usage_user) AS min_usage_user FROM "telegraf"."autogen"."cpu"  WHERE cpu = 'cpu-total' ''')
        .period(7d)
        .every(30s)

var data = hourlyAverage
    |join(weeklyAverage, weeklyMinimum)
        .as('hourlyAverage', 'weeklyAverage', 'weeklyMinimum')

data
    |eval(lambda: "hourlyAverage.mean_usage_user" - "weeklyMinimum.min_usage_user")
        .as('hourlydiff')
        .keep()
    |eval(lambda: "weeklyAverage.mean_usage_user" - "weeklyMinimum.min_usage_user")
        .as('weeklydiff')
        .keep()
    |alert()
        .crit(lambda: "hourlydiff" > 1.5 * "weeklydiff")
        .message('Turn on alert occured ! ')
    |influxDBOut()
        .database('telegraf')
        .retentionPolicy('autogen')
        .measurement('cpu_stats')
        .precision('s')

Since no alert history is show. Because of influxDBOut, I tried to explore data using chronograf but in “Measurement & Tags” column i don’t see anything measurement named “cpu_stats” and i also tried “SELECT * FROM cpu_stats” inside database.

Thanks


#2

I debugged the TICK script using the |httpOut() node. And found problem being caused by “join” node because of tolerance limits.

var hourlyAverage = batch
    |query('''SELECT mean(usage_user) AS usage_user FROM "telegraf"."autogen"."cpu"  WHERE cpu = 'cpu-total' ''')
        .period(1m)
        .every(30s)
    |httpOut('step1')

var weeklyAverage = batch
    |query('''SELECT mean(usage_user) AS usage_user FROM "telegraf"."autogen"."cpu"  WHERE cpu = 'cpu-total' ''')
        .period(1m)
        .every(30s)
    |httpOut('step2')

var weeklyMinimum = batch
    |query('''SELECT min(usage_user) AS usage_user FROM "telegraf"."autogen"."cpu"  WHERE cpu = 'cpu-total' ''')
        .period(1m)
        .every(30s)
    |httpOut('step3')

var data = hourlyAverage
    |join(weeklyAverage, weeklyMinimum)
        .as('hourlyAverage', 'weeklyAverage', 'weeklyMinimum')
        .tolerance(2m)
    |httpOut('step4')

data
    |eval(lambda: "hourlyAverage.usage_user" - "weeklyMinimum.usage_user")
        .as('hourlydiff')
        .keep()
    |httpOut('step5')
    |eval(lambda: "weeklyAverage.usage_user" - "weeklyMinimum.usage_user")
        .as('weeklydiff')
        .keep()
    |httpOut('step6')
    |alert()
        .crit(lambda: "hourlydiff" > 1.5 * "weeklydiff")
        .message('')
        .post('http://192.168.0.1:1234')
        .header('Content-type', 'application/json')
    |influxDBOut()
        .database('telegraf')
        .retentionPolicy('autogen')
        .measurement('cpu_stats')