43trnb 发表于 2015-10-20 08:39:10

python 监控mysql-Threads

监控mysql-Threads值,当值大于设定阀值50时。将 show full processlist 输出到日志。
mysql-th.py

1
2
3
4
5
6
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+')





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
th = commands.getstatusoutput("mysql -ubbs -hmysql主机ip-p密码 -e 'status'|grep Threads|awk '{print $2}'")
th_int= int(th)
if th ==0:
    print "ok"
    file_th.write(th +''+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 -hmysql主机ip-p密码 -e 'show full processlist'
")+'\n' )
file_proc.close()
    else:
print "thread < 50"
else:
    print "notok "





while.py

1
2
3
4
5
import os
import time
while True:
    os.system("pythonmysql-th.py")
    time.sleep(30)






日志输出到

1
thfile.txt





1
2
3
4
5
例:
242015-10-15-23-25-39
122015-10-15-23-25-41
112015-10-19-10-14-39
212015-10-19-10-14-41






1
2
3
4
第一列是线程数第二列是时间
如果第一列大于设定阀值50 则生成以时间命名的日志文件,记录“show full processlist” 内容
例:
2015-10-15-23-25-41.log



页: [1]
查看完整版本: python 监控mysql-Threads