How to save to monitor check multiple 'rows' by same _level

Hello!
I will try to explain by example:

import "influxdata/influxdb/monitor"
import "array"

array.from(rows: [
  {_time: 2020-01-01T00:00:00Z, _field: "cpu", _value: 95.0, server: "server_1", _measurement: "example"},
  {_time: 2020-01-01T00:01:00Z, _field: "cpu", _value: 90.0, server: "server_2", _measurement: "example"},
  {_time: 2020-01-01T00:02:00Z, _field: "cpu", _value: 2.0, server: "server_3", _measurement: "example"}
])
|> group(columns: ["_measurement"])
|> monitor.check(
        crit: (r) => r._value >= 90.0 ,
        ok: (r) => r._value < 10.0,
        messageFn: (r) => "Server -> ${r.server} with cpu ${r._value}%",
        data: {
            _check_name: "Degradation (percentage)",
            _check_id: "degradation_percent",
            _type: "threshold",
            tags: {},
        },
    )
    |> group() 

In this case I have two servers with crit level (cpu 95 and 90). But, only the one will be added to monitor and send notification.
How I can add both?

One of the tricks is to create a separate alert for each of the servers. But I don’t like this solution. :pensive:

1 Like

Hello @Amerousful,
Welcome!
I suggest using the pivot function to pivot your data:

pivot(rowKey:["_time"], columnKey: ["server"], valueColumn: "_value")

and then establishing your conditions like:

crit: (r) => r.server_1 >= 90.0 and r.server_2 >=90

Let me know if that helps!
PS the utility of pivoting your data is described in this section (fieldsAsCol() function is usually used instead of pivot). This resource might be helpful to you:

1 Like

Hello @Anaisdg!
Sorry for my late answer… Unfortunately, it isn’t the unified solution. Because I will need to add each server. Also, server_1 may be less than 90 and server_2 not.

So, I don’t see another way, only create separate checks. However, thanks for your help!

@Anaisdg Hey! I found solution with a little trick :slight_smile:
I just removed _measurement and renamed server to _measurement. And then created message by _source_measurement

import "influxdata/influxdb/monitor"
import "array"

array.from(rows: [
  {_time: 2020-01-01T00:00:00Z, _field: "cpu", _value: 95.0, server: "server_1", _measurement: "example"},
  {_time: 2020-01-01T00:01:00Z, _field: "cpu", _value: 90.0, server: "server_2", _measurement: "example"},
  {_time: 2020-01-01T00:02:00Z, _field: "cpu", _value: 2.0, server: "server_3", _measurement: "example"}
])

|> drop(columns: ["_measurement"])
|> rename(columns: {server: "_measurement"})
|> group(columns: ["_measurement"])
|> monitor.check(
        crit: (r) => r._value >= 90.0,
        messageFn: (r) => "Server -> ${r._source_measurement} with cpu ${r._value}%",
        data: {
            _check_name: "Degradation (percentage)",
            _check_id: "degradation_percent",
            _type: "threshold",
            tags: {},
        },
    )
1 Like