influxdb sum first value metric of different series but same time interval for grafana graph

谁都会走 提交于 2019-12-22 09:56:27

问题


I am using influxdb grafana and collectd and i want to display memory usage graph.

The collectd give me this metrics value for memory and save it in influxdb

influxdb/memory/memory-buffered   
influxdb/memory/memory-cached    
influxdb/memory/memory-free    
influxdb/memory/memory-used

i want to display in grafana graph the total memory so i need to sum the following metrics:

memory_buffered + memory_cached + memory_free + memory_used

How can I query this in influxdb or in grafana ?


回答1:


I think this is not possible at moment (with InfluxDB 0.9). In order to compute ratios between timeseries (fields) you would have to be able to do either nested queries or joins which were deprecated in InfluxDB 0.9:

SELECT errors_per_minute.value / pages_per_minute.value FROM errors_per_minute INNER JOIN pages_per_minute. In InfluxDB 0.9 neither the MERGE nor JOIN operations are supported.

However you could avoid such queries if you report values already as percentage from collectd (from version 5.5 it supports reporting CPU as percentage).

This is a simple bash exec script for computing percentage of cpu, memory and disk usage:

#!/bin/bash
# a collectd script reporting resources usage as percentage

HOSTNAME="${COLLECTD_HOSTNAME:-`hostname -f`}"
INTERVAL="${COLLECTD_INTERVAL:-10}"

# delay for measuring CPU
DELAY=${1:-1}
# source: http://codereview.stackexchange.com/questions/62425/using-proc-stat-to-calculate-cpu-usage
function getstat() {
    grep 'cpu ' /proc/stat | sed -e 's/  */x/g' -e 's/^cpux//'
}

function extract() {
    echo $1 | cut -d 'x' -f $2
}

function change() {
    local e=$(extract $ENDSTAT $1)
    local b=$(extract $STARTSTAT $1)
    local diff=$(( $e - $b ))
    echo $diff
}

while sleep "$INTERVAL"
do
  #Record the start statistics
  STARTSTAT=$(getstat)
  sleep $DELAY
  #Record the end statistics
  ENDSTAT=$(getstat)
  #http://www.mjmwired.net/kernel/Documentation/filesystems/proc.txt#1236
  #echo "From $STARTSTAT"
  #echo "TO   $ENDSTAT"
  #     usr    nice   sys     idle       iowait irq    guest
  #From 177834 168085 1276260 3584351494 144468 154895 0 0 0 0
  #TO   177834 168085 1276261 3584351895 144468 154895 0 0 0 0

  USR=$(change 1)
  NICE=$(change 2)
  SYS=$(change 3)
  IDLE=$(change 4)
  IOW=$(change 5)
  #echo USR $USR SYS $SYS IDLE $IDLE IOW $IOW

  ACTIVE=$(( $USR + $SYS + $IOW + $NICE))
  TOTAL=$(($ACTIVE + $IDLE))
  PCT=$(( $ACTIVE * 100 / $TOTAL ))
  #echo "BUSY $ACTIVE TOTAL $TOTAL $PCT %"
  date=$(date +%s)
  # percentage of used CPU
  echo "PUTVAL $HOSTNAME/cpu/gauge-all_pct interval=$INTERVAL $date:$PCT"
  # percentage of used memory
  mem_used=$(free | awk 'FNR == 3 {print $3/($3+$4)*100}')
  echo "PUTVAL $HOSTNAME/memory/gauge-mem_used interval=$INTERVAL $date:$mem_used"
  # percentage of used disk
  disk_used=$(df -hl | grep 'rootfs' | awk '{print substr($5, 0, length($5))}')
  echo "PUTVAL $HOSTNAME/df/gauge-used_pct interval=$INTERVAL $date:$disk_used"
done

It would be probably more efficient to write this as Python plugin. Anyway, then you can query memory usage:

SELECT mean("value") FROM "memory_value" WHERE "type" = 'gauge' AND $timeFilter GROUP BY time($interval), "host"



回答2:


I am currently using this on version 1.5.1:

SELECT sum("value") AS "total" FROM "memory_value" WHERE ("host" = 'my-host' AND "type_instance" =~ /(free|used|cached|buffered)/) AND time > now() -6h Group BY time(1m)

To get all values I use this regex in the query:

"type_instance" =~ /(free|used|cached|buffered)/)

I need to set the time(1m) to the match interval I am using in collected 60 in my case, within grafana this looks like this:

SELECT sum("value") AS "total" FROM "memory_value" WHERE ("host" =~ /^$host$/ AND "type_instance" =~ /(free|used|cached|buffered)/) AND $timeFilter GROUP BY time($__interval) fill(null)


来源:https://stackoverflow.com/questions/31210041/influxdb-sum-first-value-metric-of-different-series-but-same-time-interval-for-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!