influxdata / influxdata/kapacitor

Get latest value and calculate yesterday same timestamp value

Open
#2,604 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
2.4k
Forks
479
Avg merge
4d 16h
Merged PRs (30d)
4

Description

I want to check "the value of my current hour" and "the value of previous hour" (within 7 days) every hour and compare whether the difference between the two exceeds the threshold I set.
The "current query" is the current value, and the "past query" is the value of the last search.
For example, I compare the value of 2 p.m. between 8/5 to 8/1, I have a specific field "timestamp" that will record my timestamp, which is a string type.
(ex.
2021-08-05T06:00:14Z 3.123195641829014 INFO
2021-08-04T06:00:22Z 3.648548028550934 INFO
2021-08-02T06:00:14Z 2.9963953099600076 INFO
2021-08-01T06:00:12Z 2.7667300671179333 INFO
)

And calculating the value of both 8/5 and 8/4, 3.1-3.6= -0.5
But when I checked my outputDB, it seems that I missed some steps, so I got the same value which I have filtered through the "offset".

The following are my tick scripts:

```
var groupBy = *

var name = 'CPU batch relative monitor'

var idVar = name

var message = '{{ if eq .Level "OK" }}recover:{{ else }}alert:{{ end }}{{ .ID }}

{{ if eq .Level "OK" }}recover time:{{ else }}alert time:{{ end }}{{ .Time.Local }}

host:{{ index .Tags "host" }}

level:{{ .Level}}

{{ if eq .Level "OK" }}recover value:{{ else }}alert value:{{ end }}{{ index .Fields "value" | printf "%0.0f"}}%
'

var idTag = 'alertID'

var levelTag = 'level'

var messageField = 'message'

var durationField = 'duration'

var outputDB = 'chronograf'

var outputRP = 'autogen'

var outputMeasurement = 'alerts'

var triggerType = 'relative'

var shift = 30m

var crit = 100

var period = 2m

var chatId = 'xxx'

var cron = '0 * * * *'

var current = batch
|query('''
select value from "normal"."autogen"."cpu" where ( cpu =~ /cpu-total/ ) AND ( timestamp =~ /T14:00/ ) order by desc
''')
.period(10m)
.cron(cron)
.groupBy(groupBy)

var past = batch
|query('''
select value from "normal"."autogen"."cpu" where ( cpu =~ /cpu-total/ ) AND ( level =~ /INFO/ )AND ( timestamp =~ /T14:00/ ) order by desc
''')
.offset(10m)
.period(7d)
.cron(cron)
.groupBy(groupBy)
|shift(10m)

var trigger = current
|join(past)
.as('current', 'past')
.tolerance(2m)
|eval(lambda: float(abs("current.value" - "past.value")))
.keep()
.as('value')
|alert()
.crit(lambda: "value" < crit)
.message(message)
.id(idVar)
.idTag(idTag)
.levelTag(levelTag)
.messageField(messageField)
.durationField(durationField)
.telegram()
.chatId(chatId)
.parseMode('Markdown')

trigger
|eval(lambda: float("value"))
.as('value')
.keep()
|influxDBOut()
.create()
.database(outputDB)
.retentionPolicy(outputRP)
.measurement(outputMeasurement)
.tag('alertName', name)
.tag('triggerType', triggerType)

trigger
|httpOut('output')
```

this is my influxdb output

```
> select value,level,timestamp from cpu where (host =~ /etc/) and (timestamp =~ /T14:00/) order by desc limit 4
name: cpu
time value level timestamp
---- ----- ----- ---------
2021-08-05T06:00:14Z 3.123195641829014 INFO 2021-08-05T14:00:14+08:00

2021-08-04T06:00:22Z 3.648548028550934 INFO 2021-08-04T14:00:22+08:00

2021-08-02T06:00:14Z 2.9963953099600076 INFO 2021-08-02T14:00:14+08:00

2021-08-01T06:00:12Z 2.7667300671179333 INFO 2021-08-01T14:00:12+08:00
```

and this is my kapacitor task status

```
DOT:
digraph test-job {
graph [throughput="0.00 batches/s"];

query2 [avg_exec_time_ns="0s" batches_queried="1" errors="0" points_queried="1" working_cardinality="0" ];
query2 -> shift3 [processed="1"];

shift3 [avg_exec_time_ns="2.771µs" errors="0" working_cardinality="0" ];
shift3 -> join5 [processed="1"];

query1 [avg_exec_time_ns="13.396881ms" batches_queried="1" errors="0" points_queried="1" working_cardinality="0" ];
query1 -> join5 [processed="1"];

join5 [avg_exec_time_ns="55.59µs" errors="0" working_cardinality="1" ];
join5 -> eval6 [processed="1"];

eval6 [avg_exec_time_ns="0s" errors="0" working_cardinality="1" ];
eval6 -> alert7 [processed="1"];

alert7 [alerts_inhibited="0" alerts_triggered="0" avg_exec_time_ns="2.39µs" crits_triggered="0" errors="0" infos_triggered="0" oks_triggered="0" warns_triggered="0" working_cardinality="1" ];
alert7 -> http_out10 [processed="0"];
alert7 -> eval8 [processed="0"];

http_out10 [avg_exec_time_ns="0s" errors="0" working_cardinality="0" ];

eval8 [avg_exec_time_ns="0s" errors="0" working_cardinality="0" ];
eval8 -> influxdb_out9 [processed="0"];

influxdb_out9 [avg_exec_time_ns="0s" errors="0" points_written="0" working_cardinality="0" write_errors="0" ];
}
```

When I am trying to run the tick scripts, that doesn't work, even this crit condition definitely should triggered.
I couldn't find any references. Any suggestions are appreciated. Thanks.
My kapacitor version is 1.5.7 and influxdb version is 1.8.3

Contributor guide

Open the contributing guide

Research direction

Review the supplied TICKscript, query definitions, task DOT output, and Kapacitor 1.5.7/InfluxDB 1.8.3 versions first. Reproduce the task behavior and determine why the alert condition and output nodes process no points; the issue is done only when the cause and an actionable fix or documented limitation are established.

Written by the indexing model from the issue text.

Assessment

Domain
observability-sre
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.