-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[OSCP]KusciaTask 系统资源指标采集、暴露及统一导出 #454
base: main
Are you sure you want to change the base?
Conversation
// GetContainerStats fetches the container stats using crictl stats command | ||
func GetContainerStats() (map[string]ContainerStats, error) { | ||
// Execute the crictl stats command | ||
cmd := exec.Command("crictl", "stats") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里也建议直接调用 Containerd 的 pkg 去实现,不要使用 crictl stats 实现,这个可以搜一下大模型,有很多成熟的实现
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
类似这样:
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/typeurl"
v1 "github.com/containerd/cgroups/stats/v1" // Import cgroups stats
)
func main() {
// Create a new client connected to the default containerd socket
client, err := containerd.New("/run/containerd/containerd.sock")
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Define a context with the appropriate namespace
ctx := namespaces.WithNamespace(context.Background(), "default")
// Specify the container ID you want to monitor
containerID := "your-container-id"
// Fetch the container by its ID
container, err := client.LoadContainer(ctx, containerID)
if err != nil {
log.Fatalf("failed to load container: %v", err)
}
// Get the task associated with the container
task, err := container.Task(ctx, cio.Load)
if err != nil {
log.Fatalf("failed to load task for container: %v", err)
}
// Get the metrics for the task
metrics, err := task.Metrics(ctx)
if err != nil {
log.Fatalf("failed to get metrics: %v", err)
}
// Parse the metrics into a cgroups metrics structure
data, err := typeurl.UnmarshalAny(metrics.Data)
if err != nil {
log.Fatalf("failed to unmarshal metrics data: %v", err)
}
// Assert the data to the expected type
stats := data.(*v1.Metrics)
// Print the CPU and memory usage
fmt.Printf("CPU Usage: %v\n", stats.CPU.Usage.Total)
fmt.Printf("Memory Usage: %v\n", stats.Memory.Usage.Usage)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那task的相关任务指标是否可以通过crictl stats获得呢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这种方法只能获得CPU和内存使用,剩下的Iodes和disk该如何解决呢
} | ||
|
||
func GetKusciaTaskPID() (map[string]string, error) { | ||
const containerdDir = "/home/kuscia/containerd/run/io.containerd.runtime.v2.task/k8s.io/" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
containerd 有metrics plugin,不建议裸写。
https://github.com/containerd/containerd/blob/main/docs/ops.md
Fixedhttps://github.com//issues/400