一、python-memcached的安装

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

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

二、python统计memcache的命中率

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

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

三、相关拓展

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

  1#!/bin/bash
  2###############################
  3#检查memcached的命中率
  4#加载nagios自带utils.sh
  5###############################
  6#source /usr/local/nagios/libexec/utils.sh
  7print_usage()
  8{
  9    echo "check_memcached -H IP -P port -w warning -c critical"
 10}
 11###################
 12#获取命令行执行参数
 13###################
 14if  [ $# == 0 ];then
 15    print_usage;
 16    exit;
 17else
 18while test -n "$1";do
 19    case "$1" in
 20        -H)
 21            host=$2
 22            shift
 23            ;;
 24        -P)
 25            port=$2
 26            shift
 27            ;;
 28        -w)
 29            warning=$2
 30            shift
 31            ;;
 32        -c)
 33            critical=$2
 34            shift
 35            ;;
 36        *)
 37            echo "Unknown argument:$1"
 38            print_usage
 39            exit $STATE_UNKNOWN
 40            ;;
 41    esac
 42    shift
 43done
 44fi
 45########################
 46#function div_f()
 47#检查参数,返回两个数字比
 48########################
 49function div_f()
 50{
 51    ref=`awk -v num_a=$1 -v num_b=$2 'BEGIN{printf "%0.2f n",num_a/num_b}'`;
 52    echo $ref;
 53}
 54##################
 55#得到命中率函数
 56##################
 57function getMemcachedHits()
 58{
 59    memcachedinfo=`/usr/local/nagios/libexec/check_tcp -H $host -p $port -E -s 'statsrnquitrn' -e 'uptime' | tr "r" "@"`
 60    get_hits=`echo $memcachedinfo | grep -o "@ STAT get_hits [0-9]*"| awk '{print $4}'`
 61    cmd_get=`echo $memcachedinfo | grep -o "@ STAT cmd_get [0-9]*"| awk '{print $4}'`
 62    div_f $get_hits $cmd_get;
 63}
 64hits=`getMemcachedHits $host $port`;
 65##################
 66#得到命中率所在区间
 67##################
 68function re_rang()
 69{
 70    rang=$hits;
 71    interval_a=$critical;
 72    interval_b=$warningl
 73    if [[ $rang < $interval_a ]];then
 74        echo "0";
 75    elif [[ $rang < $interval_b ]];then
 76        echo "1";
 77    elif [[ $rang > $interval_b ]]||[[ $rang == $interval_b ]] ;then
 78        echo "2";
 79    else
 80        return;
 81    fi
 82}
 83res=`re_rang $critical $warning $hits`;
 84case "$res" in
 85    0)
 86        echo "Critical memcached_hits=$hits|memcached_hits=$hits;$warning;$critical;"
 87        exit $STATE_CRITICAL
 88        ;;
 89    1)
 90        echo "Warning  memcached_hits=$hits|memcached_hits=$hits;$warning;$critical;"
 91        exit $STATE_WARNING
 92        ;;
 93    2)
 94        echo "Ok  memcached_hits=$hits|memcached_hits=$hits;$warning;$critical;"
 95        exit $STATE_OK
 96        ;;
 97    *)
 98        echo "Unkown"
 99        exit $STATE_UNKNOWN
100        ;;
101esac