Sum of two values (bytes-in + bytes-out)

Hello,

I’m trying to add two values (bytes-in + bytes-out) in flux only as Grafana transform is not working as I need.
My query is :

import “date”
month = date.truncate(t: now(), unit: 1mo)
from(bucket: “telegraf/autogen”)
|> range(start: month)
|> filter(fn: (r) => r["_measurement"] == “snmp_interfaces” and r[“hostname”] == “$hostname” and r._field =~ /bytes-/)
|> last ()
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: “_value”)
|> map(
fn: (r) => ({
_time: r._time,
_measurement: r._measurement,
_field: “totalConso”,
_value: int(v: bytes-in + bytes-out)
})
)

I’v tried to do it like it’s wrote in documentation, but that didn’t work as expected.
I have this error : invalid: compilation failed: error at @13:34-13:36: invalid expression: invalid token for primary expression: IN error at @13:39-13:44: invalid expression @13:34-13:36: in
Does someone could help me with that ?

Regards

Hello,

I “up” this topic because I haven’t found the solution to this problem.
Can someone could help me with this ?

Regards,

Following advice on slack, I changed the query like that :
import "date"
month = date.truncate(t: now(), unit: 1mo)
from(bucket: "telegraf/autogen")
|> range(start: month)
|> filter(fn: (r) => r["_measurement"] == "snmp-interface")
|> filter(fn: (r) => r["host"] == "${hostname}")
|> filter(fn: (r) => r["host"] == "${Interface}}")
|> filter(fn: (r) => r["_field"] == "bytes-in" or r["_field"] == "bytes-out")
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => ({ _value: r.bytes-in + r.bytes-out }))
|> sum()
But I still have de same error

Regards

Hey,

In telegraf, I moved from bytes-in to bytesIn and rewrite my queries.
Everything is working now.
I don’t know if it’s the “-” ou “in” which if the problem.

Regards,

Vincent

Probably it would work if you used r["bytes-in"] + r["bytes-out"].
The dot notation is just syntactic sugar and it doesn’t work for all column names as you can see.

Here is what you had just written with different spacing. Spot the problem?
_value: r.bytes - in+r.bytes - out

@ypnos.

Thank you for the tips. I’ll keep it for futur.
I still have a flux problem.I have 2 queries : The first one show the traffic evolution during the current month


The second should be the sum of in and out bytes.

As you can see, I should have around 6GB, but I got 102.

My queries are:
import "date"
month = date.truncate(t: now(), unit: 1mo)
from(bucket: "telegraf/autogen")
  |> range(start: month)
  |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
  |> filter(fn: (r) => r["hostname"] == "${hostname}")
  |> filter(fn: (r) => r["if-name"] == "${Interface}")
  |> filter(fn: (r) => r["_field"] == "bytesIn")
  |> map(fn: (r) => ({r with _value: r._value / 1073741824}))
  |> increase()
import "date"
month = date.truncate(t: now(), unit: 1mo)
from(bucket: "telegraf/autogen")
  |> range(start: month)
  |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
  |> filter(fn: (r) => r["hostname"] == "${hostname}")
  |> filter(fn: (r) => r["if-name"] == "${Interface}")
  |> filter(fn: (r) => r["_field"] == "bytesOut")
  |> map(fn: (r) => ({r with _value: r._value / 1073741824}))
  |> increase()

For the evolution and

import "date"
month = date.truncate(t: now(), unit: 1mo)
from(bucket: "telegraf/autogen")
  |> range(start: month)
  |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
  |> filter(fn: (r) => r["hostname"] == "${hostname}")
  |> filter(fn: (r) => r["if-name"] == "${Interface}")
  |> filter(fn: (r) => r["_field"] == "bytesIn" or r["_field"] == "bytesOut")
  |> last ()
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({r with _value: (r.bytesIn + r.bytesOut) / 1073742824 }))
  |> sum ()

For the sum.
What I doing wrong with the second query ?

Regards,

What do you use last() for?

As you are calculating the overall sum, you also don’t need to pivot and calculate sum of in+out in the map. This should work as well:

import "date"
month = date.truncate(t: now(), unit: 1mo)
from(bucket: "telegraf/autogen")
  |> range(start: month)
  |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
  |> filter(fn: (r) => r["hostname"] == "${hostname}")
  |> filter(fn: (r) => r["if-name"] == "${Interface}")
  |> filter(fn: (r) => r["_field"] == "bytesIn" or r["_field"] == "bytesOut")
  |> group()
  |> sum()
  |> map(fn: (r) => ({r with _value: r._value / 1073741824}))

Hello Ypnos,

Thank you for your reply and your help.
In fact, the way how I try to get values is wrong.
bytesIn and bytesOut are counter, so they are alway incremented.
With $month variable, I’m able to know from how much this counter was incremented since the begin of the month.
This is the queries I’m using (thank for Slack help):

import "date"
month = date.truncate(t: now(), unit: 1mo)
from(bucket: "telegraf/autogen")
  |> range(start: month)
  |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
  |> filter(fn: (r) => r["hostname"] == "${hostname}")
  |> filter(fn: (r) => r["if-name"] == "${Interface}")
  |> filter(fn: (r) => r["_field"] == "bytesIn")
  |> map(fn: (r) => ({r with _value: r._value / 1073741824}))
  |> increase()

And the same for bytesOut.
If I want to know the sum of bytesIn or bytesOut, I can use my calculator :smiley: or use the Grafana Transform (not working as good as it should do) or use Flux to make the sum of consumption In and Out. This is where I fail.
could I put the result of query in variable and then sum these queries ?

regards

@Tiki10 You should only need one query for the evolution line graph. This should work:

import "date"

month = date.truncate(t: now(), unit: 1mo)

from(bucket: "telegraf/autogen")
    |> range(start: month)
    |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
    |> filter(fn: (r) => r["hostname"] == "${hostname}")
    |> filter(fn: (r) => r["if-name"] == "${Interface}")
    |> filter(fn: (r) => r["_field"] == "bytesIn" or r["_field"] == "bytesOut")
    |> increase()
    |> map(fn: (r) => ({r with _value: r._value / 1073741824}))

And for the sum total of both bytesIn and bytesOut since the beginning of the month:

import "date"

month = date.truncate(t: now(), unit: 1mo)

from(bucket: "telegraf/autogen")
    |> range(start: month)
    |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
    |> filter(fn: (r) => r["hostname"] == "${hostname}")
    |> filter(fn: (r) => r["if-name"] == "${Interface}")
    |> filter(fn: (r) => r["_field"] == "bytesIn" or r["_field"] == "bytesOut")
    |> increase()
    |> group()
    |> sum()
    |> map(fn: (r) => ({r with _value: r._value / 1073741824}))

This is a combination of your current query and what @ypnos is suggesting.