免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2696 | 回复: 0
打印 上一主题 下一主题

修改了一下网上cacti监控memcached的模板脚本,使其能够输出memcached命中率! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-04-28 15:54 |只看该作者 |倒序浏览
     最近项目组提出要实时通过流量图观察memcached命中率,目前在cacti中使用的memcache模板是在网上下的,是一个叫memcache.py的脚本。这个脚本不支持命中率的输出,简单修改了下,
使其能够输出命中率数值。
脚本信息:
vi memcached.py
#!/usr/bin/python

import sys
import memcache
from optparse import OptionParser

stats = {'total_items': 0, 'bytes_written': 0, 'uptime': 0, 'bytes': 0,
         'cmd_get': 0, 'curr_items': 0, 'curr_connections': 0, 'connection_structures': 0,
         'limit_maxbytes': 0, 'rusage_user': 0.0, 'total_connections': 0, 'cmd_set': 0,
         'time': 0, 'get_misses': 0, 'bytes_read': 0, 'rusage_system': 0.0, 'get_hits': 0}

parser = OptionParser(usage="usage: %prog [-h] [-p PORT] HOSTNAME1 HOSTNAME2 ...\nTotals will be returned for multiple HOSTNAME arguments.")
parser.set_defaults(port = "11211")
parser.add_option("-p", "--port", dest="port", metavar="PORT",
                  help="default memcached port [default: 11211]")
(options, args) = parser.parse_args()

hosts = []
if (args):
    for host in args:
        hosts.append("%s:%s" % (host, options.port));
else:
    parser.error("At least one HOSTNAME is required.")
    sys.exit(1)

mc = memcache.Client(hosts, debug=0)
mem_stats = mc.get_stats()

if (not mem_stats):
        sys.exit()

if (len(mem_stats) > 1):
    for mstat in mem_stats:
        for key, val in stats.iteritems():
            if (key == 'rusage_user' or key == 'rusage_system'):
                stats[key] += float(mstat[1][key])
            else:
                stats[key] += int(mstat[1][key])
               
else:
    mstat = mem_stats[0][1];
    for key, val in stats.iteritems():
        if (key == 'rusage_user' or key == 'rusage_system'):
            stats[key] = float(mstat[key])
        else:
            stats[key] = int(mstat[key])
            

get_hits = stats['get_hits']
get_misses = stats['get_misses']
cmd_get = stats['cmd_get']
for stat, count in stats.iteritems():
    print "%s:%s" % (stat, count),

print "%s:%0.2f" % ("hits_ratio",float(get_hits)*100/float(cmd_get)),
print "%s:%0.2f" % ("misses_ratio",float(get_misses)*100/float(cmd_get)),


运行结果:

total_items:6259295 get_hits:274288895 uptime:140190 cmd_get:284211692 time:1335599613 bytes:19265749902 curr_connections:2151 connection_structures:2171 bytes_written:1242737463805 limit_maxbytes:21474836480 cmd_set:6259295 curr_items:4255689 rusage_user:2361.720963 get_misses:9922797 rusage_system:9540.58561 bytes_read:41678401432 total_connections:22370 hits_ratio:96.51 misses_ratio:3.49

其中hits_ratio:96.51 和misses_ratio:3.49就是memcache的命中率和未命中率,通过修改cacti中memcache的数据模板,可以通过流量图显示。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP