1
+ #!/usr/bin/env python3
2
+ #-*- coding: UTF-8 -*-
3
+
4
+ import argparse
5
+ from collections import Counter
6
+ from mysql import connector
7
+
8
+
9
+ def all_connections (args ):
10
+ """返回当前数据库实例中所有的连接
11
+ """
12
+ c = Counter ()
13
+ cnx = None
14
+ try :
15
+ cnx = connector .connect (host = args .host ,port = args .port ,user = args .user ,password = args .password )
16
+ cursor = cnx .cursor ()
17
+ cursor .execute ("select user,host,state from information_schema.processlist;" )
18
+ for user ,host ,state in cursor .fetchall ():
19
+ if ':' in host :
20
+ host ,_ = host .split (':' )
21
+ c .update ({host :1 })
22
+ except Exception as e :
23
+ print (e )
24
+ exit ()
25
+ finally :
26
+ if cnx != None :
27
+ cnx .close ()
28
+
29
+ #格式化输出
30
+ print ("{0:<32} {1}" .format ("client_host_ip" .upper (),"counter" .upper ()))
31
+ print ("-" * 48 )
32
+ for host_ip ,counter in c .most_common (args .top ):
33
+ print ("{0:<32} {1}" .format (host_ip ,counter ))
34
+
35
+ def active_connections (args ):
36
+ """返回当前实例中所有的非sleep状态的连接
37
+ """
38
+ c = Counter ()
39
+ cnx = None
40
+ try :
41
+ cnx = connector .connect (host = args .host ,port = args .port ,user = args .user ,password = args .password )
42
+ cursor = cnx .cursor ()
43
+ cursor .execute ("select user,host,state from information_schema.processlist;" )
44
+ for user ,host ,state in cursor .fetchall ():
45
+ if ':' in host :
46
+ host ,_ = host .split (':' )
47
+ if 'sleep' not in state .lower () and state != '' :
48
+ c .update ({host :1 })
49
+ except Exception as e :
50
+ print (e )
51
+ exit ()
52
+ finally :
53
+ if cnx != None :
54
+ cnx .close ()
55
+
56
+ #格式化输出
57
+ print ("{0:<32} {1}" .format ("client_host_ip" .upper (),"counter" .upper ()))
58
+ print ("-" * 48 )
59
+ for host_ip ,counter in c .most_common (args .top ):
60
+ print ("{0:<32} {1}" .format (host_ip ,counter ))
61
+
62
+ def all_user (args ):
63
+ """与all_connection类似,不同的是以user为维度进行聚合
64
+ """
65
+ c = Counter ()
66
+ cnx = None
67
+ try :
68
+ cnx = connector .connect (host = args .host ,port = args .port ,user = args .user ,password = args .password )
69
+ cursor = cnx .cursor ()
70
+ cursor .execute ("select user,host,state from information_schema.processlist;" )
71
+ for user ,host ,state in cursor .fetchall ():
72
+ c .update ({user :1 })
73
+ except Exception as e :
74
+ print (e )
75
+ exit ()
76
+ finally :
77
+ if cnx != None :
78
+ cnx .close ()
79
+
80
+ #格式化输出
81
+ print ("{0:<32} {1}" .format ("client_host_ip" .upper (),"counter" .upper ()))
82
+ print ("-" * 48 )
83
+ for host_ip ,counter in c .most_common (args .top ):
84
+ print ("{0:<32} {1}" .format (host_ip ,counter ))
85
+
86
+ def active_user (args ):
87
+ """以活跃用户为维度为当前连接进行聚合
88
+ """
89
+ c = Counter ()
90
+ cnx = None
91
+ try :
92
+ cnx = connector .connect (host = args .host ,port = args .port ,user = args .user ,password = args .password )
93
+ cursor = cnx .cursor ()
94
+ cursor .execute ("select user,host,state from information_schema.processlist;" )
95
+ for user ,host ,state in cursor .fetchall ():
96
+ if 'sleep' not in state .lower () and state != '' :
97
+ c .update ({user :1 })
98
+ except Exception as e :
99
+ print (e )
100
+ exit ()
101
+ finally :
102
+ if cnx != None :
103
+ cnx .close ()
104
+
105
+ #格式化输出
106
+ print ("{0:<32} {1}" .format ("client_host_ip" .upper (),"counter" .upper ()))
107
+ print ("-" * 48 )
108
+ for host_ip ,counter in c .most_common (args .top ):
109
+ print ("{0:<32} {1}" .format (host_ip ,counter ))
110
+
111
+ operations = {
112
+ 'all_conn' :all_connections ,
113
+ 'active_conn' :active_connections ,
114
+ 'all_user' :all_user ,
115
+ 'active_user' :active_user ,
116
+ }
117
+
118
+ if __name__ == "__main__" :
119
+ parser = argparse .ArgumentParser ()
120
+ parser .add_argument ('--host' ,help = 'Connect to host' ,default = '127.0.0.1' )
121
+ parser .add_argument ('--port' ,help = 'Port number to use for connection' ,default = 3306 ,type = int )
122
+ parser .add_argument ('--user' ,help = 'User for login if not current user' ,default = 'root' )
123
+ parser .add_argument ('--password' ,default = 'Password to use when connecting to server' )
124
+ parser .add_argument ('--top' ,default = 7 ,type = int )
125
+ parser .add_argument ('operation' ,choices = operations .keys ())
126
+ args = parser .parse_args ()
127
+ operations [args .operation ](args )
0 commit comments