问题描述
我正在使用influxdb grafana并进行收集,我想显示内存使用情况图.
I am using influxdb grafana and collectd and i want to display memory usage graph.
收集到的数据为我提供了内存的该指标值,并将其保存在influxdb中
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
我想在grafana图形中显示总内存所以我需要总结以下指标:
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
如何在influxdb或grafana中查询此内容?
How can I query this in influxdb or in grafana ?
推荐答案
我认为目前(使用InfluxDB 0.9)这是不可能的.为了计算时间序列(字段)之间的比值, 嵌套查询或联接在InfluxDB 0.9中已弃用:
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:
但是,如果您报告的值已经是所收集百分比的值,则可以避免此类查询(从5.5版开始,它支持将CPU报告为百分比).
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).
这是一个简单的bash exec
脚本,用于计算CPU,内存和磁盘使用率的百分比:
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
将其编写为Python插件可能会更有效.无论如何,然后您可以查询内存使用情况:
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"
这篇关于grafana图的不同系列但时间间隔相同的influxdb总和第一值度量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!