Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #117 from debeando/mysql-top
Browse files Browse the repository at this point in the history
feat - mysql top
  • Loading branch information
nstrappazzonc authored Nov 26, 2023
2 parents 58ed637 + 5c120b5 commit 317b800
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 14 deletions.
5 changes: 3 additions & 2 deletions aws/database/describe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func NewCommand() *cobra.Command {
return
}

tbl := table.New("ATTRIBUTE", "VALUE")
tbl.Column(0, table.Column{Alignment: table.Right})
tbl := table.New()
tbl.Column(0, table.Column{Name: "ATTRIBUTE", Alignment: table.Right})
tbl.Column(1, table.Column{Name: "VALUE"})
for k, v := range instance.JSON() {
tbl.Add(k, v)
}
Expand Down
4 changes: 3 additions & 1 deletion aws/database/logs/list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ func NewCommand() *cobra.Command {
logs.SortBySize()
}

tbl := table.New("FILE", "SIZE")
tbl := table.New()
tbl.Column(0, table.Column{Name: "File"})
tbl.Column(1, table.Column{Name: "Size"})
for _, log := range logs {
tbl.Add(log.FileName, log.Size)
}
Expand Down
7 changes: 6 additions & 1 deletion aws/database/parameters/describe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ func NewCommand() *cobra.Command {
return
}

tbl := table.New("NAME", "VALUES", "APPLY METHOD", "APPLY TYPE", "MODIFIABLE")
tbl := table.New()
tbl.Column(0, table.Column{Name: "NAME"})
tbl.Column(1, table.Column{Name: "VALUES"})
tbl.Column(2, table.Column{Name: "APPLY METHOD"})
tbl.Column(3, table.Column{Name: "APPLY TYPE"})
tbl.Column(4, table.Column{Name: "MODIFIABLE"})
for _, parameter := range parameters {
tbl.Add(parameter.Name, parameter.Value, parameter.ApplyMethod, parameter.ApplyType, parameter.IsModifiable)
}
Expand Down
5 changes: 4 additions & 1 deletion aws/database/parameters/list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func NewCommand() *cobra.Command {
return
}

tbl := table.New("NAME", "DESCRIPTION", "FAMILY")
tbl := table.New()
tbl.Column(0, table.Column{Name: "NAME"})
tbl.Column(1, table.Column{Name: "DESCRIPTION"})
tbl.Column(2, table.Column{Name: "FAMILY"})
for _, parameter := range parameters {
tbl.Add(parameter.Name, parameter.Description, parameter.Family)
}
Expand Down
7 changes: 6 additions & 1 deletion aws/databases/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ func NewCommand() *cobra.Command {

instances := r.List()

tbl := table.New("ENGINE", "VERSION", "IDENTIFIER", "CLASS", "STATUS")
tbl := table.New()
tbl.Column(0, table.Column{Name: "ENGINE"})
tbl.Column(1, table.Column{Name: "VERSION"})
tbl.Column(2, table.Column{Name: "IDENTIFIER"})
tbl.Column(3, table.Column{Name: "CLASS"})
tbl.Column(4, table.Column{Name: "STATUS"})
for _, instance := range instances {
tbl.Add(instance.Engine, instance.Version, instance.Identifier, instance.Class, instance.Status)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module zenit
go 1.21

require (
github.com/aws/aws-sdk-go v1.48.0
github.com/debeando/go-common v0.4.7
github.com/aws/aws-sdk-go v1.48.3
github.com/debeando/go-common v0.4.9
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
github.com/kardianos/service v1.2.2
github.com/shirou/gopsutil v3.21.11+incompatible
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/aws/aws-sdk-go v1.47.3 h1:e0H6NFXiniCpR8Lu3lTphVdRaeRCDLAeRyTHd1tJSd8
github.com/aws/aws-sdk-go v1.47.3/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go v1.48.0 h1:1SeJ8agckRDQvnSCt1dGZYAwUaoD2Ixj6IaXB4LCv8Q=
github.com/aws/aws-sdk-go v1.48.0/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go v1.48.3 h1:btYjT+opVFxUbRz+qSCjJe07cdX82BHmMX/FXYmoL7g=
github.com/aws/aws-sdk-go v1.48.3/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -23,6 +25,8 @@ github.com/debeando/go-common v0.4.5 h1:XVE8thhXVdNoLuCOQokq3TZePxbPKv2vRqwBeKeK
github.com/debeando/go-common v0.4.5/go.mod h1:0u1RTk8umumGN+i+2V07g0itNWfDIUU19bsxEeFLMG4=
github.com/debeando/go-common v0.4.7 h1:TDWg1estW1Bah7TQvJ3qZvhP0RI3eG/2QbinuXdEGbw=
github.com/debeando/go-common v0.4.7/go.mod h1:h1hsqdnmtWyINN59EWVD58WmHEDg0gzMz8OdJs4mUH0=
github.com/debeando/go-common v0.4.9 h1:FcN8z4+qQQpxbZOzHoXh6zvVbQJkPhnh5KLujmkDiq4=
github.com/debeando/go-common v0.4.9/go.mod h1:h1hsqdnmtWyINN59EWVD58WmHEDg0gzMz8OdJs4mUH0=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
Expand Down
13 changes: 8 additions & 5 deletions mysql/digest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ func NewCommand() *cobra.Command {
queries.Analyzed(),
queries.Unique()))

tbl := table.New(
"DIGEST ID", "SCORE", "COUNT", "Q. TIME",
"L. TIME", "R. SENT", "R. EXAMINED",
)

tbl := table.New()
tbl.Column(0, table.Column{Name: "DIGEST ID"})
tbl.Column(1, table.Column{Name: "SCORE"})
tbl.Column(2, table.Column{Name: "COUNT"})
tbl.Column(3, table.Column{Name: "Q. TIME"})
tbl.Column(4, table.Column{Name: "L. TIME"})
tbl.Column(5, table.Column{Name: "R. SENT"})
tbl.Column(6, table.Column{Name: "R. EXAMINED"})
queries.Clean()
queries.SortByScore()

Expand Down
2 changes: 2 additions & 0 deletions mysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mysql

import (
"zenit/mysql/digest"
"zenit/mysql/top"

"github.com/spf13/cobra"
)
Expand All @@ -16,6 +17,7 @@ func NewCommand() *cobra.Command {
}

cmd.AddCommand(digest.NewCommand())
cmd.AddCommand(top.NewCommand())

return cmd
}
109 changes: 109 additions & 0 deletions mysql/top/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package top

import (
"fmt"

"github.com/debeando/go-common/aws/rds"
"github.com/debeando/go-common/cast"
"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"
)

var delay int
var dsn string
var host string
var password string
var port int64
var user string

const SQL_PROCESSLIST = `SELECT id, user, SUBSTRING_INDEX(host, ':', 1) AS 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');`

func NewCommand() *cobra.Command {
defer terminal.CursorShow()

var cmd = &cobra.Command{
Use: "top [IDENTIFIER] |",
Short: "Display MySQL server performance info like 'top'.",
Example: `
# Connect to RDS:
zenit mysql top test-rds --password=<password>
# Connect to Host:
zenit mysql top --host=127.0.0.1 --user=root --password=<password>
# Refresh every one second:
zenit mysql top --host=127.0.0.1 --user=root --password=<password> --delay=1`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 && len(password) == 0 {
cmd.Help()
return
} else if len(host) == 0 && len(password) == 0 {
cmd.Help()
return
}

if len(args) == 1 && len(args[0]) > 0 {
r := rds.RDS{}

if err := r.Init(); err != nil {
log.Error(err.Error())
return
}

instance, err := r.Describe(args[0])
if err != nil {
log.Error(err.Error())
return
}

host = instance.Endpoint
port = instance.Port
}

fmt.Printf("Connecting to %s:%d...\n", host, port)

dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/information_schema?timeout=3s", user, password, host, port)

m := mysql.New("top", dsn)
if err := m.Connect(); err != nil {
log.Error(err.Error())
return
}

terminal.Refresh(delay, func() bool {
processlist, _ := m.Query(SQL_PROCESSLIST)

tbl := table.New()
tbl.Title("Process List")
tbl.Column(0, table.Column{Name: "ID", Truncate: 10, Width: 10})
tbl.Column(1, table.Column{Name: "User", Truncate: 10, Width: 10})
tbl.Column(2, table.Column{Name: "Host", Truncate: 16, Width: 16})
tbl.Column(3, table.Column{Name: "Time", Truncate: 4, Width: 4})
tbl.Column(4, table.Column{Name: "Query", Truncate: 50, Width: 50})
for _, row := range processlist {
tbl.Add(row["id"], row["user"], row["host"], cast.StringToInt(row["time"]), row["info"])
}
tbl.SortBy(3).Print()

return true
})
},
}

cmd.Flags().IntVar(&delay, "delay", 3, "How long between display refreshes.")
cmd.Flags().StringVar(&host, "host", "127.0.0.1", "Connect to host.")
cmd.Flags().StringVar(&password, "password", "", "Password to use when connecting to server.")
cmd.Flags().Int64Var(&port, "port", 3306, "Port number to use for connection.")
cmd.Flags().StringVar(&user, "user", "root", "User for login if not current user.")

return cmd
}
2 changes: 1 addition & 1 deletion version/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Version is a const to have the latest version number for this code.
const VERSION string = "1.5.0-beta.1"
const VERSION string = "1.5.0-beta.4"

func NewCommand() *cobra.Command {
var cmd = &cobra.Command{
Expand Down

0 comments on commit 317b800

Please sign in to comment.