`
zhoujinhuang
  • 浏览: 91755 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

memcached监控的python脚本

阅读更多

memcached 监控脚本,用于记录memcached的状态和负载情况。

输出至文件monitor_memcache_127.0.0.1_2014_9_19.log 中。包含时间、状态、采样区间平均每秒变化量(每秒的get 、set、bytes_read、bytes_written、内存变化)。

2014-09-19 00:08:10 1411056490 2325831 583884 365383267 1295656708 169255212 1 23 10 6402 14636 -183

2014-09-19 00:08:15 1411056495 2325948 583937 365415278 1295729889 169254299 1 26 8 6092 17059 -156

根据需要可以修改 regex1,记录其它的状态的变化量。

 

 

 

import telnetlib
import time
import re,os
import sys
Host = sys.argv[1]
Port = 11211
command ='stats'+ '\n'
delay_sec = 5

tn = telnetlib.Telnet(Host,Port) 
tn.read_very_eager() 
tn.write('stats' + '\n')  
tn.read_until('END')
print command

cur_time = time.localtime()
log_file_name = "monitor_memcache"+ "_" + Host + "_" +  str(cur_time[0]) + "_" + \
                             str(cur_time[1]) + "_" + \
                             str(cur_time[2]) +".log"

log_file = open(log_file_name, "w")
regex1='STAT time (\d+).*STAT cmd_get (\d+).*STAT cmd_set (\d+).*STAT bytes_read (\d+).*STAT bytes_written (\d+).*STAT bytes (\d+)'
pat = re.compile(regex1, re.DOTALL)
lv = []
while (1) :
	tn.write(command)
	ret = tn.read_until('END')
	cur_time = time.localtime()
	m = pat.search(ret)
	v = m.groups()
	if len(lv) > 0:
		vi = [int(x) for x in v]
		d = [x-y for x,y in zip(vi,lv)]
		d_ps = [x/d[0] for x in d]
		s = lv + d_ps
		li = time.strftime('%Y-%m-%d %H:%M:%S',cur_time)+' '+ str(' '.join(list(map(str,s))))
		log_file.write(li+'\n')
		log_file.flush()
	lv = list(map(int,v))
	time.sleep(delay_sec)
log_file.close()
tn.close()

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics