监控mysql-Threads值,当值大于设定阀值50时。将 show full processlist 输出到日志。
mysql-th.py
import commands
import time
import datetime
now = datetime.datetime.now()
t= now.strftime("%Y-%m-%d-%H-%M-%S")
file_th = open(‘thfile.txt‘, ‘a+‘)th = commands.getstatusoutput("mysql -ubbs -h mysql主机ip -p密码 -e ‘status‘|grep Threads|awk ‘{print $2}‘")
th_int= int(th[1])
if th[0] ==0:
print "ok"
file_th.write(th[1] +‘ ‘+t +‘\n‘)
file_th.close( )
if th_int >50:
print "thread > 50"
file_proc =open(‘log/%s.log‘ % t ,‘w+‘)
file_proc.write(commands.getstatusoutput("mysql -ubbs -h mysql主机ip -p密码 -e ‘show full processlist‘
")[1]+‘\n‘ )
file_proc.close()
else:
print "thread < 50"
else:
print "not ok "while.py
import os
import time
while True:
os.system("python mysql-th.py")
time.sleep(30)日志输出到
thfile.txt
例: 24 2015-10-15-23-25-39 12 2015-10-15-23-25-41 11 2015-10-19-10-14-39 21 2015-10-19-10-14-41
第一列是线程数 第二列是时间 如果第一列大于设定阀值50 则生成以时间命名的日志文件,记录“show full processlist” 内容 例: 2015-10-15-23-25-41.log
本文出自 “点点滴滴做起” 博客,请务必保留此出处http://linuxinge.blog.51cto.com/4604195/1704330
原文:http://linuxinge.blog.51cto.com/4604195/1704330