[alerter,manager] 告警信息入库,监控状态变更联动
This commit is contained in:
@@ -68,18 +68,24 @@ public class CalculateAlarm {
|
||||
// 先判断采集响应数据状态 UN_REACHABLE/UN_CONNECTABLE 则需发最高级别告警
|
||||
if (metricsData.getCode() != CollectRep.Code.SUCCESS) {
|
||||
// 采集异常
|
||||
if (metricsData.getCode() == CollectRep.Code.UN_REACHABLE
|
||||
|| metricsData.getCode() == CollectRep.Code.UN_CONNECTABLE) {
|
||||
// 连接型可用性异常 UN_REACHABLE 对端不可达(网络层icmp) UN_CONNECTABLE 对端连接失败(传输层tcp,udp)
|
||||
Alert alert = Alert.builder()
|
||||
.monitorId(monitorId)
|
||||
.priority((byte) 0)
|
||||
.status((byte) 0)
|
||||
.target(CommonConstants.AVAILABLE)
|
||||
.duration(300)
|
||||
.content("监控紧急可用性告警: " + metricsData.getCode().name())
|
||||
.build();
|
||||
dataQueue.addAlertData(alert);
|
||||
Alert.AlertBuilder alertBuilder = Alert.builder()
|
||||
.monitorId(monitorId)
|
||||
.priority((byte) 0)
|
||||
.status((byte) 0)
|
||||
.duration(300);
|
||||
if (metricsData.getCode() == CollectRep.Code.UN_REACHABLE) {
|
||||
// UN_REACHABLE 对端不可达(网络层icmp)
|
||||
alertBuilder.target(CommonConstants.REACHABLE)
|
||||
.content("监控紧急可达性告警: " + metricsData.getCode().name());
|
||||
dataQueue.addAlertData(alertBuilder.build());
|
||||
} else if (metricsData.getCode() == CollectRep.Code.UN_CONNECTABLE) {
|
||||
// UN_CONNECTABLE 对端连接失败(传输层tcp,udp)
|
||||
alertBuilder.target(CommonConstants.AVAILABLE)
|
||||
.content("监控紧急可用性告警: " + metricsData.getCode().name());
|
||||
dataQueue.addAlertData(alertBuilder.build());
|
||||
} else {
|
||||
// todo 其它规范异常 TIMEOUT ...
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -127,7 +133,7 @@ public class CalculateAlarm {
|
||||
Expression expression = AviatorEvaluator.compile(expr, true);
|
||||
Boolean match = (Boolean) expression.execute(fieldValueMap);
|
||||
if (match) {
|
||||
// 阈值规则匹配,触发告警 todo 告警延迟delay参数实现
|
||||
// 阈值规则匹配,触发告警
|
||||
Alert alert = Alert.builder()
|
||||
.monitorId(monitorId)
|
||||
.priority(define.getPriority())
|
||||
|
||||
14
alerter/src/main/java/com/usthe/alert/dao/AlertDao.java
Normal file
14
alerter/src/main/java/com/usthe/alert/dao/AlertDao.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.usthe.alert.dao;
|
||||
|
||||
import com.usthe.alert.pojo.entity.Alert;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
/**
|
||||
* Alert 数据库操作
|
||||
* @author tom
|
||||
* @date 2021/12/9 10:03
|
||||
*/
|
||||
public interface AlertDao extends JpaRepository<Alert, Long>, JpaSpecificationExecutor<Alert> {
|
||||
|
||||
}
|
||||
@@ -29,7 +29,7 @@ public interface AlertDefineDao extends JpaRepository<AlertDefine, Long>, JpaSpe
|
||||
* @return 告警定义列表
|
||||
*/
|
||||
@Query("select define from AlertDefine define join AlertDefineBind bind on bind.alertDefineId = define.id " +
|
||||
"where bind.monitorId = :monitorId and define.metric = :metrics")
|
||||
"where bind.monitorId = :monitorId and define.metric = :metrics and define.enable = true")
|
||||
List<AlertDefine> queryAlertDefinesByMonitor(@Param(value = "monitorId") Long monitorId,
|
||||
@Param(value = "metrics") String metrics);
|
||||
}
|
||||
|
||||
@@ -61,8 +61,7 @@ public class Alert {
|
||||
|
||||
@ApiModelProperty(value = "告警目标对象: 监控可用性-available 指标-app.metrics.field",
|
||||
example = "1", accessMode = READ_WRITE, position = 4)
|
||||
@Min(0)
|
||||
@Max(2)
|
||||
@Length(max = 255)
|
||||
private String target;
|
||||
|
||||
@ApiModelProperty(value = "触发告警后持续时间,单位s", example = "60", accessMode = READ_WRITE, position = 7)
|
||||
|
||||
@@ -70,13 +70,9 @@ public class AlertDefine {
|
||||
@Min(0)
|
||||
private int duration;
|
||||
|
||||
@ApiModelProperty(value = "告警触发后是否发送", example = "true", accessMode = READ_WRITE, position = 8)
|
||||
@ApiModelProperty(value = "告警阈值开关", example = "true", accessMode = READ_WRITE, position = 8)
|
||||
private boolean enable = true;
|
||||
|
||||
@ApiModelProperty(value = "告警延迟时间,即延迟多久再发送告警,单位s", example = "300", accessMode = READ_WRITE, position = 9)
|
||||
@Min(0)
|
||||
private int delay;
|
||||
|
||||
@ApiModelProperty(value = "告警通知内容", example = "linux {monitor_name}: {monitor_id} cpu usage high",
|
||||
accessMode = READ_WRITE, position = 10)
|
||||
@Length(max = 1024)
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.usthe.alert.service;
|
||||
|
||||
import com.usthe.alert.pojo.entity.Alert;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
/**
|
||||
* 告警信息管理接口
|
||||
* @author tom
|
||||
* @date 2021/12/9 10:06
|
||||
*/
|
||||
public interface AlertService {
|
||||
|
||||
/**
|
||||
* 新增告警
|
||||
* @param alert 告警实体
|
||||
* @throws RuntimeException 新增过程异常抛出
|
||||
*/
|
||||
void addAlert(Alert alert) throws RuntimeException;
|
||||
|
||||
/**
|
||||
* 动态条件查询
|
||||
* @param specification 查询条件
|
||||
* @param pageRequest 分页参数
|
||||
* @return 查询结果
|
||||
*/
|
||||
Page<Alert> getAlerts(Specification<Alert> specification, PageRequest pageRequest);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.usthe.alert.service.impl;
|
||||
|
||||
import com.usthe.alert.dao.AlertDao;
|
||||
import com.usthe.alert.pojo.entity.Alert;
|
||||
import com.usthe.alert.service.AlertService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 告警信息服务实现
|
||||
* @author tom
|
||||
* @date 2021/12/10 15:39
|
||||
*/
|
||||
@Service
|
||||
public class AlertServiceImpl implements AlertService {
|
||||
|
||||
@Autowired
|
||||
private AlertDao alertDao;
|
||||
|
||||
@Override
|
||||
public void addAlert(Alert alert) throws RuntimeException {
|
||||
alertDao.save(alert);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Alert> getAlerts(Specification<Alert> specification, PageRequest pageRequest) {
|
||||
return alertDao.findAll(specification, pageRequest);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user