forked from tripy/proxyweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdb.py
239 lines (176 loc) · 6.41 KB
/
mdb.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/python3
"""ProxyWeb - A Proxysql Web user interface
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""
__author__ = "Miklos Mukka Szel"
__contact__ = "[email protected]"
__license__ = "GPLv3"
from typing import Any, Dict, List, Tuple
import mysql.connector
from mysql.connector.connection import MySQLConnection, MySQLCursor
import logging
import subprocess
from lib import config
from models import Config, Server
def get_config(file: str = "config/config.yml") -> Config:
cfg = config.parse_config_file(file)
return cfg
def db_connect(
db,
server: str,
autocommit: bool = False,
buffered: bool = False,
dictionary: bool = True
) -> Tuple[MySQLConnection, MySQLCursor]:
config = get_config()
db["cnf"] = config
server_config = config.servers[server].dsn
logging.debug(server_config)
conn = mysql.connector.connect(
**server_config,
raise_on_warnings=True,
get_warnings=True,
connection_timeout=3
)
if conn.is_connected():
logging.debug(f"Connected to {server_config.db} as {server_config.user} on {server_config.host}")
conn.autocommit = autocommit
conn.get_warnings = True
cursor = conn.cursor(
buffered=buffered,
dictionary=dictionary
)
logging.debug(f"Buffered: {buffered}, dictionary: {dictionary}, autocommit: {autocommit}")
return conn, cursor
def get_all_dbs_and_tables(db, server: str) -> Dict[str, Dict[str, List[str]]]:
all_dbs = {server: {}}
conn, cur = db_connect(db, server=server)
try:
cur.execute("SHOW DATABASES")
table_exception_list = db["cnf"]["glob"]["hide_tables"] if "hide_tables" in db["cnf"]["glob"] else []
if db["cnf"]["servers"][server]["hide_tables"]:
table_exception_list += db["cnf"]["servers"][server]["hide_tables"]
for database in cur.fetchall():
all_dbs[server][database["name"]] = []
cur.execute(f"SHOW TABLES FROM {database['name']}")
for table in cur.fetchall():
# hide tables as per global or per server config
if table["tables"] not in table_exception_list:
all_dbs[server][database["name"]].append(table["tables"])
finally:
conn.close()
return all_dbs
def get_table_content(db, server: str, database: str, table: str) -> Dict[str, Any]:
"""returns with a dict with two keys
"column_names" = list and rows = tuples"""
content = {}
logging.debug("server: {} - db: {} - table:{}".format(
server,
database,
table
))
conn, cur = db_connect(db, server=server, dictionary=False)
try:
string = "SELECT * FROM {}.{} ORDER BY 1".format(database, table)
logging.debug("query: {}".format(string))
cur.execute(string)
content["rows"] = cur.fetchall()
content["column_names"] = [
i[0] for i in cur.description
]
finally:
conn.close()
return content
def execute_adhoc_query(db, server: str, sql: str) -> Dict[str, Any]:
"""returns with a dict with two keys
"column_names" = list and rows = tuples"""
content = {}
logging.debug("server: {} - sql: {}".format(server, sql))
conn, cur = db_connect(db, server=server, dictionary=False)
try:
cur.execute(sql)
content["rows"] = cur.fetchall()
content["column_names"] = [
i[0] for i in cur.description
]
finally:
conn.close()
return content
def execute_adhoc_report(db, server: str) -> List[Dict[str, Any]]:
"""returns with a dict with two keys
"column_names" = list and rows = tuples"""
adhoc_results = []
conn, cur = db_connect(db, server=server, dictionary=False)
try:
config = get_config()
if "adhoc_report" in config.misc.categories:
for item in config.misc.categories["adhoc_report"].queries:
cur.execute(item.sql)
adhoc_results.append({
"title": item.title,
"sql": item.sql,
"info": item.info,
"column_names": [
i[0] for i in cur.description
],
"rows": cur.fetchall()
})
else:
pass
finally:
conn.close()
return adhoc_results
def get_servers() -> List[Server]:
proxysql_servers = []
try:
servers_dict = get_config()
for server in servers_dict.servers:
proxysql_servers.append(server)
return proxysql_servers
except Exception:
raise ValueError("Cannot get the serverlist from the config file")
def get_read_only(server) -> bool:
try:
config = get_config()
if config.glob.read_only:
return True
if server in config.servers:
if config.servers[server].read_only:
return True
return False
except Exception:
raise ValueError("Cannot get read_only status from the config file")
def execute_change(db, server: str, sql: str) -> Tuple[str, str]:
# this is a temporary solution as using the mysql.connector for
# certain writes ended up with weird results, ProxySQL is not a MySQL
# server after all. We're investigating the issue.
dsn = get_config().servers[server].dsn
cmd = ("mysql -h %s -P %s -u %s -p%s main -e '%s'" % (
dsn["host"],
dsn["port"],
dsn["user"],
dsn["passwd"],
sql
))
p = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, stderr = p.communicate()
return stdout.decode(), stderr.decode().replace(
"".join([
"mysql: [Warning] Using a password on the",
" command line interface can be insecure.\n"
]),
""
)