-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_params_mysql_server.py
More file actions
82 lines (65 loc) · 2.08 KB
/
update_params_mysql_server.py
File metadata and controls
82 lines (65 loc) · 2.08 KB
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
''' Check and update parameters for MySQL server
Author: Artem Rybin
Usage: update_params_mysql_server.py -f <inputfile> -p <period(sec)>
'''
import getopt, sys, os
import time
import subprocess
user = 'root'
password = '775ft'
def main():
print('Start')
while True:
# parsing args
try:
opts, args = getopt.getopt(sys.argv[1:], 'f:p:', ['file=','period='])
except getopt.GetoptError as err:
print(err)
sys.exit(2)
filename = None
period = None
for option, arg in opts:
if option in ('-f', '--file'):
filename = arg
elif option in ('-p', '--period'):
period = float(arg)
else:
assert False, 'No option'
# get parameters from inputfile
params = get_params_from_file(filename)
# get current parameters from mysql
cur_params = get_current_params(params.keys());
# set new params
set_params(params)
# check new params
cur_params = get_current_params(params.keys());
time.sleep(period)
def get_params_from_file(fname):
file = open(fname)
data = file.read()
lines = data.splitlines()
file.close()
parameters = {}
for line in lines:
key, value = line.split('|', 1 )
parameters[key] = value
print(parameters)
return parameters
def get_current_params(keys):
cur_params = {}
for key in keys:
select_str = 'SELECT @@%s'%key + ';'
cmd = ['mysql', '-u', user, '-p%s'%password, '-Bse', select_str]
proc = subprocess.Popen(cmd, stdout = subprocess.PIPE)
value = proc.communicate()[0]
cur_params[key] = value.rstrip('\n')
print(cur_params)
return cur_params
def set_params(params):
keys = params.keys()
for key in keys:
set_str = 'SET GLOBAL %s=%s'%(key, params[key]) + ';'
cmd = ['mysql', '-u', user, '-p%s'%password, '-Bse', set_str]
proc = subprocess.Popen(cmd, stdout = subprocess.PIPE)
if __name__ == '__main__':
main()