优化磁盘数据采集和展示
This commit is contained in:
1 parent
d534c6f523
commit
fdafca0337
7 files changed
+47
-51
No files matched your search
@@ -34,7 +34,7 @@ collector:
|
||||
# 网络采集包含的网卡列表(白名单,支持正则表达式)
|
||||
# 如果配置了此项,则只采集匹配的网卡,忽略 network_exclude
|
||||
# 例如: ["^eth0$", "^en0$", "^ens.*"]
|
||||
network_include: []
|
||||
network_include: [ ]
|
||||
|
||||
# 网络采集排除的网卡列表(可选,支持正则表达式)
|
||||
# 如果不配置,默认只排除回环地址(lo, lo0)
|
||||
@@ -46,6 +46,14 @@ collector:
|
||||
- "^veth.*" # 排除所有 veth 开头的虚拟接口
|
||||
- "^br-.*" # 排除所有 br- 开头的网桥接口
|
||||
|
||||
# 磁盘采集包含的挂载点列表(白名单)
|
||||
# 如果为空,默认采集系统主分区(Linux/macOS: "/",Windows: "C:\")
|
||||
# 例如:
|
||||
# Linux/macOS: ["/", "/data", "/home"]
|
||||
# Windows: ["C:\\", "D:\\"]
|
||||
disk_include:
|
||||
- "/" # 只采集根分区
|
||||
|
||||
# 自动更新配置
|
||||
auto_update:
|
||||
# 是否启用自动更新
|
||||
|
||||
+1
-1
@@ -412,7 +412,7 @@ func startMetricsMonitoring(ctx context.Context, components *AppComponents, logg
|
||||
}
|
||||
|
||||
if latest.Disk != nil {
|
||||
diskUsage = latest.Disk.AvgUsagePercent
|
||||
diskUsage = latest.Disk.UsagePercent
|
||||
}
|
||||
|
||||
if latest.Network != nil {
|
||||
|
||||
@@ -118,7 +118,6 @@ func (s *MetricService) HandleMetricData(ctx context.Context, agentID string, me
|
||||
|
||||
// 合并所有磁盘的数据用于保存总和
|
||||
var totalTotal, totalUsed, totalFree uint64
|
||||
var maxUsagePercent float64
|
||||
|
||||
// 保存每个磁盘的数据,同时累加总和
|
||||
for _, diskData := range diskDataList {
|
||||
@@ -143,27 +142,28 @@ func (s *MetricService) HandleMetricData(ctx context.Context, agentID string, me
|
||||
totalTotal += diskData.Total
|
||||
totalUsed += diskData.Used
|
||||
totalFree += diskData.Free
|
||||
if diskData.UsagePercent > maxUsagePercent {
|
||||
maxUsagePercent = diskData.UsagePercent
|
||||
}
|
||||
}
|
||||
|
||||
// 保存合并后的总和数据(mount_point 字段设置为空字符串)
|
||||
var usagePercent float64
|
||||
if totalTotal > 0 {
|
||||
usagePercent = float64(totalUsed) / float64(totalTotal) * 100
|
||||
}
|
||||
totalMetric := &models.DiskMetric{
|
||||
AgentID: agentID,
|
||||
MountPoint: "all",
|
||||
Total: totalTotal,
|
||||
Used: totalUsed,
|
||||
Free: totalFree,
|
||||
UsagePercent: maxUsagePercent, // 使用率取最大值
|
||||
UsagePercent: usagePercent,
|
||||
Timestamp: now,
|
||||
}
|
||||
latestMetrics.Disk = &DiskSummary{
|
||||
AvgUsagePercent: totalMetric.UsagePercent,
|
||||
TotalDisks: len(diskDataList),
|
||||
Total: totalMetric.Total,
|
||||
Used: totalMetric.Used,
|
||||
Free: totalMetric.Free,
|
||||
UsagePercent: totalMetric.UsagePercent,
|
||||
TotalDisks: len(diskDataList),
|
||||
Total: totalMetric.Total,
|
||||
Used: totalMetric.Used,
|
||||
Free: totalMetric.Free,
|
||||
}
|
||||
return s.metricRepo.SaveDiskMetric(ctx, totalMetric)
|
||||
|
||||
@@ -786,11 +786,11 @@ func (s *MetricService) GetAvailableNetworkInterfaces(ctx context.Context, agent
|
||||
|
||||
// DiskSummary 磁盘汇总数据
|
||||
type DiskSummary struct {
|
||||
AvgUsagePercent float64 `json:"avgUsagePercent"` // 平均使用率
|
||||
TotalDisks int `json:"totalDisks"` // 磁盘数量
|
||||
Total uint64 `json:"total"` // 总容量(字节)
|
||||
Used uint64 `json:"used"` // 已使用(字节)
|
||||
Free uint64 `json:"free"` // 空闲(字节)
|
||||
UsagePercent float64 `json:"usagePercent"` // 平均使用率
|
||||
TotalDisks int `json:"totalDisks"` // 磁盘数量
|
||||
Total uint64 `json:"total"` // 总容量(字节)
|
||||
Used uint64 `json:"used"` // 已使用(字节)
|
||||
Free uint64 `json:"free"` // 空闲(字节)
|
||||
}
|
||||
|
||||
// NetworkSummary 网络汇总数据
|
||||
|
||||
+19
-31
@@ -66,9 +66,11 @@ type CollectorConfig struct {
|
||||
// 如果为空,使用默认排除规则(虚拟网卡、回环地址等)
|
||||
NetworkExclude []string `yaml:"network_exclude"`
|
||||
|
||||
// 磁盘采集包含的挂载点列表(白名单,支持正则表达式)
|
||||
// 如果为空,默认只采集根分区 "^/$"
|
||||
// 例如: ["^/$", "^/data$", "^/mnt/.*"]
|
||||
// 磁盘采集包含的挂载点列表(白名单)
|
||||
// 如果为空,默认采集系统主分区(Linux/macOS: "/",Windows: "C:\")
|
||||
// 例如:
|
||||
// Linux/macOS: ["/", "/data", "/home"]
|
||||
// Windows: ["C:\\", "D:\\"]
|
||||
DiskInclude []string `yaml:"disk_include"`
|
||||
}
|
||||
|
||||
@@ -334,7 +336,7 @@ func (c *Config) GetNetworkExcludePatterns() ([]*regexp.Regexp, error) {
|
||||
// 1. 如果配置了 NetworkInclude(白名单),则只保留匹配白名单的网卡
|
||||
// 2. 如果没有配置 NetworkInclude,则使用 NetworkExclude(黑名单)规则
|
||||
func (c *Config) ShouldExcludeNetworkInterface(interfaceName string) bool {
|
||||
// 优先检查白��单
|
||||
// 优先检查白名单
|
||||
includePatterns, err := c.GetNetworkIncludePatterns()
|
||||
if err != nil {
|
||||
// 白名单编译失败,记录错误但继续使用黑名单逻辑
|
||||
@@ -367,42 +369,28 @@ func (c *Config) ShouldExcludeNetworkInterface(interfaceName string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// GetDiskIncludePatterns 获取磁盘包含的正则表达式列表(白名单)
|
||||
// GetDiskInclude 获取磁盘包含的挂载点列表(白名单)
|
||||
// 如果配置为空,返回默认的根分区匹配规则
|
||||
func (c *Config) GetDiskIncludePatterns() ([]*regexp.Regexp, error) {
|
||||
patterns := c.Collector.DiskInclude
|
||||
|
||||
// 如果没有配置,使用默认规则(只采集根分区)
|
||||
if len(patterns) == 0 {
|
||||
patterns = []string{"^/$"}
|
||||
}
|
||||
|
||||
var regexps []*regexp.Regexp
|
||||
for _, pattern := range patterns {
|
||||
re, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("编译磁盘包含规则 '%s' 失败: %w", pattern, err)
|
||||
// Linux/macOS: ["/"]
|
||||
// Windows: ["C:\\"]
|
||||
func (c *Config) GetDiskInclude() []string {
|
||||
if len(c.Collector.DiskInclude) == 0 {
|
||||
if runtime.GOOS == "windows" {
|
||||
return []string{"C:\\"}
|
||||
}
|
||||
regexps = append(regexps, re)
|
||||
return []string{"/"}
|
||||
}
|
||||
|
||||
return regexps, nil
|
||||
return c.Collector.DiskInclude
|
||||
}
|
||||
|
||||
// ShouldIncludeDiskMountPoint 检查挂载点是否应该被采集
|
||||
// 只有匹配 DiskInclude 白名单的挂载点才会被采集
|
||||
// 只有在 DiskInclude 白名单中的挂载点才会被采集
|
||||
func (c *Config) ShouldIncludeDiskMountPoint(mountPoint string) bool {
|
||||
includePatterns, err := c.GetDiskIncludePatterns()
|
||||
if err != nil {
|
||||
// 如果正则编译失败,只采集根分区
|
||||
return mountPoint == "/"
|
||||
}
|
||||
|
||||
for _, pattern := range includePatterns {
|
||||
if pattern.MatchString(mountPoint) {
|
||||
includeMounts := c.GetDiskInclude()
|
||||
for _, mount := range includeMounts {
|
||||
if mountPoint == mount {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -754,7 +754,7 @@ const ServerDetail = () => {
|
||||
icon: HardDrive,
|
||||
title: '磁盘使用',
|
||||
usagePercent: latestMetrics.disk
|
||||
? `${formatPercentValue(latestMetrics.disk.avgUsagePercent)}%`
|
||||
? `${formatPercentValue(latestMetrics.disk.usagePercent)}%`
|
||||
: '—',
|
||||
accent: 'purple',
|
||||
metrics: [
|
||||
|
||||
@@ -106,7 +106,7 @@ const calculateDiskUsage = (metrics?: LatestMetrics) => {
|
||||
}
|
||||
|
||||
// 后端已经计算好平均使用率,直接返回
|
||||
return metrics.disk.avgUsagePercent;
|
||||
return metrics.disk.usagePercent;
|
||||
};
|
||||
|
||||
const getProgressColor = (percent: number) => {
|
||||
|
||||
@@ -125,7 +125,7 @@ export interface MemoryMetric {
|
||||
|
||||
// 磁盘汇总数据
|
||||
export interface DiskSummary {
|
||||
avgUsagePercent: number; // 平均使用率
|
||||
usagePercent: number; // 平均使用率
|
||||
totalDisks: number; // 磁盘数量
|
||||
total: number; // 总容量(字节)
|
||||
used: number; // 已使用(字节)
|
||||
|
||||
Reference in new issue
Block a user