TICKScript maintain current state of a group of TAGs

kapacitor

#1

Hi All,

From the networking side, using influx to record port data of network switches via snmp. Looking to gain a sort of “last used” Field. essentially, it boils down to: if the max% used is higher than 0%, record the current time as last used, or set the field to the previous value. The idea is to keep the current state of play in a low retention DB, so that searches across the data are quick as the usual target for this style of search is the interface that has not been used in the longest time.

var ints = batch
    |query('select max(calcPercentIn) AS inbound, max(calcPercentOut) AS outbound, last(unusedSince3) as unusedSince3 FROM "telegraf"."autogen"."interface" WHERE ifDescr =~ /Ethernet/ AND hostname =~ /INT-FW/ ')
        .period(5m)
        .every(1m)
        .groupBy(time(5m), *)

var calcs = ints
    |default()
        .field('unusedSince3', '0')
//    |eval(lambda: if("inbound" > 0 OR "outbound" > 0, '1', '2'))
    |eval(lambda: if("inbound" > 0 OR "outbound" > 0, unixNano("time"), unixNano("unusedSince3")))
        // if in/out greater than 0, set last used to now, else bring forward the last value
        // actually need to do 3 if statements here, as mixing the timestamps and strings is not allowed.
        // 	1. set if it was used (bool)
        //  2. if 1 = true, send the time to influx
        //  3. if 1 = false, send the phrase "never"
        .as('unusedSince3')
//    |eval(lambda: if("temp.value" == 1, unixNano("time"), unixNano("time")))
//       .as('unusedSince3')
//    |eval(lambda: if("temp.value" == 2, 'unusedSince3', 'unusedSince3'))
//       .as('unusedSince3')
//        .keep('hostname', 'ifDescr', 'agent_host', 'unusedSince3')
    |influxDBOut()
        .database('telegraf')
        .retentionPolicy('autogen')
        .measurement('interface')
        .precision('m')