1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
#!/usr/bin/env python
import sys
import time
import re
import MySQLdb
def usage():
print "diff the tokudb engine status"
print "--host=HOSTNAME (default: localhost)"
print "--port=PORT"
print "--sleeptime=SLEEPTIME (default: 10 seconds)"
print "--q='show engine tokudb status'"
print "--q='select * from information_schema.global_status'"
return 1
def convert(v):
if type(v) == type('str'):
try:
v = int(v)
except:
v = float(v)
return v
def printit(stats, rs, sleeptime):
# print rs
for t in rs:
l = len(t) # grab the last 2 fields in t
k = t[l-2]
v = t[l-1]
# print k, v # debug
# try to convert v
try:
v = convert(v)
except:
pass
if stats.has_key(k):
oldv = stats[k]
if v != oldv:
print k, "|", oldv, "|", v,
try:
d = v - oldv
if sleeptime != 1:
if d >= sleeptime:
e = d / sleeptime
else:
e = float(d) / sleeptime
print "|", d, "|", e
else:
print "|", d
except:
print
stats[k] = v
print
def main():
host = None
port = None
user = None
passwd = None
sleeptime = 10
q = 'show engine tokudb status'
for a in sys.argv[1:]:
if a == "-h" or a == "-?" or a == "--help":
return usage()
match = re.match("--(.*)=(.*)", a)
if match:
exec "%s='%s'" % (match.group(1),match.group(2))
continue
return usage()
connect_parameters = {}
if host is not None:
if host[0] == '/':
connect_parameters['unix_socket'] = host
else:
connect_parameters['host'] = host
if port is not None:
connect_parameters['port'] = int(port)
if user is not None:
connect_parameters['user'] = user
if passwd is not None:
connect_parameters['passwd'] = passwd
try:
db = MySQLdb.connect(**connect_parameters)
except:
print sys.exc_info()
return 1
print "connected"
stats = {}
while 1:
try:
c = db.cursor()
n = c.execute(q)
rs = c.fetchall()
db.commit()
c.close()
except:
print "db", sys.exc_info()
return 2
try:
printit(stats, rs, int(sleeptime))
time.sleep(int(sleeptime))
except:
print "printit", sys.exc_info()
return 3
return 0
sys.exit(main())
|