238 lines
5.7 KiB
Go
238 lines
5.7 KiB
Go
package main
|
||
|
||
import (
|
||
"archive/zip"
|
||
"context"
|
||
"coredump-handler/config"
|
||
"errors"
|
||
"fmt"
|
||
"io"
|
||
"io/ioutil"
|
||
"os"
|
||
"regexp"
|
||
"strconv"
|
||
"syscall"
|
||
|
||
"github.com/containerd/containerd"
|
||
"github.com/containerd/containerd/namespaces"
|
||
"github.com/coreos/go-systemd/v22/journal"
|
||
)
|
||
|
||
const chunkSize = 1024 * 1024 * 1024 // 1GB
|
||
|
||
func isMemorySufficient(pipe_config config.Pipeconfig) (bool, error) {
|
||
percent, err := strconv.ParseFloat(pipe_config.Total_file_mem_limit[:len(pipe_config.Total_file_mem_limit)-1], 64)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
percent = percent / 100.0
|
||
var stat syscall.Statfs_t
|
||
wd, err := syscall.Getwd()
|
||
if err != nil {
|
||
fmt.Println(err)
|
||
return false, err
|
||
}
|
||
syscall.Statfs(wd, &stat)
|
||
// 剩余空间的大小为块的数量 * 每个块的大小
|
||
// stat.Bfree表示可用的块的数量,stat.Bsize表示每个块的大小
|
||
usedSpace := (int64(stat.Blocks) - int64(stat.Bfree))
|
||
totalSpace := int64(stat.Blocks)
|
||
usage := float64(usedSpace) / float64(totalSpace)
|
||
if usage >= percent {
|
||
return false, nil
|
||
}
|
||
return true, nil
|
||
}
|
||
func createCoreDumpDir(pipe_config *config.Pipeconfig, args []string) error {
|
||
pipe_config.File_base_path = fmt.Sprintf("%s/%s_%s_%s", pipe_config.File_base_path, args[1], args[2], args[3])
|
||
dirName := pipe_config.File_base_path
|
||
if _, err := os.Stat(dirName); os.IsNotExist(err) {
|
||
// 目录不存在,创建目录
|
||
if err := os.MkdirAll(dirName, os.ModePerm); err != nil {
|
||
return err
|
||
}
|
||
} else {
|
||
return errors.New("directory already exists")
|
||
}
|
||
return nil
|
||
}
|
||
func changeDirectory(dir string) error {
|
||
if err := os.Chdir(dir); err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
func getContainerId(pid string) (string, error) {
|
||
cgroup_path := fmt.Sprintf("/proc/%s/cgroup", pid)
|
||
content, err := ioutil.ReadFile(cgroup_path)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
re := regexp.MustCompile(`([a-f\d]{64})`)
|
||
match := re.FindStringSubmatch(string(content))
|
||
if len(match) < 2 {
|
||
return "", errors.New("failed to extract container ID from cgroup file")
|
||
}
|
||
containerID := match[1]
|
||
return containerID, nil
|
||
}
|
||
func getImageId(container_id string, sock_path string) (string, error) {
|
||
// 连接 containerd daemon
|
||
client, err := containerd.New(sock_path)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
defer client.Close()
|
||
// 根据容器 ID 获取容器信息
|
||
ctx := namespaces.WithNamespace(context.Background(), "k8s.io")
|
||
container, err := client.LoadContainer(ctx, container_id)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
// 获取容器关联的镜像信息
|
||
imageRef, err := container.Image(ctx)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
return imageRef.Name(), nil
|
||
}
|
||
func writeCoreConfig(data string) error {
|
||
err := ioutil.WriteFile("coredump.config", []byte(data), 0666)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
fmt.Printf("Wrote %d bytes to file\n", len(data))
|
||
return nil
|
||
}
|
||
func writeCoreDumpToFile() error {
|
||
file, err := os.Create("coredump.info")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer file.Close()
|
||
|
||
// 从标准输入流中读取数据,并将其写入文件中
|
||
buf := make([]byte, 1024)
|
||
for {
|
||
n, err := io.ReadAtLeast(os.Stdin, buf, 1)
|
||
if err != nil && err != io.EOF {
|
||
return err
|
||
}
|
||
if n == 0 {
|
||
break
|
||
}
|
||
if _, err := file.Write(buf[:n]); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
func compress() error {
|
||
// Create a new zip archive.
|
||
dest := "coredump.info"
|
||
zipfile, err := os.Create(dest + ".zip")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer zipfile.Close()
|
||
|
||
// Create a new zip writer.
|
||
zipwriter := zip.NewWriter(zipfile)
|
||
defer zipwriter.Close()
|
||
|
||
// Create a zip file header.
|
||
header := &zip.FileHeader{
|
||
Name: dest,
|
||
Method: zip.Deflate,
|
||
}
|
||
|
||
// Write the header to the zip file.
|
||
writer, err := zipwriter.CreateHeader(header)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// Copy the dataStream to the zip file in chunks.
|
||
buf := make([]byte, 1024)
|
||
for {
|
||
n, err := io.ReadAtLeast(os.Stdin, buf, 1)
|
||
if err != nil && err != io.EOF {
|
||
return err
|
||
}
|
||
if n == 0 {
|
||
break
|
||
}
|
||
_, err = writer.Write(buf[:n])
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
func main() {
|
||
//切换到config文件路径
|
||
err := changeDirectory("/root/pipe/corepipe")
|
||
if err != nil {
|
||
journal.Print(journal.PriErr, err.Error())
|
||
return
|
||
}
|
||
//读取config文件并初始化
|
||
pipe_config, err := config.PipeInit()
|
||
if err != nil {
|
||
journal.Print(journal.PriErr, err.Error())
|
||
return
|
||
}
|
||
//判断硬盘使用率
|
||
flag, err := isMemorySufficient(pipe_config)
|
||
if err != nil && !flag {
|
||
journal.Print(journal.PriErr, err.Error())
|
||
return
|
||
}
|
||
//判断参数传入是否符合要求
|
||
if len(os.Args) < 4 {
|
||
journal.Print(journal.PriErr, "parameter passing exception")
|
||
return
|
||
}
|
||
//创建存储coredump内容文件夹
|
||
err = createCoreDumpDir(&pipe_config, os.Args)
|
||
if err != nil {
|
||
journal.Print(journal.PriErr, err.Error())
|
||
return
|
||
}
|
||
//切换至存储coredump目录
|
||
err = changeDirectory(pipe_config.File_base_path)
|
||
if err != nil {
|
||
journal.Print(journal.PriErr, err.Error())
|
||
return
|
||
}
|
||
//查找发生coredump进程对应的container id
|
||
container_id, err := getContainerId(os.Args[1])
|
||
//根据查找到的container id查找对应的image name
|
||
var image_id string
|
||
if err == nil && len(container_id) != 0 {
|
||
image_id, err = getImageId(container_id, pipe_config.Containered_sock_path)
|
||
if err != nil {
|
||
journal.Print(journal.PriInfo, err.Error())
|
||
}
|
||
}
|
||
//将image name写入coredump config
|
||
err = writeCoreConfig(image_id)
|
||
if err != nil {
|
||
journal.Print(journal.PriInfo, err.Error())
|
||
}
|
||
//根据配置项选择存储coredump文件方式
|
||
if pipe_config.Save_model == 0 {
|
||
err = writeCoreDumpToFile()
|
||
if err != nil {
|
||
journal.Print(journal.PriErr, err.Error())
|
||
}
|
||
} else if pipe_config.Save_model == 1 {
|
||
err = compress()
|
||
if err != nil {
|
||
journal.Print(journal.PriErr, err.Error())
|
||
}
|
||
}
|
||
}
|