- 论坛徽章:
- 0
|
最近项目组提出要实时通过流量图观察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的数据模板,可以通过流量图显示。 |
|