coredump-tool增加展示时对数据排序,精简list命令展示内容。增加info指令

This commit is contained in:
linxin
2023-05-26 16:29:09 +08:00
parent 8a39fe00c5
commit e159360b3e
2 changed files with 64 additions and 2 deletions

Binary file not shown.

View File

@@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
@@ -59,6 +60,9 @@ func WalkDirectory(dir string) {
if config.Image_name == "" {
config.Image_name = "NULL"
}
if _, err := os.Stat(config.Storage); err != nil {
config.Storage = "MISS"
}
configs = append(configs, config)
}
@@ -72,7 +76,9 @@ func WalkDirectory(dir string) {
return nil
})
sort.Slice(configs, func(i, j int) bool {
return configs[i].Timestamp < configs[j].Timestamp
})
if err != nil {
fmt.Printf("Error walking directory %s: %v\n", dir, err)
}
@@ -80,6 +86,37 @@ func WalkDirectory(dir string) {
func list(pid string) {
table := simpletable.New()
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Text: "PID"},
{Text: "EXE"},
{Text: "IMAGE"},
{Text: "TIMESTAMP"},
},
}
total := 0
// output the config's info
for _, c := range configs {
if pid != "" && strings.Compare(c.Initial_ns_pid, pid) != 0 {
continue
}
coreTime := time.Unix(c.Timestamp, 0).Format("2006-01-02 15:04:05")
r := []*simpletable.Cell{
{Text: c.Initial_ns_pid},
{Text: c.Process_exe_path},
{Text: c.Image_name},
{Text: coreTime},
}
table.Body.Cells = append(table.Body.Cells, r)
total += 1
}
fmt.Println(table.String())
fmt.Println("Total", total, "coredumps")
}
func info(pid string) {
table := simpletable.New()
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Text: "PID"},
@@ -121,7 +158,6 @@ func list(pid string) {
}
func debug(config types.Coredump_config, command string) error {
// using kubectl to create a pod
if strings.HasSuffix(config.Storage, ".minidump") {
corefile := strings.Replace(config.Storage, ".minidump", ".coredump", -1)
cmd := exec.Command("minidump-2-core", "-o", corefile, config.Storage)
@@ -361,6 +397,32 @@ func main() {
return nil
},
},
{
Name: "info",
Aliases: []string{"i"},
Usage: "Display all coredump files's info",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pid",
Aliases: []string{"p"},
Usage: "Pid to match",
Value: "",
Destination: &pid,
},
&cli.StringFlag{
Name: "dir",
Aliases: []string{"d"},
Usage: "Coredump directory path(default: /var/lib/coredump)",
Value: "/var/lib/coredump",
Destination: &dirPath,
},
},
Action: func(c *cli.Context) error {
WalkDirectory(dirPath)
info(pid)
return nil
},
},
{
Name: "debug",
Usage: "Start a debugging session for a coredump",