use of com.usthe.common.entity.alerter.Alert in project hertzbeat by dromara.
the class CalculateAlarm method calculate.
private void calculate(CollectRep.MetricsData metricsData) {
long currentTimeMilli = System.currentTimeMillis();
long monitorId = metricsData.getId();
String app = metricsData.getApp();
String metrics = metricsData.getMetrics();
// 先判断调度优先级为0的指标组采集响应数据状态 UN_REACHABLE/UN_CONNECTABLE 则需发最高级别告警进行监控状态变更
if (metricsData.getPriority() == 0) {
if (metricsData.getCode() != CollectRep.Code.SUCCESS) {
// 采集异常
if (metricsData.getCode() == CollectRep.Code.UN_AVAILABLE) {
// todo 采集器不可用
} else if (metricsData.getCode() == CollectRep.Code.UN_REACHABLE) {
// UN_REACHABLE 对端不可达(网络层icmp)
handlerMonitorStatusAlert(String.valueOf(monitorId), app, metricsData.getCode());
} else if (metricsData.getCode() == CollectRep.Code.UN_CONNECTABLE) {
// UN_CONNECTABLE 对端连接失败(传输层tcp,udp)
handlerMonitorStatusAlert(String.valueOf(monitorId), app, metricsData.getCode());
} else {
// 其他异常
handlerMonitorStatusAlert(String.valueOf(monitorId), app, metricsData.getCode());
}
return;
} else {
// 判断关联监控之前是否有可用性或者不可达告警,发送恢复告警进行监控状态恢复
Alert preAlert = triggeredAlertMap.remove(String.valueOf(monitorId));
if (preAlert != null && preAlert.getStatus() == CommonConstants.ALERT_STATUS_CODE_PENDING) {
// 发送告警恢复
Map<String, String> tags = new HashMap<>(6);
tags.put(CommonConstants.TAG_MONITOR_ID, String.valueOf(monitorId));
tags.put(CommonConstants.TAG_MONITOR_APP, app);
String target = CommonConstants.AVAILABLE;
String content = this.bundle.getString("alerter.availability.resolved");
if (CommonConstants.REACHABLE.equals(preAlert.getTarget())) {
target = CommonConstants.REACHABLE;
content = this.bundle.getString("alerter.reachability.resolved");
}
Alert resumeAlert = Alert.builder().tags(tags).target(target).content(content).priority(CommonConstants.ALERT_PRIORITY_CODE_WARNING).status(CommonConstants.ALERT_STATUS_CODE_RESTORED).firstTriggerTime(currentTimeMilli).lastTriggerTime(currentTimeMilli).times(1).build();
dataQueue.addAlertData(resumeAlert);
}
}
}
// 查出此监控类型下的此指标集合下关联配置的告警定义信息
// field - define[]
Map<String, List<AlertDefine>> defineMap = alertDefineService.getMonitorBindAlertDefines(monitorId, app, metrics);
if (defineMap == null || defineMap.isEmpty()) {
return;
}
List<CollectRep.Field> fields = metricsData.getFieldsList();
Map<String, Object> fieldValueMap = new HashMap<>(16);
for (CollectRep.ValueRow valueRow : metricsData.getValuesList()) {
if (!valueRow.getColumnsList().isEmpty()) {
fieldValueMap.clear();
String instance = valueRow.getInstance();
if (!"".equals(instance)) {
fieldValueMap.put("instance", instance);
}
for (int index = 0; index < valueRow.getColumnsList().size(); index++) {
String valueStr = valueRow.getColumns(index);
CollectRep.Field field = fields.get(index);
if (field.getType() == CommonConstants.TYPE_NUMBER) {
Double doubleValue = CommonUtil.parseDoubleStr(valueStr);
if (doubleValue != null) {
fieldValueMap.put(field.getName(), doubleValue);
}
} else {
if (!"".equals(valueStr)) {
fieldValueMap.put(field.getName(), valueStr);
}
}
}
for (Map.Entry<String, List<AlertDefine>> entry : defineMap.entrySet()) {
List<AlertDefine> defines = entry.getValue();
for (AlertDefine define : defines) {
String expr = define.getExpr();
try {
Expression expression = AviatorEvaluator.compile(expr, true);
Boolean match = (Boolean) expression.execute(fieldValueMap);
if (match) {
// 阈值规则匹配,判断已触发阈值次数,触发告警
String monitorAlertKey = String.valueOf(monitorId) + define.getId();
Alert triggeredAlert = triggeredAlertMap.get(monitorAlertKey);
if (triggeredAlert != null) {
int times = triggeredAlert.getTimes() + 1;
triggeredAlert.setTimes(times);
triggeredAlert.setLastTriggerTime(currentTimeMilli);
int defineTimes = define.getTimes() == null ? 0 : define.getTimes();
if (times >= defineTimes) {
triggeredAlertMap.remove(monitorAlertKey);
dataQueue.addAlertData(triggeredAlert);
}
} else {
int times = 1;
fieldValueMap.put("app", app);
fieldValueMap.put("metrics", metrics);
fieldValueMap.put("metric", define.getField());
Map<String, String> tags = new HashMap<>(6);
tags.put(CommonConstants.TAG_MONITOR_ID, String.valueOf(monitorId));
tags.put(CommonConstants.TAG_MONITOR_APP, app);
Alert alert = Alert.builder().tags(tags).alertDefineId(define.getId()).priority(define.getPriority()).status(CommonConstants.ALERT_STATUS_CODE_PENDING).target(app + "." + metrics + "." + define.getField()).times(times).firstTriggerTime(currentTimeMilli).lastTriggerTime(currentTimeMilli).content(AlertTemplateUtil.render(define.getTemplate(), fieldValueMap)).build();
int defineTimes = define.getTimes() == null ? 0 : define.getTimes();
if (times >= defineTimes) {
dataQueue.addAlertData(alert);
} else {
triggeredAlertMap.put(monitorAlertKey, alert);
}
}
// 此优先级以下的阈值规则则忽略
break;
}
} catch (Exception e) {
log.warn(e.getMessage());
}
}
}
}
}
}
use of com.usthe.common.entity.alerter.Alert in project hertzbeat by dromara.
the class CalculateAlarm method handlerMonitorStatusAlert.
private void handlerMonitorStatusAlert(String monitorId, String app, CollectRep.Code code) {
Alert preAlert = triggeredAlertMap.get(monitorId);
long currentTimeMill = System.currentTimeMillis();
if (preAlert == null) {
Map<String, String> tags = new HashMap<>(6);
tags.put(CommonConstants.TAG_MONITOR_ID, monitorId);
tags.put(CommonConstants.TAG_MONITOR_APP, app);
Alert.AlertBuilder alertBuilder = Alert.builder().tags(tags).priority(CommonConstants.ALERT_PRIORITY_CODE_EMERGENCY).status(CommonConstants.ALERT_STATUS_CODE_PENDING).target(CommonConstants.AVAILABLE).content(this.bundle.getString("alerter.availability.emergency") + ": " + code.name()).firstTriggerTime(currentTimeMill).lastTriggerTime(currentTimeMill).nextEvalInterval(alerterProperties.getAlertEvalIntervalBase()).times(1);
if (code == CollectRep.Code.UN_REACHABLE) {
alertBuilder.target(CommonConstants.REACHABLE).content(this.bundle.getString("alerter.reachability.emergency") + ": " + code.name());
}
if (alerterProperties.getSystemAlertTriggerTimes() > 1) {
alertBuilder.nextEvalInterval(0L);
alertBuilder.status(CommonConstants.ALERT_STATUS_CODE_NOT_REACH);
Alert alert = alertBuilder.build();
triggeredAlertMap.put(monitorId, alert);
} else {
Alert alert = alertBuilder.build();
dataQueue.addAlertData(alert.clone());
triggeredAlertMap.put(monitorId, alert);
}
return;
}
if (preAlert.getLastTriggerTime() + preAlert.getNextEvalInterval() >= currentTimeMill) {
// 还在告警评估时间间隔静默期
preAlert.setTimes(preAlert.getTimes() + 1);
triggeredAlertMap.put(monitorId, preAlert);
} else {
preAlert.setTimes(preAlert.getTimes() + 1);
if (preAlert.getTimes() >= alerterProperties.getSystemAlertTriggerTimes()) {
preAlert.setLastTriggerTime(currentTimeMill);
long nextEvalInterval = preAlert.getNextEvalInterval() * 2;
if (preAlert.getNextEvalInterval() == 0L) {
nextEvalInterval = alerterProperties.getAlertEvalIntervalBase();
}
nextEvalInterval = Math.min(nextEvalInterval, alerterProperties.getMaxAlertEvalInterval());
preAlert.setNextEvalInterval(nextEvalInterval);
preAlert.setStatus(CommonConstants.ALERT_STATUS_CODE_PENDING);
triggeredAlertMap.put(monitorId, preAlert);
dataQueue.addAlertData(preAlert.clone());
} else {
triggeredAlertMap.put(monitorId, preAlert);
}
}
}
use of com.usthe.common.entity.alerter.Alert in project hertzbeat by dromara.
the class AlertsController method getAlerts.
@GetMapping
@ApiOperation(value = "Get a list of alarm information based on query filter items", notes = "根据查询过滤项获取告警信息列表")
public ResponseEntity<Message<Page<Alert>>> getAlerts(@ApiParam(value = "Alarm ID List | 告警IDS", example = "6565466456") @RequestParam(required = false) List<Long> ids, @ApiParam(value = "Alarm monitor object ID | 告警监控对象ID", example = "6565463543") @RequestParam(required = false) Long monitorId, @ApiParam(value = "Alarm level | 告警级别", example = "6565463543") @RequestParam(required = false) Byte priority, @ApiParam(value = "Alarm Status | 告警状态", example = "6565463543") @RequestParam(required = false) Byte status, @ApiParam(value = "Alarm content fuzzy query | 告警内容模糊查询", example = "linux") @RequestParam(required = false) String content, @ApiParam(value = "Sort field, default id | 排序字段,默认id", example = "name") @RequestParam(defaultValue = "id") String sort, @ApiParam(value = "Sort Type | 排序方式,asc:升序,desc:降序", example = "desc") @RequestParam(defaultValue = "desc") String order, @ApiParam(value = "List current page | 列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex, @ApiParam(value = "Number of list pagination | 列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
Specification<Alert> specification = (root, query, criteriaBuilder) -> {
List<Predicate> andList = new ArrayList<>();
if (ids != null && !ids.isEmpty()) {
CriteriaBuilder.In<Long> inPredicate = criteriaBuilder.in(root.get("id"));
for (long id : ids) {
inPredicate.value(id);
}
andList.add(inPredicate);
}
if (monitorId != null) {
Predicate predicate = criteriaBuilder.equal(root.get("monitorId"), monitorId);
andList.add(predicate);
}
if (priority != null) {
Predicate predicate = criteriaBuilder.equal(root.get("priority"), priority);
andList.add(predicate);
}
if (status != null) {
Predicate predicate = criteriaBuilder.equal(root.get("status"), status);
andList.add(predicate);
}
if (content != null && !"".equals(content)) {
Predicate predicateContent = criteriaBuilder.like(root.get("content"), "%" + content + "%");
andList.add(predicateContent);
}
Predicate[] predicates = new Predicate[andList.size()];
return criteriaBuilder.and(andList.toArray(predicates));
};
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
Page<Alert> alertPage = alertService.getAlerts(specification, pageRequest);
Message<Page<Alert>> message = new Message<>(alertPage);
return ResponseEntity.ok(message);
}
use of com.usthe.common.entity.alerter.Alert in project hertzbeat by dromara.
the class NoticeConfigServiceImpl method sendTestMsg.
@Override
public boolean sendTestMsg(NoticeReceiver noticeReceiver) {
Alert alert = new Alert();
alert.setTarget(ALERT_TEST_TARGET);
alert.setContent(ALERT_TEST_CONTENT);
alert.setTimes(1);
alert.setFirstTriggerTime(System.currentTimeMillis());
alert.setLastTriggerTime(System.currentTimeMillis());
alert.setPriority(CommonConstants.ALERT_PRIORITY_CODE_CRITICAL);
return dispatcherAlarm.sendNoticeMsg(noticeReceiver, alert);
}
Aggregations