修改日志打印方式为直接写入systemd
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"archive/zip"
|
"archive/zip"
|
||||||
"context"
|
"context"
|
||||||
"coredump-handler/config"
|
"coredump-handler/config"
|
||||||
"coredump-handler/logger"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -16,6 +15,7 @@ import (
|
|||||||
|
|
||||||
"github.com/containerd/containerd"
|
"github.com/containerd/containerd"
|
||||||
"github.com/containerd/containerd/namespaces"
|
"github.com/containerd/containerd/namespaces"
|
||||||
|
"github.com/coreos/go-systemd/v22/journal"
|
||||||
)
|
)
|
||||||
|
|
||||||
const chunkSize = 1024 * 1024 * 1024 // 1GB
|
const chunkSize = 1024 * 1024 * 1024 // 1GB
|
||||||
@@ -80,7 +80,6 @@ func getImageId(container_id string, sock_path string) (string, error) {
|
|||||||
// 连接 containerd daemon
|
// 连接 containerd daemon
|
||||||
client, err := containerd.New(sock_path)
|
client, err := containerd.New(sock_path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Println(err)
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
@@ -88,13 +87,11 @@ func getImageId(container_id string, sock_path string) (string, error) {
|
|||||||
ctx := namespaces.WithNamespace(context.Background(), "k8s.io")
|
ctx := namespaces.WithNamespace(context.Background(), "k8s.io")
|
||||||
container, err := client.LoadContainer(ctx, container_id)
|
container, err := client.LoadContainer(ctx, container_id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Println(err)
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// 获取容器关联的镜像信息
|
// 获取容器关联的镜像信息
|
||||||
imageRef, err := container.Image(ctx)
|
imageRef, err := container.Image(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Println(err)
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return imageRef.Name(), nil
|
return imageRef.Name(), nil
|
||||||
@@ -173,46 +170,41 @@ func compress() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//切换到config文件路径
|
//切换到config文件路径
|
||||||
err := changeDirectory("/root/pipe/corepipe")
|
err := changeDirectory("/root/pipe/corepipe")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
journal.Print(journal.PriErr, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//读取config文件并初始化
|
//读取config文件并初始化
|
||||||
pipe_config, err := config.PipeInit()
|
pipe_config, err := config.PipeInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
journal.Print(journal.PriErr, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//判断硬盘使用率
|
//判断硬盘使用率
|
||||||
flag, err := isMemorySufficient(pipe_config)
|
flag, err := isMemorySufficient(pipe_config)
|
||||||
if err != nil && !flag {
|
if err != nil && !flag {
|
||||||
fmt.Println(err)
|
journal.Print(journal.PriErr, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//判断参数传入是否符合要求
|
//判断参数传入是否符合要求
|
||||||
if len(os.Args) < 4 {
|
if len(os.Args) < 4 {
|
||||||
fmt.Println("parameter passing exception")
|
journal.Print(journal.PriErr, "parameter passing exception")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//创建存储coredump内容文件夹
|
//创建存储coredump内容文件夹
|
||||||
err = createCoreDumpDir(&pipe_config, os.Args)
|
err = createCoreDumpDir(&pipe_config, os.Args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
journal.Print(journal.PriErr, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//切换至存储coredump目录
|
//切换至存储coredump目录
|
||||||
err = changeDirectory(pipe_config.File_base_path)
|
err = changeDirectory(pipe_config.File_base_path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
journal.Print(journal.PriErr, err.Error())
|
||||||
return
|
|
||||||
}
|
|
||||||
//日志功能初始化
|
|
||||||
err = logger.Init()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//查找发生coredump进程对应的container id
|
//查找发生coredump进程对应的container id
|
||||||
@@ -222,24 +214,24 @@ func main() {
|
|||||||
if err == nil && len(container_id) != 0 {
|
if err == nil && len(container_id) != 0 {
|
||||||
image_id, err = getImageId(container_id, pipe_config.Containered_sock_path)
|
image_id, err = getImageId(container_id, pipe_config.Containered_sock_path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Println(err)
|
journal.Print(journal.PriInfo, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//将image name写入coredump config
|
//将image name写入coredump config
|
||||||
err = writeCoreConfig(image_id)
|
err = writeCoreConfig(image_id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Println(err)
|
journal.Print(journal.PriInfo, err.Error())
|
||||||
}
|
}
|
||||||
//根据配置项选择存储coredump文件方式
|
//根据配置项选择存储coredump文件方式
|
||||||
if pipe_config.Save_model == 0 {
|
if pipe_config.Save_model == 0 {
|
||||||
err = writeCoreDumpToFile()
|
err = writeCoreDumpToFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Println(err)
|
journal.Print(journal.PriErr, err.Error())
|
||||||
}
|
}
|
||||||
} else if pipe_config.Save_model == 1 {
|
} else if pipe_config.Save_model == 1 {
|
||||||
err = compress()
|
err = compress()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Println(err)
|
journal.Print(journal.PriErr, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ func TestGetImageId(t *testing.T) {
|
|||||||
name: "get image id successfully",
|
name: "get image id successfully",
|
||||||
input: input{
|
input: input{
|
||||||
container_id: "invalid",
|
container_id: "invalid",
|
||||||
sock_path: "/run/containerd/containerd.sock",
|
sock_path: "/run/k3s/containerd/containerd.sock",
|
||||||
},
|
},
|
||||||
want: "",
|
want: "",
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
|
|||||||
6
go.mod
6
go.mod
@@ -2,7 +2,10 @@ module coredump-handler
|
|||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require github.com/containerd/containerd v1.7.0
|
require (
|
||||||
|
github.com/containerd/containerd v1.7.0
|
||||||
|
github.com/coreos/go-systemd/v22 v22.5.0
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 // indirect
|
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 // indirect
|
||||||
@@ -33,7 +36,6 @@ require (
|
|||||||
github.com/opencontainers/runc v1.1.4 // indirect
|
github.com/opencontainers/runc v1.1.4 // indirect
|
||||||
github.com/opencontainers/runtime-spec v1.1.0-rc.1 // indirect
|
github.com/opencontainers/runtime-spec v1.1.0-rc.1 // indirect
|
||||||
github.com/opencontainers/selinux v1.11.0 // indirect
|
github.com/opencontainers/selinux v1.11.0 // indirect
|
||||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/sirupsen/logrus v1.9.0 // indirect
|
github.com/sirupsen/logrus v1.9.0 // indirect
|
||||||
go.opencensus.io v0.24.0 // indirect
|
go.opencensus.io v0.24.0 // indirect
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -27,6 +27,8 @@ github.com/containerd/ttrpc v1.2.1/go.mod h1:sIT6l32Ph/H9cvnJsfXM5drIVzTr5A2flTf
|
|||||||
github.com/containerd/typeurl/v2 v2.1.0 h1:yNAhJvbNEANt7ck48IlEGOxP7YAp6LLpGn5jZACDNIE=
|
github.com/containerd/typeurl/v2 v2.1.0 h1:yNAhJvbNEANt7ck48IlEGOxP7YAp6LLpGn5jZACDNIE=
|
||||||
github.com/containerd/typeurl/v2 v2.1.0/go.mod h1:IDp2JFvbwZ31H8dQbEIY7sDl2L3o3HZj1hsSQlywkQ0=
|
github.com/containerd/typeurl/v2 v2.1.0/go.mod h1:IDp2JFvbwZ31H8dQbEIY7sDl2L3o3HZj1hsSQlywkQ0=
|
||||||
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||||
|
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
|
||||||
|
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||||
github.com/cyphar/filepath-securejoin v0.2.3 h1:YX6ebbZCZP7VkM3scTTokDgBL2TY741X51MTk3ycuNI=
|
github.com/cyphar/filepath-securejoin v0.2.3 h1:YX6ebbZCZP7VkM3scTTokDgBL2TY741X51MTk3ycuNI=
|
||||||
github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
|
github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
|
||||||
@@ -110,8 +112,6 @@ github.com/opencontainers/runtime-spec v1.1.0-rc.1/go.mod h1:jwyrGlmzljRJv/Fgzds
|
|||||||
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
|
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
|
||||||
github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU=
|
github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU=
|
||||||
github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
|
github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
|
||||||
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
|
||||||
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
|||||||
Reference in New Issue
Block a user