|
|
@@ -0,0 +1,207 @@
|
|
|
+package com.ufo.project.alarm.task;
|
|
|
+
|
|
|
+import java.lang.management.ManagementFactory;
|
|
|
+import java.nio.file.FileStore;
|
|
|
+import java.nio.file.FileSystems;
|
|
|
+
|
|
|
+import com.sun.management.OperatingSystemMXBean;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.ufo.module.system.domain.SysDictData;
|
|
|
+import com.ufo.module.system.service.ISysDictTypeService;
|
|
|
+import com.ufo.project.alarm.domain.AlarmEventRecord;
|
|
|
+import com.ufo.project.alarm.domain.AlarmEventRule;
|
|
|
+import com.ufo.project.alarm.domain.AlarmEventTemplate;
|
|
|
+import com.ufo.project.alarm.domain.enums.AlarmEventRecordStatus;
|
|
|
+import com.ufo.project.alarm.service.IAlarmEventRecordService;
|
|
|
+import com.ufo.project.alarm.service.IAlarmEventRuleService;
|
|
|
+import com.ufo.project.alarm.service.IAlarmEventTemplateService;
|
|
|
+import cn.hutool.http.Header;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class AlarmTask {
|
|
|
+
|
|
|
+ /** 扫描间隔(毫秒), 每轮结束后间隔5分钟 */
|
|
|
+ private static final long SCAN_INTERVAL_MILLIS = 300_000L;
|
|
|
+ /** 系统健康检查字典类型 */
|
|
|
+ private static final String SYSTEM_HEALTH_CHECK_DICT_TYPE = "ALARM_SYSTEM_HEALTH_CHECK";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysDictTypeService sysDictTypeService;
|
|
|
+ @Autowired
|
|
|
+ private IAlarmEventTemplateService alarmEventTemplateService;
|
|
|
+ @Autowired
|
|
|
+ private IAlarmEventRuleService alarmEventRuleService;
|
|
|
+ @Autowired
|
|
|
+ private IAlarmEventRecordService alarmEventRecordService;
|
|
|
+
|
|
|
+ @Scheduled(initialDelay = 17_000, fixedDelay = SCAN_INTERVAL_MILLIS)
|
|
|
+ public void systemHealthCheck() {
|
|
|
+ log.info("start to check system health...");
|
|
|
+ List<SysDictData> dictDataList = sysDictTypeService.selectDictDataByType(SYSTEM_HEALTH_CHECK_DICT_TYPE);
|
|
|
+ if(CollectionUtils.isEmpty(dictDataList)){
|
|
|
+ log.info("missing config of system health check, config dict type {}", SYSTEM_HEALTH_CHECK_DICT_TYPE);
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ JSONArray jsonArray = JSON.parseArray(dictDataList.get(0).getDictValue());
|
|
|
+ // 每个元素: {type: gatewayhealth, dictCode: gateway_app_config, "templateName": "", ruleCode: ""}
|
|
|
+ // {type: systeminfo}
|
|
|
+ jsonArray.forEach(item -> {
|
|
|
+ JSONObject jo = (JSONObject) item;
|
|
|
+ String type = jo.getString("type");
|
|
|
+ if(type.equals("gatewayhealth")){
|
|
|
+ this.handleGatewayHealth(jo);
|
|
|
+ }else if(type.equals("systeminfo")){
|
|
|
+ this.handleSystemInfo(jo);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ log.info("finished checking system health");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handleGatewayHealth(JSONObject jo) {
|
|
|
+ String gatewayAppConfigCode = jo.getString("dictCode");
|
|
|
+ String templateName = jo.getString("templateName");
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private void handleSystemInfo(JSONObject jo) {
|
|
|
+ this.checkMemoryUsage(jo);
|
|
|
+ this.checkDiskUsage(jo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("restriction")
|
|
|
+ private void checkMemoryUsage(JSONObject jo) {
|
|
|
+ OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
|
|
|
+ // 1. 获取系统总物理内存 (字节)
|
|
|
+ long totalPhysicalMemory = osBean.getTotalPhysicalMemorySize();
|
|
|
+ // 2. 获取系统剩余可用物理内存 (字节)
|
|
|
+ long freePhysicalMemory = osBean.getFreePhysicalMemorySize();
|
|
|
+ // 3. 计算已使用的物理内存
|
|
|
+ long usedPhysicalMemory = totalPhysicalMemory - freePhysicalMemory;
|
|
|
+ // 4. 计算内存使用率
|
|
|
+ double memoryUsageRatio = (double) usedPhysicalMemory / totalPhysicalMemory * 100;
|
|
|
+
|
|
|
+ String templateName = jo.getString("templateName");
|
|
|
+ String ruleCode = jo.getString("ruleCode");
|
|
|
+ AlarmEventRule rule = alarmEventRuleService.getOne(Wrappers.<AlarmEventRule>lambdaQuery().eq(AlarmEventRule::getRuleCode, ruleCode));
|
|
|
+ JSONObject conditionJson = JSON.parseObject(rule.getConditionJson());
|
|
|
+ double warningRatio = conditionJson.getJSONObject("warning").getDoubleValue("memory") * 100;
|
|
|
+ double alarmRatio = conditionJson.getJSONObject("alarm").getDoubleValue("memory") * 100;
|
|
|
+ // 5. 阈值告警逻辑
|
|
|
+ if (memoryUsageRatio >= alarmRatio) {
|
|
|
+ this.buildAlarmEventRecord(templateName, ruleCode, "系统物理内存使用率超过" + alarmRatio + "%");
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ if (memoryUsageRatio >= warningRatio) {
|
|
|
+ this.buildAlarmEventRecord(templateName, ruleCode, "系统物理内存使用率超过" + warningRatio + "%");
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkDiskUsage(JSONObject jo) {
|
|
|
+ Iterable<FileStore> fileStores = FileSystems.getDefault().getFileStores();
|
|
|
+
|
|
|
+ String templateName = jo.getString("templateName");
|
|
|
+ String ruleCode = jo.getString("ruleCode");
|
|
|
+ AlarmEventRule rule = alarmEventRuleService.getOne(Wrappers.<AlarmEventRule>lambdaQuery().eq(AlarmEventRule::getRuleCode, ruleCode));
|
|
|
+ JSONObject conditionJson = JSON.parseObject(rule.getConditionJson());
|
|
|
+ double warningRatio = conditionJson.getJSONObject("warning").getDoubleValue("disk") * 100;
|
|
|
+ double alarmRatio = conditionJson.getJSONObject("alarm").getDoubleValue("disk") * 100;
|
|
|
+ for (FileStore store : fileStores) {
|
|
|
+ // 过滤掉总空间为 0 的虚拟文件系统(如 /proc, /sys 等)
|
|
|
+ if (store.getTotalSpace() <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ long totalSpace = store.getTotalSpace(); // 总空间
|
|
|
+ long usableSpace = store.getUsableSpace(); // 当前用户可用的剩余空间
|
|
|
+ long usedSpace = totalSpace - usableSpace; // 已使用空间
|
|
|
+
|
|
|
+ double usageRatio = (double) usedSpace / totalSpace * 100;
|
|
|
+ if(usageRatio >= alarmRatio){
|
|
|
+ this.buildAlarmEventRecord(templateName, ruleCode, "系统磁盘" + store.name() + "使用率超过" + alarmRatio + "%");
|
|
|
+ continue ;
|
|
|
+ }
|
|
|
+ if(usageRatio >= warningRatio){
|
|
|
+ this.buildAlarmEventRecord(templateName, ruleCode, "系统磁盘" + store.name() + "使用率超过" + warningRatio + "%");
|
|
|
+ continue ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private AlarmEventRecord buildAlarmEventRecord(String templateName, String ruleCode, String content) {
|
|
|
+ AlarmEventTemplate template = alarmEventTemplateService.getOne(Wrappers.<AlarmEventTemplate>lambdaQuery().eq(AlarmEventTemplate::getTemplateName, templateName));
|
|
|
+ AlarmEventRule rule = alarmEventRuleService.getOne(Wrappers.<AlarmEventRule>lambdaQuery().eq(AlarmEventRule::getRuleCode, ruleCode));
|
|
|
+ return this.buildAlarmEventRecord(template, rule, content);
|
|
|
+ }
|
|
|
+ private AlarmEventRecord buildAlarmEventRecord(String templateName, AlarmEventRule rule, String content) {
|
|
|
+ AlarmEventTemplate template = alarmEventTemplateService.getOne(Wrappers.<AlarmEventTemplate>lambdaQuery().eq(AlarmEventTemplate::getTemplateName, templateName));
|
|
|
+ return this.buildAlarmEventRecord(template, rule, content);
|
|
|
+ }
|
|
|
+
|
|
|
+ private AlarmEventRecord buildAlarmEventRecord(AlarmEventTemplate template, AlarmEventRule rule, String content) {
|
|
|
+ LocalDateTime time = LocalDateTime.now();
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
|
|
+
|
|
|
+ Long templateId = null == template ? 0l : template.getId();
|
|
|
+ Integer level = null == template ? 1 : template.getLevel();
|
|
|
+
|
|
|
+ Long ruleId = null == rule ? 0l : rule.getId();
|
|
|
+
|
|
|
+ AlarmEventRecord record = new AlarmEventRecord().setStationId(0l)
|
|
|
+ .setSource("gatewayhealth_" + time.format(formatter))
|
|
|
+ .setAlarmTemplateId(templateId).setAlarmLevel(level).setStatus(AlarmEventRecordStatus.UNCONFIRMED)
|
|
|
+ .setAlarmTime(time).setAlarmEventRuleId(ruleId)
|
|
|
+ .setAlarmContent(content);
|
|
|
+ record.setCreateBy("system");
|
|
|
+ record.setCreateTime(new Date());
|
|
|
+ this.alarmEventRecordService.save(record);
|
|
|
+
|
|
|
+ return record;
|
|
|
+ }
|
|
|
+}
|