Question about calculating percentage

Im using telegraf to monitor my lxd instance. I use metrics “disk-size” and “disk-size-free” to calculate the percentage of used disk. Then I want to be alerted if disk usage is too high. there is no percentage metric so I have to do the calculation myself. Whats recommended here? For now I to the calculation in the influxdb(2). Now is it recommended to calculate the percentage already on the host and send the calculated metrics via telegraf? or is it better to do the calculation on influx?

Now I put the query on a dashboard and have it refresh automatically. Im looking for the most efficient way of doing things.

thank you

Hello @DCieD,
It’s up to you!
For a calculation this simple, I don’t think you can really go wrong either way. It’s easy to set up a task that does this in influxdb v2. An advantage here is that you’ll familiarize yourself with Flux and the task system in case you want to do more calculations in the future.
Your flux script would look like:

// Specify the bucket to query from
from(bucket: "your_bucket")
  // Specify the range; adjust according to your needs
  |> range(start: -1h)
  // Filter for the specific measurement(s) you're interested in
  |> filter(fn: (r) => 
    r._measurement == "your_measurement" &&
    (r._field == "field1" || r._field == "field2")
  // Pivot to align field1 and field2 values on the same row for each point
  |> pivot(
      columnKey: ["_field"],
      valueColumn: "_value"
  // Calculate the percentage
  |> map(fn: (r) => ({
      _time: r._time,
      _measurement: r._measurement,
      // Replace with appropriate tag keys if needed
      tag: r.tag,
      percentage: (r.field1 / r.field2) * 100
  // Optionally, you can filter out rows where the calculation may not be valid
  |> filter(fn: (r) => exists r.percentage and r.percentage >= 0)
  // Output the result; adjust to your preferred destination
  |> to(bucket: "your_output_bucket", org: "your_org")

However this is also easily achievable with telegraf. You could use the starlark processor plugin:

Here’s a math example:

You could also use the execd processor plugin.

The advantage here is you’re already using telegraf so this might be the simplest approach for you.

If it were me I’d probably use telegraf since you’re already using it.

1 Like