29
29
CMD_PROCESSLIST = "show full processlist"
30
30
31
31
32
- def connect (conf = '~/.my.cnf' , section = 'client' ):
33
- """
34
- connect to MySQL from conf file.
35
- """
36
- parser = SafeConfigParser ()
37
- parser .read ([os .path .expanduser (conf )])
38
-
32
+ def connect (cnf ):
39
33
args = {}
40
-
41
- args ['host' ] = parser .get (section , 'host' )
42
- args ['user' ] = parser .get (section , 'user' )
43
- args ['passwd' ] = parser .get (section , 'password' )
44
- args ['charset' ] = 'utf8'
45
- if parser .has_option (section , 'port' ):
46
- args ['port' ] = int (parser .get (section , 'port' ))
34
+ args ['host' ] = cnf .get ('host' , 'localhost' )
35
+ args ['user' ] = cnf .get ('user' , '' )
36
+ args ['passwd' ] = cnf .get ('password' , '' )
37
+ args ['charset' ] = cnf .get ('default-character-set' , 'utf8' )
38
+ if 'port' in cnf :
39
+ args ['port' ] = int (get ('port' ))
47
40
return MySQLdb .connect (** args )
48
41
49
42
@@ -74,21 +67,36 @@ def normalize_query(row):
74
67
return row
75
68
76
69
70
+ def read_mycnf (extra_file = None , group_suffix = '' ):
71
+ cnf_files = [os .path .expanduser ('~/.my.cnf' )]
72
+ if extra_file is not None :
73
+ if not os .path .isfile (extra_file ):
74
+ print >> sys .stderr , "[warn]" , extra_file , "is not exists."
75
+ else :
76
+ cnf_files += [extra_file ]
77
+
78
+ parser = SafeConfigParser ()
79
+ parser .read (cnf_files )
80
+
81
+ cnf = dict (parser .items ('client' ))
82
+ if group_suffix :
83
+ cnf .update (parser .items ('client' + group_suffix ))
84
+ return cnf
85
+
86
+
77
87
def build_option_parser ():
78
88
parser = OptionParser ()
79
89
parser .add_option (
80
90
'-o' , '--out' ,
81
- help = "write raw queries to this file." ,
91
+ help = "Write raw queries to this file." ,
82
92
)
83
93
parser .add_option (
84
- '-c' , '--config' ,
85
- help = "read MySQL configuration from. (default: '~/.my.cnf'" ,
86
- default = '~/.my.cnf'
94
+ '-e' , '--defaults-extra-file' , dest = 'extra_file' ,
95
+ help = "Read MySQL configuration from this file additionaly" ,
87
96
)
88
97
parser .add_option (
89
- '-s' , '--section' ,
90
- help = "read MySQL configuration from this section. (default: '[DEFAULT]')" ,
91
- default = "DEFAULT"
98
+ '-s' , '--defaults-group-suffix' , dest = 'group_suffix' ,
99
+ help = "Read MySQL configuration from this section additionally" ,
92
100
)
93
101
parser .add_option (
94
102
'-n' , '--num-summary' , metavar = "K" ,
@@ -100,6 +108,8 @@ def build_option_parser():
100
108
help = "Interval of executing show processlist [sec] (default: 1.0)" ,
101
109
type = "float" , default = 1.0
102
110
)
111
+ parser .add_option ('-u' , '--user' )
112
+ parser .add_option ('-p' , '--password' )
103
113
return parser
104
114
105
115
@@ -114,12 +124,18 @@ def show_summary(counter, limit, file=sys.stdout):
114
124
def main ():
115
125
parser = build_option_parser ()
116
126
opts , args = parser .parse_args ()
127
+ outfile = None
117
128
118
129
try :
119
- outfile = None
130
+ cnf = read_mycnf (opts .extra_file , opts .group_suffix )
131
+ if opts .user :
132
+ cnf ['user' ] = opts .user
133
+ if opts .password :
134
+ cnf ['password' ] = opts .password
135
+ con = connect (cnf )
136
+
120
137
if opts .out :
121
138
outfile = open (opts .out , "w" )
122
- con = connect (opts .config , opts .section )
123
139
except Exception , e :
124
140
parser .error (e )
125
141
@@ -140,6 +156,7 @@ def main():
140
156
if outfile :
141
157
print >> outfile , "\n Summary"
142
158
show_summary (counter , opts .num_summary , outfile )
159
+ outfile .close ()
143
160
144
161
145
162
if __name__ == '__main__' :
0 commit comments