Commit 181b02cc authored by wujipeng's avatar wujipeng
Browse files

服务器cpu、内存超阈值报警

parent 99c2501f
......@@ -6,7 +6,9 @@ import com.example.operation_platform.model.dto.ProjectServerAlertsDTO;
import com.example.operation_platform.model.entity.ProjectServerAlertsEntity;
import com.example.operation_platform.model.vo.ProjectServerAlertsVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
import java.util.List;
/**
......@@ -19,4 +21,7 @@ import java.util.List;
public interface ProjectServerAlertsMapper extends BaseMapper<ProjectServerAlertsEntity> {
List<ProjectServerAlertsVO> alertsList(ProjectServerAlertsDTO projectServerAlertsDTO);
List<ProjectServerAlertsVO> selectData(@Param("time") Date time);
}
......@@ -14,4 +14,7 @@ import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class ProjectServerAlertsVO extends ProjectServerAlertsEntity {
private String projectId;
}
......@@ -5,6 +5,7 @@ import com.example.operation_platform.model.dto.ProjectServerAlertsDTO;
import com.example.operation_platform.model.entity.ProjectServerAlertsEntity;
import com.example.operation_platform.model.vo.ProjectServerAlertsVO;
import java.util.Date;
import java.util.List;
/**
......@@ -16,4 +17,6 @@ import java.util.List;
public interface ProjectServerAlertsService extends IService<ProjectServerAlertsEntity> {
List<ProjectServerAlertsVO> alertsList(ProjectServerAlertsDTO projectServerAlertsDTO);
List<ProjectServerAlertsVO> selectData(Date time);
}
......@@ -9,6 +9,7 @@ import com.example.operation_platform.model.vo.ProjectServerAlertsVO;
import com.example.operation_platform.service.ProjectServerAlertsService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
......@@ -23,4 +24,9 @@ public class ProjectServerAlertsServiceImpl extends ServiceImpl<ProjectServerAle
public List<ProjectServerAlertsVO> alertsList(ProjectServerAlertsDTO projectServerAlertsDTO) {
return baseMapper.alertsList(projectServerAlertsDTO);
}
@Override
public List<ProjectServerAlertsVO> selectData(Date time) {
return baseMapper.selectData(time);
}
}
package com.example.operation_platform.task;
import com.example.operation_platform.model.dto.BsWorkDTO;
import com.example.operation_platform.model.entity.ScheduleEntity;
import com.example.operation_platform.model.entity.UserEntity;
import com.example.operation_platform.model.vo.ProjectServerAlertsVO;
import com.example.operation_platform.service.BsWorkService;
import com.example.operation_platform.service.ProjectServerAlertsService;
import com.example.operation_platform.service.ScheduleService;
import com.example.operation_platform.service.UserService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @author : wujipeng
* @date : 2026/3/20 10:22
* @Version: 1.0
* @Desc : 描述信息
*/
@Component
public class ServerAlertTask {
@Resource
private ProjectServerAlertsService projectServerAlertsService;
@Resource
private BsWorkService bsWorkService;
@Resource
private ScheduleService scheduleService;
@Resource
private UserService userService;
@Scheduled(cron = "0 */1 * * * ?")
public void processRealTimeAlerts() {
//查询项目信息
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MINUTE, -1);
//查询一分钟内的数据
List<ProjectServerAlertsVO> projectServerAlertsList = projectServerAlertsService.selectData(calendar.getTime());
if (!projectServerAlertsList.isEmpty()) {
ScheduleEntity schedule = scheduleService.getUser();
UserEntity user = userService.getById(schedule.getUserId());
for (ProjectServerAlertsVO projectServerAlertsVO : projectServerAlertsList) {
BsWorkDTO bsWorkDTO = new BsWorkDTO();
bsWorkDTO.setProjectId(projectServerAlertsVO.getProjectId());
bsWorkDTO.setLevel("P1");
bsWorkDTO.setType("服务器异常");
bsWorkDTO.setDetail(projectServerAlertsVO.getDescription());
bsWorkDTO.setLoginName(user.getName());
bsWorkDTO.setHandleBy(schedule.getUserId());
bsWorkService.addWork(bsWorkDTO);
}
}
}
// @Scheduled(cron = "*/10 * * * * ?")
// public void processRealTimeAlertsTest() {
// ScheduleEntity schedule = scheduleService.getUser();
// UserEntity user = userService.getById(schedule.getUserId());
// BsWorkDTO bsWorkDTO = new BsWorkDTO();
// bsWorkDTO.setProjectId("2011321281488302081");
// bsWorkDTO.setLevel("P1");
// bsWorkDTO.setType("服务器异常");
// bsWorkDTO.setDetail("测试");
// bsWorkDTO.setLoginName(user.getName());
// bsWorkDTO.setHandleBy(schedule.getUserId());
// bsWorkService.addWork(bsWorkDTO);
// }
}
......@@ -21,4 +21,19 @@
alert_time
limit 10
</select>
<select id="selectData" resultType="com.example.operation_platform.model.vo.ProjectServerAlertsVO">
select psa.id,
psa.alert_time,
psa.alert_type,
psa.metric_value,
psa.threshold,
psa.ip_address,
psa.description,
psa.resolved,
bp.id as projectId
from project_server_alerts psa
left join bs_project bp on bp.ips like concat('%',psa.ip_address,'%')
where psa.alert_time >= #{time}
and psa.resolved = false
</select>
</mapper>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment