forked from dwayn/mysql-sharded-schema-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql-sharded-schema-auditor
executable file
·61 lines (50 loc) · 2.57 KB
/
mysql-sharded-schema-auditor
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
#!/usr/bin/env python
import argparse
import MySQLdb
from ShardAuditor import ShardAuditor
import settings
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-H', '--host', help="Hostname to filter all shard hosts")
parser.add_argument('-p', '--port', help="Port to filter all shard hosts")
parser.add_argument('--shards', type=int, nargs='+', help="Space separate list of shard_id\'s to audit", metavar='SHARD_ID')
parser.add_argument('--model', help='Model shard schema to override the config')
parser.add_argument('--status', action='store_true', help='Output status during execution')
args = parser.parse_args()
if args.model:
settings.MODEL_SHARD_DB_CREDENTIALS['dbname'] = args.model
dbconn = MySQLdb.connect(host=settings.LOCATOR_DB_CREDENTIALS['host'],
port=settings.LOCATOR_DB_CREDENTIALS['port'],
user=settings.LOCATOR_DB_CREDENTIALS['user'],
passwd=settings.LOCATOR_DB_CREDENTIALS['password'],
db=settings.LOCATOR_DB_CREDENTIALS['dbname'])
db = dbconn.cursor()
auditor = ShardAuditor()
auditor.load_baseline_schema(settings.MODEL_SHARD_DB_CREDENTIALS['user'],
settings.MODEL_SHARD_DB_CREDENTIALS['password'],
settings.MODEL_SHARD_DB_CREDENTIALS['host'],
settings.MODEL_SHARD_DB_CREDENTIALS['port'],
settings.MODEL_SHARD_DB_CREDENTIALS['dbname'])
whereclause = ''
if settings.LOCATOR_TABLE['where_clause']:
whereclause = " " + settings.LOCATOR_TABLE['where_clause']
sql = "select " + settings.LOCATOR_TABLE['shardid_col'] + ", " + settings.LOCATOR_TABLE['dbname_col'] + ", " + settings.LOCATOR_TABLE['host_col'] + ", " \
+ settings.LOCATOR_TABLE['port_col'] + " from " + settings.LOCATOR_TABLE['tablename'] + whereclause
db.execute(sql)
rows = db.fetchall()
if not rows:
print "No shards were found using the following query: " + sql
exit()
for row in rows:
shard, dbname, host, port = row
if args.host and args.host != host:
continue
if args.port and args.port != port:
continue
if args.shards and shard not in args.shards:
continue
if args.status:
print "AUDITING: " + shard
auditor.audit_schema(settings.SHARD_DB_CREDENTIALS['user'], settings.SHARD_DB_CREDENTIALS['password'], host, dbname, port)
if __name__ == "__main__":
main()