🔨 优化告警引擎.
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
*/
|
||||
package org.dromara.visor.module.monitor.convert;
|
||||
|
||||
import org.dromara.visor.common.mapstruct.JsonConversion;
|
||||
import org.dromara.visor.module.monitor.entity.domain.AlarmEventDO;
|
||||
import org.dromara.visor.module.monitor.entity.dto.AlarmEventTriggerDTO;
|
||||
import org.dromara.visor.module.monitor.entity.dto.AlarmPolicyAlarmCountDTO;
|
||||
@@ -40,7 +41,7 @@ import java.util.List;
|
||||
* @version 1.0.0
|
||||
* @since 2025-9-17 21:31
|
||||
*/
|
||||
@Mapper
|
||||
@Mapper(uses = JsonConversion.class)
|
||||
public interface AlarmEventConvert {
|
||||
|
||||
AlarmEventConvert MAPPER = Mappers.getMapper(AlarmEventConvert.class);
|
||||
|
||||
@@ -57,17 +57,17 @@ public class AlarmEventDO extends BaseDO {
|
||||
@TableField("agent_key")
|
||||
private String agentKey;
|
||||
|
||||
@Schema(description = "主机id")
|
||||
@TableField("host_id")
|
||||
private Long hostId;
|
||||
@Schema(description = "事件来源")
|
||||
@TableField("source_type")
|
||||
private String sourceType;
|
||||
|
||||
@Schema(description = "主机名称")
|
||||
@TableField("host_name")
|
||||
private String hostName;
|
||||
@Schema(description = "事件来源id")
|
||||
@TableField("source_id")
|
||||
private Long sourceId;
|
||||
|
||||
@Schema(description = "主机地址")
|
||||
@TableField("host_address")
|
||||
private String hostAddress;
|
||||
@Schema(description = "事件来源id")
|
||||
@TableField("source_info")
|
||||
private String sourceInfo;
|
||||
|
||||
@Schema(description = "策略id")
|
||||
@TableField("policy_id")
|
||||
|
||||
@@ -56,14 +56,14 @@ public class AlarmEventTriggerDTO extends BaseDO {
|
||||
@Schema(description = "agentKey")
|
||||
private String agentKey;
|
||||
|
||||
@Schema(description = "主机id")
|
||||
private Long hostId;
|
||||
@Schema(description = "事件来源")
|
||||
private String sourceType;
|
||||
|
||||
@Schema(description = "主机名称")
|
||||
private String hostName;
|
||||
@Schema(description = "事件来源id")
|
||||
private Long sourceId;
|
||||
|
||||
@Schema(description = "主机地址")
|
||||
private String hostAddress;
|
||||
@Schema(description = "事件来源id")
|
||||
private String sourceInfo;
|
||||
|
||||
@Schema(description = "策略id")
|
||||
private Long policyId;
|
||||
|
||||
@@ -48,13 +48,16 @@ public class AlarmEventQueryRequest extends BaseQueryRequest {
|
||||
@Schema(description = "id")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "主机名称")
|
||||
private Long hostId;
|
||||
|
||||
@Size(max = 32)
|
||||
@Schema(description = "agentKey")
|
||||
private String agentKey;
|
||||
|
||||
@Schema(description = "事件来源")
|
||||
private String sourceType;
|
||||
|
||||
@Schema(description = "事件来源id")
|
||||
private Long sourceId;
|
||||
|
||||
@Schema(description = "策略id")
|
||||
private Long policyId;
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
*/
|
||||
package org.dromara.visor.module.monitor.entity.vo;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
@@ -51,14 +52,14 @@ public class AlarmEventVO implements Serializable {
|
||||
@Schema(description = "id")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "主机名称")
|
||||
private Long hostId;
|
||||
@Schema(description = "事件来源")
|
||||
private String sourceType;
|
||||
|
||||
@Schema(description = "主机名称")
|
||||
private String hostName;
|
||||
@Schema(description = "事件来源id")
|
||||
private Long sourceId;
|
||||
|
||||
@Schema(description = "主机地址")
|
||||
private String hostAddress;
|
||||
@Schema(description = "事件来源id")
|
||||
private JSONObject sourceInfo;
|
||||
|
||||
@Schema(description = "agentKey")
|
||||
private String agentKey;
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.monitor.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 告警事件来源
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025/10/13 22:03
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum AlarmEventSourceTypeEnum {
|
||||
|
||||
/**
|
||||
* 主机告警
|
||||
*/
|
||||
HOST,
|
||||
|
||||
/**
|
||||
* 拨测告警
|
||||
*/
|
||||
UPTIME,
|
||||
|
||||
;
|
||||
|
||||
public static AlarmEventSourceTypeEnum of(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
for (AlarmEventSourceTypeEnum item : values()) {
|
||||
if (item.name().equals(value)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -491,38 +491,41 @@ public abstract class BaseAlarmEngine implements IAlarmEngine {
|
||||
// 构建参数
|
||||
List<Map<String, Object>> paramsList = new ArrayList<>();
|
||||
for (AlarmEventTriggerDTO event : alarmEvents) {
|
||||
MonitorMetricsContextDTO metrics = monitorMetricsContext.getMonitorMetrics(event.getMetricsId());
|
||||
MetricsUnitEnum unit = MetricsUnitEnum.of(metrics.getUnit());
|
||||
AlarmLevelEnum level = AlarmLevelEnum.of(event.getAlarmLevel());
|
||||
AlarmTriggerConditionEnum triggerCondition = AlarmTriggerConditionEnum.of(event.getTriggerCondition());
|
||||
try {
|
||||
MonitorMetricsContextDTO metrics = monitorMetricsContext.getMonitorMetrics(event.getMetricsId());
|
||||
MetricsUnitEnum unit = MetricsUnitEnum.of(metrics.getUnit());
|
||||
AlarmLevelEnum level = AlarmLevelEnum.of(event.getAlarmLevel());
|
||||
AlarmTriggerConditionEnum triggerCondition = AlarmTriggerConditionEnum.of(event.getTriggerCondition());
|
||||
|
||||
// 告警事件参数
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("id", event.getId());
|
||||
params.put("relKey", event.getId());
|
||||
params.put("policyId", policy.getId());
|
||||
params.put("policyName", policy.getName());
|
||||
params.put("ruleId", event.getPolicyRuleId());
|
||||
params.put("hostId", event.getHostId());
|
||||
params.put("hostName", event.getHostName());
|
||||
params.put("hostAddress", event.getHostAddress());
|
||||
params.put("metrics", metrics.getMeasurement() + "." + metrics.getValue());
|
||||
params.put("metricsId", metrics.getId());
|
||||
params.put("metricsName", metrics.getName());
|
||||
params.put("metricsField", metrics.getValue());
|
||||
params.put("metricsMeasurement", metrics.getMeasurement());
|
||||
params.put("tags", event.getAlarmTags());
|
||||
params.put("level", level.name());
|
||||
params.put("levelLabel", level.getLabel());
|
||||
params.put("levelSeverity", level.getSeverity());
|
||||
params.put("levelColor", level.getColor());
|
||||
params.put("consecutiveCount", event.getConsecutiveCount());
|
||||
params.put("triggerCondition", triggerCondition.getCondition());
|
||||
params.put("alarmInfo", event.getAlarmInfo());
|
||||
params.put("alarmValue", unit.format(event.getAlarmValue(), new MetricsUnitEnum.FormatOptions(2, metrics.getSuffix())));
|
||||
params.put("alarmThreshold", unit.format(event.getAlarmThreshold(), new MetricsUnitEnum.FormatOptions(4, metrics.getSuffix())));
|
||||
params.put("alarmTime", Dates.format(event.getCreateTime()));
|
||||
paramsList.add(params);
|
||||
// 告警事件参数
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("id", event.getId());
|
||||
params.put("relKey", event.getId());
|
||||
params.put("policyId", policy.getId());
|
||||
params.put("policyName", policy.getName());
|
||||
params.put("ruleId", event.getPolicyRuleId());
|
||||
params.put("metrics", metrics.getMeasurement() + "." + metrics.getValue());
|
||||
params.put("metricsId", metrics.getId());
|
||||
params.put("metricsName", metrics.getName());
|
||||
params.put("metricsField", metrics.getValue());
|
||||
params.put("metricsMeasurement", metrics.getMeasurement());
|
||||
params.put("tags", event.getAlarmTags());
|
||||
params.put("level", level.name());
|
||||
params.put("levelLabel", level.getLabel());
|
||||
params.put("levelSeverity", level.getSeverity());
|
||||
params.put("levelColor", level.getColor());
|
||||
params.put("consecutiveCount", event.getConsecutiveCount());
|
||||
params.put("triggerCondition", triggerCondition.getCondition());
|
||||
params.put("alarmInfo", event.getAlarmInfo());
|
||||
params.put("alarmValue", unit.format(event.getAlarmValue(), new MetricsUnitEnum.FormatOptions(2, metrics.getSuffix())));
|
||||
params.put("alarmThreshold", unit.format(event.getAlarmThreshold(), new MetricsUnitEnum.FormatOptions(4, metrics.getSuffix())));
|
||||
params.put("alarmTime", Dates.format(event.getCreateTime()));
|
||||
// 设置额外告警推送参数
|
||||
this.setExtraAlarmPushParams(params, event);
|
||||
paramsList.add(params);
|
||||
} catch (Exception e) {
|
||||
log.info("AlarmEngine-setAlarmParams error", e);
|
||||
}
|
||||
}
|
||||
// 推送消息
|
||||
for (Map<String, Object> params : paramsList) {
|
||||
@@ -532,4 +535,12 @@ public abstract class BaseAlarmEngine implements IAlarmEngine {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置告警推送参数
|
||||
*
|
||||
* @param params params
|
||||
* @param event event
|
||||
*/
|
||||
protected abstract void setExtraAlarmPushParams(Map<String, Object> params, AlarmEventTriggerDTO event);
|
||||
|
||||
}
|
||||
|
||||
@@ -31,10 +31,12 @@ import org.dromara.visor.module.asset.entity.dto.host.HostBaseDTO;
|
||||
import org.dromara.visor.module.monitor.convert.AlarmEventConvert;
|
||||
import org.dromara.visor.module.monitor.entity.domain.AlarmEventDO;
|
||||
import org.dromara.visor.module.monitor.entity.dto.*;
|
||||
import org.dromara.visor.module.monitor.enums.AlarmEventSourceTypeEnum;
|
||||
import org.dromara.visor.module.monitor.enums.AlarmHandleStatusEnum;
|
||||
import org.dromara.visor.module.monitor.enums.AlarmSwitchEnum;
|
||||
import org.dromara.visor.module.monitor.enums.MetricsUnitEnum;
|
||||
import org.dromara.visor.module.monitor.handler.alarm.model.AlarmEngineRule;
|
||||
import org.dromara.visor.module.monitor.handler.alarm.model.HostAlarmSourceInfo;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -98,9 +100,14 @@ public class MetricsAlarmEngine extends BaseAlarmEngine {
|
||||
Map<String, String> tags = agentMetrics.getTags();
|
||||
AlarmEventDO alarmEvent = AlarmEventDO.builder()
|
||||
.agentKey(agentKey)
|
||||
.hostId(host.getId())
|
||||
.hostName(host.getName())
|
||||
.hostAddress(host.getAddress())
|
||||
.sourceType(AlarmEventSourceTypeEnum.HOST.name())
|
||||
.sourceId(host.getId())
|
||||
.sourceInfo(HostAlarmSourceInfo.builder()
|
||||
.name(host.getName())
|
||||
.code(host.getCode())
|
||||
.address(host.getAddress())
|
||||
.build()
|
||||
.toJsonString())
|
||||
.policyId(rule.getPolicyId())
|
||||
.policyRuleId(rule.getId())
|
||||
.metricsId(rule.getMetricsId())
|
||||
@@ -126,4 +133,13 @@ public class MetricsAlarmEngine extends BaseAlarmEngine {
|
||||
return AlarmEventConvert.MAPPER.toTrigger(alarmEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setExtraAlarmPushParams(Map<String, Object> params, AlarmEventTriggerDTO event) {
|
||||
HostAlarmSourceInfo sourceInfo = JSON.parseObject(event.getAlarmInfo(), HostAlarmSourceInfo.class);
|
||||
params.put("hostId", event.getSourceId());
|
||||
params.put("hostName", sourceInfo.getName());
|
||||
params.put("hostCode", sourceInfo.getCode());
|
||||
params.put("hostAddress", sourceInfo.getAddress());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.module.monitor.handler.alarm.model;
|
||||
|
||||
import cn.orionsec.kit.lang.able.IJsonObject;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 主机告警源信息
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025/10/13 22:09
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(name = "HostAlarmSourceInfo", description = "主机告警源信息")
|
||||
public class HostAlarmSourceInfo implements IJsonObject {
|
||||
|
||||
@Schema(description = "主机名称")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "主机编码")
|
||||
private String code;
|
||||
|
||||
@Schema(description = "主机地址")
|
||||
private String address;
|
||||
|
||||
}
|
||||
@@ -223,7 +223,8 @@ public class AlarmEventServiceImpl implements AlarmEventService {
|
||||
public LambdaQueryWrapper<AlarmEventDO> buildQueryWrapper(AlarmEventQueryRequest request) {
|
||||
return alarmEventDAO.wrapper()
|
||||
.eq(AlarmEventDO::getId, request.getId())
|
||||
.eq(AlarmEventDO::getHostId, request.getHostId())
|
||||
.eq(AlarmEventDO::getSourceType, request.getSourceType())
|
||||
.eq(AlarmEventDO::getSourceId, request.getSourceId())
|
||||
.eq(AlarmEventDO::getAgentKey, request.getAgentKey())
|
||||
.eq(AlarmEventDO::getPolicyId, request.getPolicyId())
|
||||
.eq(AlarmEventDO::getMetricsId, request.getMetricsId())
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
<!-- 通用查询映射结果 -->
|
||||
<resultMap id="BaseResultMap" type="org.dromara.visor.module.monitor.entity.domain.AlarmEventDO">
|
||||
<id column="id" property="id"/>
|
||||
<result column="host_id" property="hostId"/>
|
||||
<result column="host_name" property="hostName"/>
|
||||
<result column="host_address" property="hostAddress"/>
|
||||
<result column="source_type" property="sourceType"/>
|
||||
<result column="source_id" property="sourceId"/>
|
||||
<result column="source_info" property="sourceInfo"/>
|
||||
<result column="agent_key" property="agentKey"/>
|
||||
<result column="policy_id" property="policyId"/>
|
||||
<result column="policy_rule_id" property="policyRuleId"/>
|
||||
@@ -41,7 +41,7 @@
|
||||
|
||||
<!-- 通用查询结果列 -->
|
||||
<sql id="Base_Column_List">
|
||||
id, host_id, host_name, host_address, agent_key, policy_id, policy_rule_id, metrics_id, metrics_measurement, alarm_tags, alarm_value, alarm_threshold, alarm_info, alarm_level, trigger_condition, consecutive_count, false_alarm, handle_status, handle_time, handle_remark, handle_user_id, handle_username, create_time, update_time, creator, updater, deleted
|
||||
id, event_source, source_id, source_info, agent_key, policy_id, policy_rule_id, metrics_id, metrics_measurement, alarm_tags, alarm_value, alarm_threshold, alarm_info, alarm_level, trigger_condition, consecutive_count, false_alarm, handle_status, handle_time, handle_remark, handle_user_id, handle_username, create_time, update_time, creator, updater, deleted
|
||||
</sql>
|
||||
|
||||
<select id="selectPolicyEventCount" resultMap="CountResultMap">
|
||||
|
||||
Reference in New Issue
Block a user