This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicola Strappazzon C
committed
Nov 19, 2023
1 parent
58ed637
commit bf3ff00
Showing
9 changed files
with
130 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package top | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/debeando/go-common/aws/rds" | ||
"github.com/debeando/go-common/log" | ||
"github.com/debeando/go-common/mysql" | ||
"github.com/debeando/go-common/table" | ||
"github.com/debeando/go-common/terminal" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCommand() *cobra.Command { | ||
defer terminal.Show() | ||
|
||
var cmd = &cobra.Command{ | ||
Use: "top [IDENTIFIER]", | ||
Short: "Display MySQL server performance info like 'top'.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
cmd.Help() | ||
return | ||
} | ||
|
||
r := rds.RDS{} | ||
|
||
if err := r.Init(); err != nil { | ||
log.Error(err.Error()) | ||
return | ||
} | ||
|
||
instance, err := r.Describe(args[0]) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Printf("Connecting to %s:%d ...\n", instance.Endpoint, instance.Port) | ||
|
||
m := mysql.New("", fmt.Sprintf("monitor:monitor@tcp(%s:%d)/information_schema?timeout=3s", instance.Endpoint, instance.Port)) | ||
m.Connect() | ||
|
||
terminal.Reset() | ||
terminal.Clear() | ||
terminal.Flush() | ||
terminal.Hide() | ||
|
||
for i := 0; i < 100; i++ { | ||
// terminal.Reset() | ||
// terminal.Clear() | ||
// terminal.Flush() | ||
// Save the cursor position | ||
fmt.Print("\x1B7") // Save the cursor position | ||
fmt.Print("\x1B[2K") // Erase the entire line | ||
fmt.Print("\x1B[0J") // Erase from cursor to end of screen | ||
fmt.Print("\x1B[?47h") // Save screen | ||
fmt.Print("\x1B[1J") // Erase from cursor to beginning of screen | ||
fmt.Print("\x1B[?47l") // Restore screen | ||
defer fmt.Print("\x1B8") // Restore the cursor position util new size is calculated | ||
|
||
terminal.Cursor(0,0) | ||
// terminal.Refresh(func() { | ||
// ocultar la query de exploracion. | ||
processlist, _ := m.Query("SELECT id, user, host, db, command, time, state, info FROM information_schema.processlist WHERE id <> connection_id() AND length(info) > 0 AND command NOT IN ('Daemon', 'Sleep') AND user NOT IN ('rdsadmin');") | ||
|
||
tbl := table.New() | ||
tbl.Title("Process List") | ||
tbl.Column(0, table.Column{Name: "Time"}) | ||
tbl.Column(1, table.Column{Name: "Query", Truncate: 80, Width: 80}) | ||
for _, row := range processlist { | ||
tbl.Add(row["time"], row["info"]) | ||
} | ||
tbl.Print() | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
|
||
terminal.Show() | ||
|
||
// busca user y pass en config. | ||
// si existe las usa. | ||
// sino existen, usa las definidas en el env var. | ||
// poner sobre las env var en la help. | ||
|
||
// cnf := config.GetInstance() | ||
// cnf.Load() | ||
// for host := range cnf.Inputs.MySQL { | ||
// fmt.Println(cnf.Inputs.MySQL[host]) | ||
// } | ||
}, | ||
} | ||
|
||
return cmd | ||
} |