-
Notifications
You must be signed in to change notification settings - Fork 11
/
kill-mysql-query.go
203 lines (163 loc) · 4.53 KB
/
kill-mysql-query.go
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"fmt"
"os"
"strings"
"github.com/mugli/go-kill-mysql-query/configuration"
"github.com/mugli/go-kill-mysql-query/mysql"
"github.com/gookit/color"
"github.com/jmoiron/sqlx"
"github.com/manifoldco/promptui"
)
func main() {
var config configuration.Config
if len(os.Args) >= 2 {
switch os.Args[1] {
case "generate", "init":
generateConfig()
case "help", "-h", "--help":
showHelp()
default:
config = readConfig(os.Args[1])
}
} else {
config = readConfig("")
}
killQueries(config)
}
func readConfig(filePath string) configuration.Config {
config, err := configuration.Read(filePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return config
}
func killQueries(config configuration.Config) {
dbConn, err := mysql.Connect(config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer mysql.Disconnect()
for {
longQueries, err := mysql.GetLongRunningQueries(dbConn, config)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
showKillPrompt(longQueries, dbConn, config)
fmt.Println("💫 Rechecking...")
}
}
func generateConfig() {
var file string
if len(os.Args) > 2 {
file = os.Args[2]
}
err := configuration.Generate(file)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}
func showHelp() {
help := `
_____ ____
/ \ | o |
| |/ ___\|
|_________/
|_|_| |_|_|
kill-mysql-query interactively shows long running queries in MySQL database
and provides option to kill them one by one. Great for firefighting. 🔥🚨🚒
It can connect to MySQL server as configured, using SSH Tunnel if necessary
and let you decide which query to kill. By default queries running for more
than 10 seconds will be marked as long running queries, but it can be configured.
------
Usage:
kill-mysql-query [config.toml]:
Checks for long running queries in the configured server.
If no file is given, it tries to read from config.toml
in the current directory.
Other commands:
generate [config.toml]:
Generates a new empty configuration file
init:
Alias for generate
help, --help, -h:
Shows this message
`
fmt.Println(help)
os.Exit(0)
}
func showKillPrompt(longQueries []mysql.MysqlProcess, dbConn *sqlx.DB, config configuration.Config) {
if len(longQueries) == 0 {
fmt.Printf("✨ No queries are running for more than %d second(s). Quitting! 👋\n", config.LongQuery.TimeoutSecond)
os.Exit(0)
}
cyan := color.FgCyan.Render
green := color.FgGreen.Render
if len(longQueries) == 1 {
fmt.Println()
fmt.Println()
fmt.Printf("❄️ Found %s long running query!\n", cyan("1"))
query := longQueries[0]
label := fmt.Sprintf("🐢 This query is running for %s second(s) in the `%s` database:\n\n%s\n\n", cyan(query.Time), cyan(query.DB), cyan(query.TruncatedQuery))
fmt.Println(label)
prompt := promptui.Prompt{
Label: "🧨 Kill it?",
IsConfirm: true,
Default: "n",
}
result, _ := prompt.Run()
if strings.TrimSpace(strings.ToLower(result)) == "y" {
err := mysql.KillMySQLProcess(query.KillCommand, dbConn)
if err != nil {
fmt.Printf("😓 There was an error killing the query: %v\n", err)
os.Exit(1)
}
} else {
fmt.Println("Quitting! 👋")
os.Exit(0)
}
}
if len(longQueries) > 1 {
fmt.Println()
fmt.Println()
fmt.Printf("❄️ Found %s long running queries!\n", cyan(len(longQueries)))
fmt.Println()
templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "👉 DB `{{ .DB | cyan }}`, Running Time: {{ .Time | cyan }}s, Query: {{ .TruncatedQuery | cyan }}",
Inactive: " DB `{{ .DB }}`, Running Time: {{ .Time }}s, Query: {{ .TruncatedQuery }}",
Selected: "💥 DB `{{ .DB | cyan }}`, Running Time: {{ .Time | cyan }}s, Query: {{ .TruncatedQuery | cyan }}",
Details: `
--------- QUERY ----------
{{ "ID:" | faint }} {{ .ID }}
{{ "DB:" | faint }} {{ .DB }}
{{ "State:" | faint }} {{ .State.String }}
{{ "Command:" | faint }} {{ .Command }}
{{ "Running Time:" | faint }} {{ .Time }} second(s)
{{ "Query:" | faint }} {{ .TruncatedQuery }}`,
}
label := fmt.Sprintf("Press %s to confirm. Which one to kill?", green("ENTER"))
prompt := promptui.Select{
Label: label,
Items: longQueries,
Templates: templates,
Size: 10,
}
i, _, err := prompt.Run()
if err != nil {
fmt.Printf("😓 There was an error: %v\n", err)
os.Exit(1)
}
selectedQuery := longQueries[i]
err = mysql.KillMySQLProcess(selectedQuery.KillCommand, dbConn)
if err != nil {
fmt.Printf("😓 There was an error killing the query: %v\n", err)
os.Exit(1)
}
}
}