This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
/
mysqlprocgrep.py
executable file
·163 lines (142 loc) · 5.83 KB
/
mysqlprocgrep.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python
#
# Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
"""
This file contains the procgrep utility which allows users to search process
information.
"""
import os.path
import sys
from mysql.utilities.common.tools import check_python_version
from mysql.utilities.exception import EmptyResultError
from mysql.utilities.command.proc import (ProcessGrep, KILL_CONNECTION,
KILL_QUERY, PRINT_PROCESS, ID, USER,
HOST, DB, COMMAND, INFO, STATE)
from mysql.utilities.common.tools import check_connector_python
from mysql.utilities.common.options import (add_regexp, setup_common_options,
add_verbosity, add_format_option,
add_character_set_option,
get_ssl_dict,
check_password_security)
# Check Python version compatibility
check_python_version()
def add_pattern(option, opt, value, parser, field):
"""Callback used in optparse for the --match-<pattern> option.
"""
entry = (field, value)
try:
getattr(parser.values, option.dest).append(entry)
except AttributeError:
setattr(parser.values, option.dest, [entry])
# Check for connector/python
if not check_connector_python():
sys.exit(1)
if __name__ == '__main__':
# Setup the command parser and setup server, help
parser = setup_common_options(os.path.basename(sys.argv[0]),
"mysqlprocgrep - search process information",
"%prog --server=user:pass@host:port:socket "
"[options]", True)
# Add character set option
add_character_set_option(parser)
# Add regexp
add_regexp(parser)
parser.add_option(
"-Q", "--print-sql", "--sql",
dest="print_sql", action="store_true", default=False,
help="print the statement instead of sending it to the server. If a "
"kill option is submitted, a procedure will be generated "
"containing the code for executing the kill."
)
parser.add_option(
"--sql-body",
dest="sql_body", action="store_true", default=False,
help="only print the body of the procedure."
)
parser.add_option(
"--kill-connection",
action="append_const", const=KILL_CONNECTION,
dest="actions", default=[],
help="kill all matching connections."
)
parser.add_option(
"--kill-query",
action="append_const", const=KILL_QUERY,
dest="actions", default=[],
help="kill query for all matching processes."
)
parser.add_option(
"--print",
action="append_const", const=PRINT_PROCESS,
dest="actions", default=[],
help="print all matching processes."
)
# Output format
add_format_option(parser, "display the output in either grid (default), "
"tab, csv, or vertical format", "grid")
# Add verbosity mode
add_verbosity(parser, False)
# Adding the --match-* options
for col in (ID, USER, HOST, DB, COMMAND, INFO, STATE):
parser.add_option(
"--match-" + col.lower(),
action="callback", callback=add_pattern, callback_args=(col,),
dest="matches", type="string", metavar="PATTERN", default=[],
help="match the '{0}' column of the PROCESSLIST table.".format(col)
)
parser.add_option(
"--age",
dest="age", default=None,
help="show only processes that have been in the current state more "
"than a given time."
)
(options, args) = parser.parse_args()
# Check security settings
check_password_security(options, args)
# Print SQL if only --sql-body is given
if options.sql_body:
options.print_sql = True
if (options.server is None or len(options.server) == 0) \
and not options.print_sql:
parser.error("You need at least one server if you're not using the "
"--sql option")
elif options.server is not None and len(options.server) > 0 \
and options.print_sql:
parser.error("You should not include servers in the call if you are "
"using the --sql option")
# If no option was supplied, we print the processes by default
if len(options.actions) == 0:
options.actions.append(PRINT_PROCESS)
try:
command = ProcessGrep(options.matches, options.actions,
options.use_regexp,
age=options.age)
if options.print_sql:
print(command.sql(options.sql_body).strip())
else:
ssl_opts = get_ssl_dict(options)
command.execute(options.server, format=options.format,
charset=options.charset, ssl_opts=ssl_opts)
except EmptyResultError:
_, details, _ = sys.exc_info()
sys.stderr.write("No matches\n")
sys.exit(1)
except:
_, details, _ = sys.exc_info()
sys.stderr.write('ERROR: {0}\n'.format(details))
sys.exit(2)
sys.exit()