influxdb sum first value metric of different series but the same time interval for grafana graph
Posted
技术标签:
【中文标题】influxdb sum first value metric of different series but the same time interval for grafana graph【英文标题】:influxdb sum first value metric of different series but same time interval for grafana graph 【发布时间】:2015-09-21 11:40:17 【问题描述】:我正在使用 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 中查询?
【问题讨论】:
【参考方案1】:我认为目前这是不可能的(使用 InfluxDB 0.9)。为了计算 ratios between timeseries (fields),您必须能够执行在 InfluxDB 0.9 中已弃用的 nested queries 或 joins:
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"
【讨论】:
投反对票有什么理由吗? InfluxDB 0.9 有更好的解决方案吗?【参考方案2】:我目前在 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)
【讨论】:
以上是关于influxdb sum first value metric of different series but the same time interval for grafana graph的主要内容,如果未能解决你的问题,请参考以下文章
9_InfluxDB常用函数选择类函数(TOP() BOTTOM() FIRST() LAST() MAX() MIN() PERCENTILE())
8InfluxDB常用函数聚合函数,count()函数,DISTINCT()函数,MEAN()函数,MEDIAN()函数,SPREAD()函数,SUM()函数
InfluxDB?????????InfluxDB????????????????????????????????????