Grafana

Kilobytes per second

From my load logger project I collect the number of bytes in/out of my network devices. If I want to graph that in grafana using my MySQL data source (as KB/s) then I have to use SQL to munge the numbers. This gets what I need in SQL:

SELECT
      id,
      created_on,
      n4_r, 
      ((n4_r - @lastValue) / 1024 ) AS AvgKbPerSec,
      @lastValue := n4_r
   FROM
      data_logger,
      ( SELECT @lastValue := 0 ) SQLVars
   ORDER BY
created_on,id

Then to get it working with Grafana, I change it to this (wrapped in an extra select, because graphana only wants one value to graph):

SELECT TIME, KbPerSec FROM (
SELECT
      created_on AS "time",
      IF(@lastValue = 0, 0, ((n4_r - @lastValue) / 1024 )) AS Kb,
      @lastValue := n4_r
   FROM
      data_logger,
      ( SELECT @lastValue := 0 ) SQLVars
 WHERE $__timeFilter(created_on)
   ORDER BY
created_on) AS newtable 
 WHERE $__timeFilter(TIME)
   ORDER BY
TIME