summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlinxin <[email protected]>2023-05-12 17:58:39 +0800
committerlinxin <[email protected]>2023-05-12 17:58:39 +0800
commit4fa0f88355cbcfa7c4df15593697238188227e58 (patch)
tree0f966444451c18c6c73bcde5875ce6caabb9b861
parentd1fb86fac0d4156f0b8476ecd96f4b057bedecf1 (diff)
增加展示container id字段,修改tool查询info文件逻辑1.0.5
-rw-r--r--coredump-handler/coredump-handler.go5
-rw-r--r--coredump-tool/coredump-tool.go213
-rw-r--r--types/types.go2
3 files changed, 124 insertions, 96 deletions
diff --git a/coredump-handler/coredump-handler.go b/coredump-handler/coredump-handler.go
index 914a5b1..5a3678c 100644
--- a/coredump-handler/coredump-handler.go
+++ b/coredump-handler/coredump-handler.go
@@ -125,6 +125,7 @@ func getImageName(container_id string, sock_path string) (string, error) {
if err != nil {
return "", err
}
+ // container_info,err:=container.Info(ctx)
// 获取容器关联的镜像信息
imageRef, err := container.Image(ctx)
if err != nil {
@@ -282,11 +283,15 @@ func main() {
container_id, err := getContainerId(coredump_config.Initial_ns_pid)
//根据查找到的container id查找对应的image name
if err == nil && len(container_id) != 0 {
+ coredump_config.Container_id = container_id
coredump_config.Image_name, err = getImageName(container_id, pipe_config.Containered_sock_path)
if err != nil {
journal.Print(journal.PriInfo, err.Error())
}
}
+ if coredump_config.Container_id == "" {
+ coredump_config.Container_id = "NULL"
+ }
if coredump_config.Image_name == "" {
coredump_config.Image_name = "NULL"
}
diff --git a/coredump-tool/coredump-tool.go b/coredump-tool/coredump-tool.go
index 6a86faf..d0908c7 100644
--- a/coredump-tool/coredump-tool.go
+++ b/coredump-tool/coredump-tool.go
@@ -28,19 +28,128 @@ import (
var configs []types.Coredump_config
-func terminalSize() (width, height int, err error) {
- file, err := os.OpenFile("/dev/tty", os.O_WRONLY, 0)
+// WalkDirectory 遍历文件目录并处理后缀名为.config的文件
+
+func WalkDirectory(dir string) {
+ err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ return err
+ }
+
+ if info.IsDir() && strings.HasPrefix(info.Name(), "coredump_") {
+ err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ return err
+ }
+
+ if !info.IsDir() && strings.HasSuffix(info.Name(), ".info") {
+ data, err := os.ReadFile(path)
+ if err != nil {
+ return err
+ }
+
+ var config types.Coredump_config
+ err = json.Unmarshal(data, &config)
+ if err != nil {
+ return err
+ }
+ if config.Container_id == "" {
+ config.Container_id = "NULL"
+ }
+ if config.Image_name == "" {
+ config.Image_name = "NULL"
+ }
+ configs = append(configs, config)
+ }
+
+ return nil
+ })
+
+ if err != nil {
+ return err
+ }
+ }
+
+ return nil
+ })
+
if err != nil {
- return 0, 0, err
+ fmt.Printf("Error walking directory %s: %v\n", dir, err)
}
- defer file.Close()
+}
- width, height, err = term.GetSize(int(file.Fd()))
- if err != nil {
- return 0, 0, err
+// func WalkDirectory(dir string) {
+// err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
+// if err != nil {
+// return err
+// }
+
+// if !info.IsDir() {
+// if strings.HasSuffix(info.Name(), ".info") {
+// data, err := os.ReadFile(path)
+// if err != nil {
+// return err
+// }
+
+// var config types.Coredump_config
+// err = json.Unmarshal(data, &config)
+// if err != nil {
+// return err
+// }
+// configs = append(configs, config)
+// }
+// }
+
+// return nil
+// })
+
+// if err != nil {
+// fmt.Printf("Error walking directory %s: %v\n", dir, err)
+// }
+// }
+func list(pid string) {
+ table := simpletable.New()
+
+ table.Header = &simpletable.Header{
+ Cells: []*simpletable.Cell{
+ {Text: "PID"},
+ {Text: "UID"},
+ {Text: "GID"},
+ {Text: "SIG"},
+ {Text: "EXE"},
+ {Text: "CONTAINER"},
+ {Text: "IMAGE"},
+ {Text: "HOSTNAME"},
+ {Text: "STORAGE"},
+ {Text: "TIMESTAMP"},
+ },
+ }
+ total := 0
+ // 输出配置信息
+ 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.UID},
+ {Text: c.GID},
+ {Text: strconv.Itoa(c.Signal)},
+ {Text: c.Process_exe_path},
+ {Text: c.Container_id},
+ {Text: c.Image_name},
+ {Text: c.Hostname},
+ {Text: c.Storage},
+ {Text: coreTime},
+ }
+ table.Body.Cells = append(table.Body.Cells, r)
+ total += 1
}
- return width, height, nil
+ fmt.Println(table.String())
+ fmt.Println("Total", total, "coredumps")
}
+
func debug(config types.Coredump_config, command string) error {
// 使用kubectl命令执行debug操作
if config.Image_name != "NULL" {
@@ -236,94 +345,6 @@ func debugInpod(conf *rest.Config, clientset *kubernetes.Clientset, config types
return podName, nil
}
-// WalkDirectory 遍历文件目录并处理后缀名为.config的文件
-func WalkDirectory(dir string) {
- err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
- if err != nil {
- return err
- }
-
- if !info.IsDir() {
- if strings.HasSuffix(info.Name(), ".info") {
- data, err := os.ReadFile(path)
- if err != nil {
- return err
- }
-
- var config types.Coredump_config
- err = json.Unmarshal(data, &config)
- if err != nil {
- return err
- }
- configs = append(configs, config)
- }
- }
-
- return nil
- })
-
- if err != nil {
- fmt.Printf("Error walking directory %s: %v\n", dir, err)
- }
-}
-func list(pid string) {
- table := simpletable.New()
-
- table.Header = &simpletable.Header{
- Cells: []*simpletable.Cell{
- {Text: "PID"},
- {Text: "UID"},
- {Text: "GID"},
- {Text: "SIG"},
- {Text: "EXE"},
- {Text: "IMAGE"},
- {Text: "HOSTNAME"},
- {Text: "STORAGE"},
- {Text: "TIMESTAMP"},
- },
- }
- total := 0
- // 输出配置信息
- 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.UID},
- {Text: c.GID},
- {Text: strconv.Itoa(c.Signal)},
- {Text: c.Process_exe_path},
- {Text: c.Image_name},
- {Text: c.Hostname},
- {Text: c.Storage},
- {Text: coreTime},
- }
- table.Body.Cells = append(table.Body.Cells, r)
- total += 1
- }
- fmt.Println(table.String())
- fmt.Println("Total", total, "coredumps")
-}
-
-// // Help 打印使用帮助
-// func Help() {
-// fmt.Println("Usage: coredump-ctl [list | debug | help] [options] ")
-// fmt.Println("list options:")
-// fmt.Println(" -dir string")
-// fmt.Println(" Directory path")
-// fmt.Println(" -p string")
-// fmt.Println(" Pid to matching")
-// fmt.Println("debug options:")
-// fmt.Println(" -dir string")
-// fmt.Println(" Directory path")
-// fmt.Println(" -p string")
-// fmt.Println(" Pid to matching")
-// fmt.Println(" -command string")
-// fmt.Println(" exe command when attach to pod")
-// }
-
func main() {
var (
pid string
diff --git a/types/types.go b/types/types.go
index 559af95..77c29b0 100644
--- a/types/types.go
+++ b/types/types.go
@@ -19,6 +19,7 @@ type Coredump_config struct {
Process_exe_path string
Timestamp int64
Corepipe_config_path string `json:"-"`
+ Container_id string
Image_name string
UID string
GID string
@@ -32,6 +33,7 @@ type Coredump_cli_table struct {
GID string
SIG int
EXE string
+ CONTAINER string
IMAGE string
HOSTNAME string
STORAGE string