首页 文章

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

提问于
浏览
2

我正在使用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

    我认为目前这是不可能的(使用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

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

相关问题