缓存命中率计算公式:
MAIN.cache_hit / (MAIN.cache_hit + MAIN.cache_miss)
以下为监控varnish缓存命中率的脚本,一共有两个功能:
- 监控varnish总命中率,使用以上公式进行计算
- 监控最近一段时间内的命中率,方法是把当前获得的hit数和总数以变量形式输出到文件中,下一次执行该脚本时从文件中获取旧数据,使用当前数据与旧数据的差值进行计算命中率
使用方法:
传入totalHitRate 或 recentHitRate 以获取对应的值,可结合zabbix做监控
#!/bin/bash totalHit=`varnishstat -1 | awk ‘/MAIN.cache_hit\>/{print $2}‘` totalMiss=`varnishstat -1 | awk ‘/MAIN.cache_miss\>/{print $2}‘` total=$[${totalHit}+${totalMiss}] echoTotalHitRate() { totalHitRate=$(awk ‘BEGIN{print "‘"$totalHit"‘"/"‘"$total"‘"}‘) echo ${totalHitRate} } echoRecentHitRate() { [ -f lastData ] && source `pwd`/lastData recentHit=$[$totalHit-${lastHit:-0}] recentTotal=$[$total-${lastTotal:-0}] echo "lastHit=$totalHit" > lastData echo "lastTotal=$total" >> lastData [ $recentTotal != "0" ] && recentHitRate=$(awk ‘BEGIN{print "‘"$recentHit"‘"/"‘"$recentTotal"‘"}‘) || recentHitRate=`echoTotalHitRate` echo ${recentHitRate} } case $1 in totalHitRate) echoTotalHitRate ;; recentHitRate) echoRecentHitRate ;; esac
时间: 2024-11-08 19:05:08