python统计memcached命中率

一、python-memcached的安装

这里列一个通过python获取memcached命中率的代码,该代码需要依赖python-memcached包文件,该包可以通过easy_install或pypi安装,也可以直接网官上下载源码包进行安装 。这里就以centos 下的easy_install为例:

[root@localhost ~]# easy_install python-memcached
Searching for python-memcached
Reading http://pypi.python.org/simple/python-memcached/
Reading http://www.tummy.com/Community/software/python-memcached/
Best match: python-memcached 1.53
Downloading http://ftp.tummy.com/pub/python-memcached/old-releases/python-memcached-1.53.tar.gz
Processing python-memcached-1.53.tar.gz
Running python-memcached-1.53/setup.py -q bdist_egg --dist-dir /tmp/easy_install-GukjRE/python-memcached-1.53/egg-dist-tmp-wDUrUa
warning: no files found matching '*.rst'
warning: no files found matching '*.txt'
warning: no files found matching 'MakeFile'
warning: no previously-included files matching '*.pyc' found anywhere in distribution
warning: no previously-included files matching '.gitignore' found anywhere in distribution
warning: no previously-included files matching '.DS_Store' found anywhere in distribution
zip_safe flag not set; analyzing archive contents...
Adding python-memcached 1.53 to easy-install.pth file
Installed /usr/lib/python2.6/site-packages/python_memcached-1.53-py2.6.egg
Processing dependencies for python-memcached
Finished processing dependencies for python-memcached

二、python统计memcache的命中率

安装完成后,就可以import该包进行python脚本的调用了,脚本如下 :

#!/usr/bin/env python
from __future__ import division
import memcache    #导入memcache模块
host=['192.168.1.200:11211','192.168.1.201:11212']   #定义memcached服务器列表
mc=memcache.Client(host,debug=0)
stat=mc.get_stats()
for i in range(len(stat)):
      host=stat[i][0].split(‘ ‘)[0]   #获取服务器名字
      get=int(stat[i][1]['cmd_get'])     #获取get数
      hit=int(stat[i][1]['get_hits'])    #获取hit数
      miss=int(stat[i][1]['get_misses'])   #获取miss数
if get==0:
      rate=0
else:
      rate=hit/get*100
      print ‘%st%.2f%%’%(host,rate)

三、相关拓展

使用python进行统计的代码是比较简洁的,当然也可以通过shell进行统计,这里再给出一个nagios下的一个shell版的memcached命中率统计的插件:

#!/bin/bash
###############################
#检查memcached的命中率
#加载nagios自带utils.sh
###############################
#source /usr/local/nagios/libexec/utils.sh
print_usage()
{
    echo "check_memcached -H IP -P port -w warning -c critical"
}
###################
#获取命令行执行参数
###################
if  [ $# == 0 ];then
    print_usage;
    exit;
else
while test -n "$1";do
    case "$1" in
        -H)
            host=$2
            shift
            ;;
        -P)
            port=$2
            shift
            ;;
        -w)
            warning=$2
            shift
            ;;
        -c)
            critical=$2
            shift
            ;;
        *)
            echo "Unknown argument:$1"
            print_usage
            exit $STATE_UNKNOWN
            ;;
    esac
    shift
done
fi
########################
#function div_f()
#检查参数,返回两个数字比
########################
function div_f()
{
    ref=`awk -v num_a=$1 -v num_b=$2 'BEGIN{printf "%0.2f n",num_a/num_b}'`;
    echo $ref;
}
##################
#得到命中率函数
##################
function getMemcachedHits()
{
    memcachedinfo=`/usr/local/nagios/libexec/check_tcp -H $host -p $port -E -s 'statsrnquitrn' -e 'uptime' | tr "r" "@"`
    get_hits=`echo $memcachedinfo | grep -o "@ STAT get_hits [0-9]*"| awk '{print $4}'`
    cmd_get=`echo $memcachedinfo | grep -o "@ STAT cmd_get [0-9]*"| awk '{print $4}'`
    div_f $get_hits $cmd_get;
}
hits=`getMemcachedHits $host $port`;
##################
#得到命中率所在区间
##################
function re_rang()
{
    rang=$hits;
    interval_a=$critical;
    interval_b=$warningl
    if [[ $rang < $interval_a ]];then
        echo "0";
    elif [[ $rang < $interval_b ]];then
        echo "1";
    elif [[ $rang > $interval_b ]]||[[ $rang == $interval_b ]] ;then
        echo "2";
    else
        return;
    fi
}
res=`re_rang $critical $warning $hits`;
case "$res" in
    0)
        echo "Critical memcached_hits=$hits|memcached_hits=$hits;$warning;$critical;"
        exit $STATE_CRITICAL
        ;;
    1)
        echo "Warning  memcached_hits=$hits|memcached_hits=$hits;$warning;$critical;"
        exit $STATE_WARNING
        ;;
    2)
        echo "Ok  memcached_hits=$hits|memcached_hits=$hits;$warning;$critical;"
        exit $STATE_OK
        ;;
    *)
        echo "Unkown"
        exit $STATE_UNKNOWN
        ;;
esac

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注