我认为目前这是不可能的(使用 InfluxDB 0.9)。为了计算时间序列(字段)之间的比率,您必须能够执行InfluxDB 0.9 中已弃用的嵌套查询或连接:
从 errors_per_minute INNER JOIN pages_per_minute 中选择 errors_per_minute.value / pages_per_minute.value。InfluxDB 0.9 既不支持 MERGE 也不支持 JOIN 操作。
但是,如果您已经从 collectd 以百分比形式报告值(从 5.5 版开始,它支持以百分比形式报告 CPU),则可以避免此类查询。
这是一个简单的 bashexec
脚本,用于计算 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"