Skip to content

Commit ddca72d

Browse files
committed
feat(podman): 添加容器监控和告警功能
- 新增 ContainerMonitor 结构体和相关方法,实现容器监控和告警 - 添加 Podman 客户端连接和配置支持 - 实现容器指标收集和处理逻辑 - 增加 CPU 和内存使用率的告警功能 - 优化容器目标筛选和配置管理
1 parent 109c597 commit ddca72d

File tree

1 file changed

+79
-9
lines changed

1 file changed

+79
-9
lines changed

internal/monitor/podman/podman.go

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ package podman
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"sync"
78
"time"
9+
10+
podmanClient "github.com/containers/podman/v4/pkg/api/client" // 注意这里的 v4
11+
// 注意这里的 v4
12+
// 保持不变
13+
// 保持不变
814
)
915

1016
type ContainerState struct {
@@ -21,13 +27,15 @@ type ContainerMonitorConfig struct {
2127
Interval time.Duration
2228
TargetContainers []string
2329
AlertThresholds map[string]float64
30+
SocketPath string
2431
}
2532

2633
type ContainerMoitor struct {
27-
config ContainerMonitorConfig
28-
mu sync.RWMutex
29-
alertCh chan Alert
30-
stopCh chan struct{}
34+
config ContainerMonitorConfig
35+
mu sync.RWMutex
36+
alertCh chan Alert
37+
stopCh chan struct{}
38+
podmanClient *podmanClient.Client
3139
}
3240

3341
type Alert struct {
@@ -40,11 +48,11 @@ type Alert struct {
4048
}
4149

4250
func NewContainerMonitor(config ContainerMonitorConfig) (*ContainerMoitor, error) {
43-
return &ContainerMoitor{
44-
config: config,
45-
alertCh: make(chan Alert, 10),
46-
stopCh: make(chan struct{}),
47-
}, nil
51+
if config.SocketPath == "" {
52+
config.SocketPath = "unix:/run/podman/podman.sock"
53+
log.Printf("Podman SocketPath 未指定,尝试使用默认路径: %s。请在 config 中明确指定以避免问题。", config.SocketPath)
54+
}
55+
4856
}
4957

5058
func (m *ContainerMoitor) Start(ctx context.Context) {
@@ -83,4 +91,66 @@ func (m *ContainerMoitor) collectMetrics(ctx context.Context) ([]ContainerState,
8391
{"container1", "my-app-db"},
8492
{"container2", "my-app-web"},
8593
}
94+
95+
for _, sc := range simulatedContainers {
96+
if len(m.config.AlertThresholds) > 0 {
97+
found := false
98+
for _, target := range m.config.TargetContainers {
99+
if target == sc.ID || target == sc.Name {
100+
found = true
101+
break
102+
}
103+
}
104+
if !found {
105+
continue
106+
}
107+
108+
}
109+
cpu := float64(time.Now().UnixNano() % 100)
110+
mem := float64(time.Now().UnixNano()%100 + 200)
111+
112+
allStats = append(allStats, ContainerState{
113+
CPUUsage: cpu,
114+
MemoryUsage: mem,
115+
Name: sc.ID,
116+
ContainerID: sc.Name,
117+
NetworkRxBytes: uint64(time.Now().UnixNano() % 1000),
118+
NetworkTxBytes: uint64(time.Now().UnixNano() % 1000),
119+
Timestamp: time.Now(),
120+
})
121+
}
122+
return allStats, nil
123+
124+
}
125+
126+
func (m *ContainerMoitor) processMetrics(stats []ContainerState) {
127+
for _, s := range stats {
128+
log.Printf("容器 [%s]: CPU使用率=%f%%, 内存使用率=%f", s.Name, s.CPUUsage, s.MemoryUsage)
129+
130+
if threshold, ok := m.config.AlertThresholds["cpu-high"]; ok && s.CPUUsage > threshold {
131+
m.alertCh <- Alert{
132+
ContainerID: s.ContainerID,
133+
Message: fmt.Sprintf("CPU使用率过高: %f%%", s.CPUUsage),
134+
Metric: "CPUUsage",
135+
Threshold: threshold,
136+
Timestamp: s.Timestamp,
137+
Value: s.CPUUsage,
138+
}
139+
}
140+
if threshold, ok := m.config.AlertThresholds["mem-high"]; ok && s.MemoryUsage > threshold {
141+
m.alertCh <- Alert{
142+
ContainerID: s.ContainerID,
143+
Message: fmt.Sprintf("内存使用率过高: %f%%", s.MemoryUsage),
144+
Metric: "MemoryUsageMB",
145+
Threshold: threshold,
146+
Timestamp: s.Timestamp,
147+
Value: s.MemoryUsage,
148+
}
149+
}
150+
}
151+
152+
}
153+
154+
func (m *ContainerMoitor) GetAlerts() <-chan Alert {
155+
return m.alertCh
86156
}

0 commit comments

Comments
 (0)