增加网速告警

This commit is contained in:
dushixiang committed 2025-11-27 21:35:27 +08:00
1 parent 8cfdbbc25d
commit 73c9814af8
5 files changed
+30 -12

No files matched your search

+8 -3
View File
@@ -358,8 +358,8 @@ func startMetricsMonitoring(ctx context.Context, components *AppComponents, logg
continue
}
// 提取 CPU、内存、磁盘使用率
var cpuUsage, memoryUsage, diskUsage float64
// 提取 CPU、内存、磁盘使用率、网速
var cpuUsage, memoryUsage, diskUsage, networkSpeed float64
if latest.CPU != nil {
cpuUsage = latest.CPU.UsagePercent
@@ -373,8 +373,13 @@ func startMetricsMonitoring(ctx context.Context, components *AppComponents, logg
diskUsage = latest.Disk.AvgUsagePercent
}
if latest.Network != nil {
// 网速 = (发送速率 + 接收速率) / 1024 / 1024 (转换为 MB/s)
networkSpeed = float64(latest.Network.TotalBytesSentRate+latest.Network.TotalBytesRecvRate) / 1024 / 1024
}
// 检查告警规则
if err := components.AlertService.CheckMetrics(ctx, agent.ID, cpuUsage, memoryUsage, diskUsage); err != nil {
if err := components.AlertService.CheckMetrics(ctx, agent.ID, cpuUsage, memoryUsage, diskUsage, networkSpeed); err != nil {
logger.Error("检查告警规则失败", zap.String("agentId", agent.ID), zap.Error(err))
}
}
+4 -3
View File
@@ -30,9 +30,10 @@ type AlertRules struct {
DiskThreshold float64 `json:"diskThreshold"` // 磁盘使用率阈值(0-100)
DiskDuration int `json:"diskDuration"` // 持续时间(秒)
// 网络断开告警配置
NetworkEnabled bool `json:"networkEnabled"` // 是否启用网络断开告警
NetworkDuration int `json:"networkDuration"` // 持续时间(秒)
// 网络告警配置
NetworkEnabled bool `json:"networkEnabled"` // 是否启用网络告警
NetworkThreshold float64 `json:"networkThreshold"` // 网速阈值(MB/s)
NetworkDuration int `json:"networkDuration"` // 持续时间(秒)
// HTTPS 证书告警配置
CertEnabled bool `json:"certEnabled"` // 是否启用证书告警
+11 -2
View File
@@ -84,7 +84,7 @@ func (s *AlertService) ListAlertRecords(ctx context.Context, agentID string, lim
}
// CheckMetrics 检查指标并触发告警
func (s *AlertService) CheckMetrics(ctx context.Context, agentID string, cpu, memory, disk float64) error {
func (s *AlertService) CheckMetrics(ctx context.Context, agentID string, cpu, memory, disk, networkSpeed float64) error {
// 获取全局告警配置
globalConfigs, err := s.alertRepo.FindEnabledByAgentID(ctx, "global")
if err != nil {
@@ -117,6 +117,11 @@ func (s *AlertService) CheckMetrics(ctx context.Context, agentID string, cpu, me
if config.Rules.DiskEnabled {
s.checkAlert(ctx, &config, &agent, "disk", disk, config.Rules.DiskThreshold, config.Rules.DiskDuration, now)
}
// 检查网速告警
if config.Rules.NetworkEnabled {
s.checkAlert(ctx, &config, &agent, "network", networkSpeed, config.Rules.NetworkThreshold, config.Rules.NetworkDuration, now)
}
}
return nil
@@ -298,7 +303,11 @@ func (s *AlertService) buildAlertMessage(state *models.AlertState) string {
case "disk":
alertTypeName = "磁盘使用率"
case "network":
alertTypeName = "网络连接"
return fmt.Sprintf("网速持续%d秒超过%.2fMB/s,当前值%.2fMB/s",
state.Duration,
state.Threshold,
state.Value,
)
case "cert":
return fmt.Sprintf("HTTPS证书剩余天数%.0f天,低于阈值%.0f天", state.Value, state.Threshold)
case "service":
+6 -4
View File
@@ -41,6 +41,7 @@ const AlertSettings = () => {
diskThreshold: 85,
diskDuration: 60,
networkEnabled: false,
networkThreshold: 100,
networkDuration: 60,
certEnabled: true,
certThreshold: 30,
@@ -103,9 +104,10 @@ const AlertSettings = () => {
{/*<Divider orientation="left">告警规则</Divider>*/}
{[
{key: 'cpu', title: 'CPU 告警规则', thresholdLabel: 'CPU 使用率阈值 (%)'},
{key: 'memory', title: '内存告警规则', thresholdLabel: '内存使用率阈值 (%)'},
{key: 'disk', title: '磁盘告警规则', thresholdLabel: '磁盘使用率阈值 (%)'},
{key: 'cpu', title: 'CPU 告警规则', thresholdLabel: 'CPU 使用率阈值 (%)', max: 100},
{key: 'memory', title: '内存告警规则', thresholdLabel: '内存使用率阈值 (%)', max: 100},
{key: 'disk', title: '磁盘告警规则', thresholdLabel: '磁盘使用率阈值 (%)', max: 100},
{key: 'network', title: '网速告警规则', thresholdLabel: '网速阈值 (MB/s)', max: 10000},
].map((rule) => (
<Card key={rule.key} title={rule.title} type="inner">
<Form.Item noStyle shouldUpdate>
@@ -128,7 +130,7 @@ const AlertSettings = () => {
>
<InputNumber
min={0}
max={100}
max={rule.max}
style={{width: '100%'}}
disabled={!enabled}
/>
+1
View File
@@ -406,6 +406,7 @@ export interface AlertRules {
diskThreshold: number;
diskDuration: number;
networkEnabled: boolean;
networkThreshold: number; // 网速阈值(MB/s)
networkDuration: number;
certEnabled: boolean; // HTTPS 证书告警开关
certThreshold: number; // 证书剩余天数阈值(天)