|
@@ -1,6 +1,7 @@
|
|
|
package com.ufo.project.alarm.task;
|
|
package com.ufo.project.alarm.task;
|
|
|
|
|
|
|
|
import java.lang.management.ManagementFactory;
|
|
import java.lang.management.ManagementFactory;
|
|
|
|
|
+import java.net.ConnectException;
|
|
|
import java.nio.file.FileStore;
|
|
import java.nio.file.FileStore;
|
|
|
import java.nio.file.FileSystems;
|
|
import java.nio.file.FileSystems;
|
|
|
|
|
|
|
@@ -31,6 +32,8 @@ import com.ufo.project.alarm.domain.enums.AlarmEventRecordStatus;
|
|
|
import com.ufo.project.alarm.service.IAlarmEventRecordService;
|
|
import com.ufo.project.alarm.service.IAlarmEventRecordService;
|
|
|
import com.ufo.project.alarm.service.IAlarmEventRuleService;
|
|
import com.ufo.project.alarm.service.IAlarmEventRuleService;
|
|
|
import com.ufo.project.alarm.service.IAlarmEventTemplateService;
|
|
import com.ufo.project.alarm.service.IAlarmEventTemplateService;
|
|
|
|
|
+
|
|
|
|
|
+import cn.hutool.core.io.IORuntimeException;
|
|
|
import cn.hutool.http.Header;
|
|
import cn.hutool.http.Header;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
import cn.hutool.http.HttpResponse;
|
|
@@ -78,41 +81,51 @@ public class AlarmTask {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void handleGatewayHealth(JSONObject jo) {
|
|
private void handleGatewayHealth(JSONObject jo) {
|
|
|
- String gatewayAppConfigCode = jo.getString("dictCode");
|
|
|
|
|
String templateName = jo.getString("templateName");
|
|
String templateName = jo.getString("templateName");
|
|
|
String ruleCode = jo.getString("ruleCode");
|
|
String ruleCode = jo.getString("ruleCode");
|
|
|
- List<SysDictData> dictDataList = sysDictTypeService.selectDictDataByTypeWithoutCache(gatewayAppConfigCode);
|
|
|
|
|
- if(CollectionUtils.isEmpty(dictDataList)){
|
|
|
|
|
- log.info("missing config of gateway app config, config dict type {}", gatewayAppConfigCode);
|
|
|
|
|
- this.buildAlarmEventRecord(templateName, ruleCode, "缺少网关系统配置项 gateway_app_config");
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- String dictValue = dictDataList.get(0).getDictValue();
|
|
|
|
|
- JSONObject gatewayConfigJo = JSON.parseObject(dictValue);
|
|
|
|
|
-
|
|
|
|
|
- String url = gatewayConfigJo.getString("baseUrl") + gatewayConfigJo.getString("pointStateUrl");
|
|
|
|
|
- List<Map<String, Object>> body = new ArrayList<>();
|
|
|
|
|
- Map<String, Object> item = new LinkedHashMap<>();
|
|
|
|
|
- item.put("channelId", -1);
|
|
|
|
|
- item.put("deviceId", "-1");
|
|
|
|
|
- item.put("pointType", "-1");
|
|
|
|
|
- item.put("pointCode", "-1");
|
|
|
|
|
- body.add(item);
|
|
|
|
|
- HttpResponse response = HttpRequest.post(url)
|
|
|
|
|
- .header(Header.CONTENT_TYPE, "application/json; charset=UTF-8")
|
|
|
|
|
- .body(JSON.toJSONString(body))
|
|
|
|
|
- .timeout(3000)
|
|
|
|
|
- .execute();
|
|
|
|
|
- // 检查响应状态码
|
|
|
|
|
- if (response.getStatus() != 200 && response.getStatus() != 500) {
|
|
|
|
|
- log.info("gateway health check failed, status code: {}", response.getStatus());
|
|
|
|
|
- this.buildAlarmEventRecord(templateName, ruleCode, "网关请求失败");
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ String gatewayAppConfigCode = jo.getString("dictCode");
|
|
|
|
|
+ List<SysDictData> dictDataList = sysDictTypeService.selectDictDataByTypeWithoutCache(gatewayAppConfigCode);
|
|
|
|
|
+ if(CollectionUtils.isEmpty(dictDataList)){
|
|
|
|
|
+ log.info("missing config of gateway app config, config dict type {}", gatewayAppConfigCode);
|
|
|
|
|
+ this.buildAlarmEventRecord(templateName, ruleCode, "缺少网关系统配置项 gateway_app_config");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ String dictValue = dictDataList.get(0).getDictValue();
|
|
|
|
|
+ JSONObject gatewayConfigJo = JSON.parseObject(dictValue);
|
|
|
|
|
+
|
|
|
|
|
+ String url = gatewayConfigJo.getString("baseUrl") + gatewayConfigJo.getString("pointStateUrl");
|
|
|
|
|
+ List<Map<String, Object>> body = new ArrayList<>();
|
|
|
|
|
+ Map<String, Object> item = new LinkedHashMap<>();
|
|
|
|
|
+ item.put("channelId", -1);
|
|
|
|
|
+ item.put("deviceId", "-1");
|
|
|
|
|
+ item.put("pointType", "-1");
|
|
|
|
|
+ item.put("pointCode", "-1");
|
|
|
|
|
+ body.add(item);
|
|
|
|
|
+ HttpRequest.post(url)
|
|
|
|
|
+ .header(Header.CONTENT_TYPE, "application/json; charset=UTF-8")
|
|
|
|
|
+ .body(JSON.toJSONString(body))
|
|
|
|
|
+ .timeout(3000)
|
|
|
|
|
+ .execute();
|
|
|
|
|
+ } catch (IORuntimeException e) {
|
|
|
|
|
+ Throwable cause = e.getCause();
|
|
|
|
|
+ if (cause instanceof ConnectException) {
|
|
|
|
|
+ log.info("网关连接被拒绝,请检查服务是否启动或端口是否正确");
|
|
|
|
|
+ this.buildAlarmEventRecord(templateName, ruleCode, "网关连接失败(Connection refused)");
|
|
|
|
|
+ }else {
|
|
|
|
|
+ log.error("gateway health check failed", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("gateway health check failed", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
private void handleSystemInfo(JSONObject jo) {
|
|
private void handleSystemInfo(JSONObject jo) {
|
|
|
- this.checkMemoryUsage(jo);
|
|
|
|
|
- this.checkDiskUsage(jo);
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ this.checkMemoryUsage(jo);
|
|
|
|
|
+ this.checkDiskUsage(jo);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("system info check failed", e);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("restriction")
|
|
@SuppressWarnings("restriction")
|
|
@@ -133,18 +146,19 @@ public class AlarmTask {
|
|
|
JSONObject conditionJson = JSON.parseObject(rule.getConditionJson());
|
|
JSONObject conditionJson = JSON.parseObject(rule.getConditionJson());
|
|
|
double warningRatio = conditionJson.getJSONObject("warning").getDoubleValue("memory") * 100;
|
|
double warningRatio = conditionJson.getJSONObject("warning").getDoubleValue("memory") * 100;
|
|
|
double alarmRatio = conditionJson.getJSONObject("alarm").getDoubleValue("memory") * 100;
|
|
double alarmRatio = conditionJson.getJSONObject("alarm").getDoubleValue("memory") * 100;
|
|
|
|
|
+ log.info("system memory usage ratio: {}%", memoryUsageRatio);
|
|
|
// 5. 阈值告警逻辑
|
|
// 5. 阈值告警逻辑
|
|
|
if (memoryUsageRatio >= alarmRatio) {
|
|
if (memoryUsageRatio >= alarmRatio) {
|
|
|
- this.buildAlarmEventRecord(templateName, ruleCode, "系统物理内存使用率超过" + alarmRatio + "%");
|
|
|
|
|
|
|
+ this.buildAlarmEventRecord(templateName, rule, "系统物理内存使用率超过" + alarmRatio + "%");
|
|
|
return ;
|
|
return ;
|
|
|
}
|
|
}
|
|
|
if (memoryUsageRatio >= warningRatio) {
|
|
if (memoryUsageRatio >= warningRatio) {
|
|
|
- this.buildAlarmEventRecord(templateName, ruleCode, "系统物理内存使用率超过" + warningRatio + "%");
|
|
|
|
|
|
|
+ this.buildAlarmEventRecord(templateName, rule, "系统物理内存使用率超过" + warningRatio + "%");
|
|
|
return ;
|
|
return ;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void checkDiskUsage(JSONObject jo) {
|
|
|
|
|
|
|
+ private void checkDiskUsage(JSONObject jo) throws Exception {
|
|
|
Iterable<FileStore> fileStores = FileSystems.getDefault().getFileStores();
|
|
Iterable<FileStore> fileStores = FileSystems.getDefault().getFileStores();
|
|
|
|
|
|
|
|
String templateName = jo.getString("templateName");
|
|
String templateName = jo.getString("templateName");
|
|
@@ -163,12 +177,13 @@ public class AlarmTask {
|
|
|
long usedSpace = totalSpace - usableSpace; // 已使用空间
|
|
long usedSpace = totalSpace - usableSpace; // 已使用空间
|
|
|
|
|
|
|
|
double usageRatio = (double) usedSpace / totalSpace * 100;
|
|
double usageRatio = (double) usedSpace / totalSpace * 100;
|
|
|
|
|
+ log.info("disk {} usage ratio: {}%", store.name(), usageRatio);
|
|
|
if(usageRatio >= alarmRatio){
|
|
if(usageRatio >= alarmRatio){
|
|
|
- this.buildAlarmEventRecord(templateName, ruleCode, "系统磁盘" + store.name() + "使用率超过" + alarmRatio + "%");
|
|
|
|
|
|
|
+ this.buildAlarmEventRecord(templateName, rule, "系统磁盘" + store.name() + "使用率超过" + alarmRatio + "%");
|
|
|
continue ;
|
|
continue ;
|
|
|
}
|
|
}
|
|
|
if(usageRatio >= warningRatio){
|
|
if(usageRatio >= warningRatio){
|
|
|
- this.buildAlarmEventRecord(templateName, ruleCode, "系统磁盘" + store.name() + "使用率超过" + warningRatio + "%");
|
|
|
|
|
|
|
+ this.buildAlarmEventRecord(templateName, rule, "系统磁盘" + store.name() + "使用率超过" + warningRatio + "%");
|
|
|
continue ;
|
|
continue ;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|