|
| 1 | +// Copyright © 2017 Hong Bin <[email protected]> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package cmd |
| 22 | + |
| 23 | +import ( |
| 24 | + "database/sql" |
| 25 | + "fmt" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + "os" |
| 28 | + "os/signal" |
| 29 | + "path" |
| 30 | + "path/filepath" |
| 31 | + "strconv" |
| 32 | + // "strings" |
| 33 | + "syscall" |
| 34 | + "time" |
| 35 | +) |
| 36 | + |
| 37 | +// slowlogCmd represents the slowlog command |
| 38 | +var slowlogCmd = &cobra.Command{ |
| 39 | + Use: "slowlog", |
| 40 | + Short: "Capture MySQL slow log ", |
| 41 | + Long: `Custom query time threshold, capture MySQL slow log |
| 42 | +For example: Set query time threshold 0.1s, capture slow log for 60 seconds. |
| 43 | +$ mysqldba slowlog -u user -p pass -d /path -l 60 -t 0.1`, |
| 44 | + Run: func(cmd *cobra.Command, args []string) { |
| 45 | + run() |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +func run() { |
| 50 | + db := mysqlConnect() |
| 51 | + res := getLogConfig(db) |
| 52 | + getSlowLog(db, res) |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +var ( |
| 57 | + dirPath string |
| 58 | + duration int |
| 59 | + queryTime float32 |
| 60 | +) |
| 61 | + |
| 62 | +func init() { |
| 63 | + RootCmd.AddCommand(slowlogCmd) |
| 64 | + |
| 65 | + slowlogCmd.Flags().IntVarP(&duration, "long", "l", 60, "How long is capture") |
| 66 | + slowlogCmd.Flags().Float32VarP(&queryTime, "time", "t", 0, "Long query time threshold") |
| 67 | + slowlogCmd.Flags().StringVarP(&dirPath, "dir", "d", ".", "Specify save directory") |
| 68 | +} |
| 69 | + |
| 70 | +func getLogConfig(db *sql.DB) map[string]string { |
| 71 | + var r = make(map[string]string) |
| 72 | + rows, err := db.Query(globalVariableSQL) |
| 73 | + ifErrWithLog(err) |
| 74 | + defer rows.Close() |
| 75 | + |
| 76 | + for rows.Next() { |
| 77 | + var n, v string |
| 78 | + err := rows.Scan(&n, &v) |
| 79 | + ifErrWithLog(err) |
| 80 | + r[n] = v |
| 81 | + } |
| 82 | + err = rows.Err() |
| 83 | + ifErrWithLog(err) |
| 84 | + return r |
| 85 | +} |
| 86 | + |
| 87 | +func restoreOption(db *sql.DB, r map[string]string) { |
| 88 | + |
| 89 | + sql, err := strconv.ParseFloat(r["long_query_time"], 32) |
| 90 | + ifErrWithLog(err) |
| 91 | + |
| 92 | + _, err = db.Exec("SET GLOBAL LONG_QUERY_TIME=?", sql) |
| 93 | + ifErrWithLog(err) |
| 94 | + |
| 95 | + db.Exec("SET GLOBAL SLOW_QUERY_LOG=?", r["slow_query_log"]) |
| 96 | + db.Exec("SET GLOBAL LOG_OUTPUT=?", r["log_output"]) |
| 97 | +} |
| 98 | + |
| 99 | +func getSlowLog(db *sql.DB, r map[string]string) { |
| 100 | + |
| 101 | + c := make(chan os.Signal, 2) |
| 102 | + signal.Notify(c, os.Interrupt, syscall.SIGTERM) |
| 103 | + go func() { |
| 104 | + <-c |
| 105 | + fmt.Println("Configuration restored. Exiting..") |
| 106 | + restoreOption(db, r) |
| 107 | + |
| 108 | + os.Exit(1) |
| 109 | + }() |
| 110 | + |
| 111 | + fmt.Printf("Original configuration \n slow_query_log: %v\n long_query_time: %v\n log_output: %v\n", r["slow_query_log"], r["long_query_time"], r["log_output"]) |
| 112 | + |
| 113 | + _, err := db.Exec("SET GLOBAL SLOW_QUERY_LOG=ON") |
| 114 | + ifErrWithLog(err) |
| 115 | + db.Exec("SET GLOBAL LOG_OUTPUT=FILE") |
| 116 | + db.Exec("FLUSH SLOW LOGS") |
| 117 | + db.Exec("SET GLOBAL LONG_QUERY_TIME=?", queryTime) |
| 118 | + var slowLogFile string |
| 119 | + if path.Dir(r["slow_query_log_file"]) == "." { |
| 120 | + slowLogFile = fmt.Sprintf("%s%s", r["datadir"], r["slow_query_log_file"]) |
| 121 | + } else { |
| 122 | + slowLogFile = r["slow_query_log_file"] |
| 123 | + } |
| 124 | + |
| 125 | + for i := 1; i <= duration; i++ { |
| 126 | + fmt.Fprintf(os.Stdout, "Query is recording to %v %vs.\r", slowLogFile, i) |
| 127 | + time.Sleep(time.Second) |
| 128 | + } |
| 129 | + |
| 130 | + restoreOption(db, r) |
| 131 | + |
| 132 | + tarFile := fmt.Sprintf("%s/%s_%vs_%s.tar.gz", dirPath, filepath.Base(r["slow_query_log_file"]), queryTime, time.Now().Format("20060102150405")) |
| 133 | + err = tarIt(slowLogFile, tarFile) |
| 134 | + ifErrWithLog(err) |
| 135 | + fmt.Printf("\nConfiguration restored. Archived to %v\n", tarFile) |
| 136 | + |
| 137 | +} |
0 commit comments