监控mysql上客户端的连接数:
vi a1.py
#!/usr/bin/python
import os
import datetime
d1 = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
d2 = datetime.datetime.now().strftime("%Y-%m-%d")
os.system(‘/bin/rm -rf /log/Hosts/date +%Y-%m-%d -d "7 days ago"
‘)
os.system(‘/bin/netstat -anp | grep 10.0.0.2:3306 > /log/Hosts/mysqls.txt‘)
d3 = os.path.join("/log/Hosts",d2)
d4 = os.path.join(d3,"mysqls2.txt")
d5 = os.path.join(d3,"mysqls3.txt")
if not os.path.exists(d3):
os.makedirs(d3)
os.mknod(d4)
os.mknod(d5)
os.remove(d5)
os.mknod(d5)
list = []
a = open(‘/log/Hosts/mysqls.txt‘,‘r‘)
b = a.readlines()
a.close()
for c in b:
d = c.split()[4]
e = d.split(":")[0]
list.append(e)
e2 = set(list)
h = open(d4,‘a‘)
h.write(" "+"\n")
h.write(str(d1)+"\n")
h.write(" "+"\n")
h.close()
for f in e2:
g = list.count(f)
g2 = (‘%s:%s‘ % (f,g))
h2 = open(d4,‘a‘)
h2.write(g2+"\n")
h2.close()
if g >= 1200:
h3 = open(d5,‘a‘)
h3.write(g2+"\n")
h3.close()
:wq
python a1.py
原文地址:http://blog.51cto.com/yangzhiming/2149248