Skip to content
This repository was archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
netmarkjp committed Jul 31, 2013
1 parent 6417e9c commit ef2d5b8
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 4 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
.DS_Store
*.pyc
*.swo
*.swp
dist/*
tmp/*
venv/*
target/*
project/target/*
project/project/target/*
logs/*
.settings/*
# github
*.py[cod]

# C extensions
Expand Down
4 changes: 0 additions & 4 deletions README.md

This file was deleted.

33 changes: 33 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
myvardump, myvardiff
============================

- myvardump.py: dump MySQL `show global variables` to json
- myvardiff.py: diff running MySQL `show global variables` to json(dumped by myvardump)

..
$ python myvardump.py --config tmp/my.cnf --out tmp/test.json
$ python myvardiff.py --config tmp/my.cnf --before tmp/test.json
read_buffer_size: 131072 -> 1048576
wait_timeout: 28890 -> 28800

Installation
============================
..
git clone https://github.com/netmarkjp/myvardiff
pip install MySQL-python

How to Use
============================
..
python myvardump.py --config example.my.cnf --out variables.json

..
python myvardiff.py --config example.my.cnf --before variables.json

Note
----------------------------
This program was inspired by myprofiler( https://github.com/methane/myprofiler )
5 changes: 5 additions & 0 deletions example.my.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[DEFAULT]
host = localhost
# socket =
user = root
password =
105 changes: 105 additions & 0 deletions myvardiff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
109 changes: 109 additions & 0 deletions myvardump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env python
#coding: utf-8

"""myvardump - dump mysql variables to json (for myvardiff)
https://github.com/netmarkjp/myvardump
This program was inspired by myprofiler( https://github.com/methane/myprofiler )
"""

import os
import sys
import json
import time
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(
'--out',
help="json filename (if file already exists, automaticaly rotate) (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()

if os.path.exists(opts.out):
src = opts.out
dst = "%s.%s" % (opts.out, time.strftime('%Y%m%d_%H%M%S', time.localtime()))
os.rename(src, dst)

out = json.dumps(values, sort_keys=True, indent=2)
f = open(opts.out, mode='w')
f.write(out)
f.close()

if __name__ == '__main__':
main()

0 comments on commit ef2d5b8

Please sign in to comment.