This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myvardiff.py
executable file
·105 lines (85 loc) · 2.65 KB
/
myvardiff.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
#!/usr/bin/env python
#coding: utf-8
"""myvardiff - diff mysql variables to dumped variables(json, dumped by myvardump)
https://github.com/netmarkjp/myvardiff
This program was inspired by myprofiler( https://github.com/methane/myprofiler )
"""
import os
import sys
import json
from ConfigParser import SafeConfigParser
from optparse import OptionParser
try:
# MySQL-python
import MySQLdb
from MySQLdb.cursors import DictCursor
except ImportError:
try:
# PyMySQL
import pymysql as MySQLdb
from pymysql.cursors import DictCursor
except ImportError:
print "Please install MySQLdb(MySQL-python) or PyMySQL"
sys.exit(1)
def connect(conf='~/.my.cnf', section='DEFAULT'):
"""
connect to MySQL from conf file.
"""
parser = SafeConfigParser()
parser.read([os.path.expanduser(conf)])
args = {}
if parser.has_option(section, 'socket'):
args['unix_socket'] = parser.get(section, 'socket')
else:
args['host'] = parser.get(section, 'host')
if parser.has_option(section, 'port'):
args['port'] = int(parser.get(section, 'port'))
args['user'] = parser.get(section, 'user')
args['passwd'] = parser.get(section, 'password')
args['charset'] = 'utf8'
return MySQLdb.connect(**args)
def variables(con):
cur = con.cursor(DictCursor)
cur.execute("show global variables")
for row in cur.fetchall():
if row['Variable_name'] is None:
continue
if row['Value'] is None:
continue
yield row
def build_option_parser():
parser = OptionParser()
parser.add_option(
'-c', '--config',
help="read MySQL configuration from. (default: '~/.my.cnf'",
default='~/.my.cnf'
)
parser.add_option(
'-s', '--section',
help="read MySQL configuration from this section. (default: '[DEFAULT]')",
default="DEFAULT"
)
parser.add_option(
'--before',
help="dumped json filename by myvardump.py (default: 'myvardump.json')",
default="myvardump.json"
)
return parser
def main():
parser = build_option_parser()
opts, args = parser.parse_args()
try:
con = connect(opts.config, opts.section)
except Exception, e:
parser.error(e)
values = dict()
for variable in variables(con):
values.update({variable['Variable_name']: variable['Value']})
con.close()
for k, v in json.load(open(opts.before)).iteritems():
if k not in values:
print "%s missing" % k
elif v != values[k]:
print "%s:\t%s -> %s" % (k, v, values[k])
if __name__ == '__main__':
main()