mysql-queries-killer: use a config file

This commit is contained in:
Jérémy Lecour 2021-08-31 11:58:52 +02:00 committed by Jérémy Lecour
parent b908fc6cee
commit e76f2fe448

View file

@ -1,12 +1,12 @@
#!/bin/sh #!/bin/sh
VERSION="21.07.1" VERSION="21.08"
show_version() { show_version() {
cat <<END cat <<END
mysql-queries-killer version ${VERSION} mysql-queries-killer version ${VERSION}
Copyright 2021-2021 Evolix <info@evolix.fr>, Copyright 2018-2021 Evolix <info@evolix.fr>,
Jérémy Lecour <jlecour@evolix.fr> Jérémy Lecour <jlecour@evolix.fr>
and others. and others.
@ -23,34 +23,68 @@ mysql-queries-killer can list/kill SQL queries
END END
show_usage show_usage
} }
show_usage() { show_usage() {
cat <<END cat <<END
Usage: mysql-queries-killer [--port <port>] --list [--time <time>] Usage: mysql-queries-killer [--instance <instance>] --list [--time <time>]
or mysql-queries-killer [--port <port>] --kill [--time <time>] or mysql-queries-killer [--instance <instance>] --kill [--time <time>]
Options Options
--port MySQL port (default: 3306) --instance MySQL instance name (see below)
--time query busy time, in seconds (default: 600) --time query busy time, in seconds (default: 600)
--list list matching queries (default action) --list list matching queries (default action)
--kill kill connexions related to matching queries --kill kill connexions related to matching queries
--help Print this message and exit --help Print this message and exit
--version Print version and exit --version Print version and exit
If an instance parameter is provided, pt-kill will look for a configuration
file at ~/.pt-kill.<instance>.cnf
If no instance parameter is provided, pt-kill will use the conventional
configuration files.
END END
} }
error() {
>&2 echo "mysql-queries-killer: $1"
exit
}
kill_connexions() { kill_connexions() {
cmd="${PTKILL_BIN} --host 127.0.0.1 --port ${port} --busy-time ${time} --kill --print" cmd="${PTKILL_BIN}"
if [ -n "${config_file}" ]; then
cmd="${cmd} --config ${config_file}"
fi
cmd="${cmd} --busy-time ${time} --run-time 1 --interval 1 --print --kill"
${cmd} ${cmd}
} }
list_queries() { list_queries() {
cmd="${PTKILL_BIN} --host 127.0.0.1 --port ${port} --busy-time ${time} --print" cmd="${PTKILL_BIN}"
if [ -n "${config_file}" ]; then
cmd="${cmd} --config ${config_file}"
fi
cmd="${cmd} --busy-time ${time} --run-time 1 --interval 1 --print"
${cmd} ${cmd}
} }
set_config_file() {
config_file=""
if [ -n "${instance}" ]; then
file="${HOME}/.pt-kill.${instance}.cnf"
if [ ! -f "${file}" ]; then
error "The config file \`${file}' doesn't exist." 2
else
config_file="${file}"
fi
fi
}
main() { main() {
case ${action} in case ${action} in
kill) kill)
@ -89,23 +123,23 @@ while :; do
--list) --list)
action="list" action="list"
;; ;;
--port) --instance)
# with value separated by space # with value separated by space
if [ -n "$2" ]; then if [ -n "$2" ]; then
port=$2 instance=$2
shift shift
else else
printf 'ERROR: "--port" requires a non-empty option argument.\n' >&2 printf 'ERROR: "--instance" requires a non-empty option argument.\n' >&2
exit 1 exit 1
fi fi
;; ;;
--port=?*) --instance=?*)
# with value speparated by = # with value speparated by =
port=${1#*=} instance=${1#*=}
;; ;;
--port=) --instance=)
# without value # without value
printf 'ERROR: "--port" requires a non-empty option argument.\n' >&2 printf 'ERROR: "--instance" requires a non-empty option argument.\n' >&2
exit 1 exit 1
;; ;;
--time) --time)
@ -151,18 +185,13 @@ done
# Initial values # Initial values
action=${action:-} action=${action:-}
time=${time:-"600"} time=${time:-"600"}
port=${port:-"3306"} instance=${instance:-""}
set_config_file
set -u set -u
set -e set -e
if [ -z "${port}" ]; then
echo "You must provide an port name" >&2
echo "" >&2
show_usage >&2
exit 1
fi
main main
exit 0 exit 0