flowsdb和grafana图的不同序列但相同时间间隔的第一个值度量

时间:2015-07-03 15:20:04

标签: influxdb collectd grafana

我正在使用Influxdb grafana和collectd,我想显示内存使用情况图。

collectd为我提供内存的度量值并将其保存在Influxdb

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

我想在grafana图中显示总内存 所以我需要总结以下指标:

memory_buffered + memory_cached + memory_free + memory_used

如何在Influxdb或grafana中查询?

2 个答案:

答案 0 :(得分:1)

我认为目前这是不可能的(使用InfluxDB 0.9)。要计算ratios between timeseries (fields),您必须能够执行在InfluxDB 0.9中弃用的nested queriesjoins

  

SELECT errors_per_minute.value / pages_per_minute.value FROM errors_per_minute INNER JOIN pages_per_minute。在InfluxDB 0.9中,既不支持MERGE也不支持JOIN操作。

但是,如果从collectd报告已经作为百分比的值,则可以避免此类查询(从版本5.5开始,它支持以百分比形式报告CPU)。

这是一个简单的bash exec脚本,用于计算cpu,内存和磁盘使用百分比:

#!/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

将此作为Python插件编写可能更有效。无论如何,你可以查询内存使用情况:

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

答案 1 :(得分:0)

我目前在版本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)

要获取所有值,我在查询中使用此正则表达式:

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

在我的情况下,我需要将time(1m)设置为我在收集的60中使用的匹配间隔,在grafana中,这看起来像这样:

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)