整理下初始化数据表

This commit is contained in:
dengqichen 2025-12-08 17:09:43 +08:00
parent c36ee0808c
commit 279c19ad7a
4 changed files with 101 additions and 73 deletions

View File

@ -12,6 +12,7 @@ import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -38,18 +39,18 @@ public class ServerCategoryApiController
}
@Override
public Response<ServerCategoryDTO> update(Long aLong, ServerCategoryDTO dto) {
return super.update(aLong, dto);
public Response<ServerCategoryDTO> update(@PathVariable Long id, @Validated @RequestBody ServerCategoryDTO dto) {
return super.update(id, dto);
}
@Override
public Response<Void> delete(Long aLong) {
return super.delete(aLong);
public Response<Void> delete(@PathVariable Long id) {
return super.delete(id);
}
@Override
public Response<ServerCategoryDTO> findById(Long aLong) {
return super.findById(aLong);
public Response<ServerCategoryDTO> findById(@PathVariable Long id) {
return super.findById(id);
}
@Override

View File

@ -29,16 +29,16 @@ public class ServerAlertRuleDTO extends BaseDTO {
@NotNull(message = "监控指标类型不能为空")
private MonitorMetricEnum alertType;
@Schema(description = "警告阈值(%)", required = true, example = "80.00")
@Schema(description = "警告阈值(CPU/MEMORY/DISK为%NETWORK为MB/s)", required = true, example = "80.00")
@NotNull(message = "警告阈值不能为空")
@DecimalMin(value = "0.00", message = "警告阈值必须大于等于0")
@DecimalMax(value = "100.00", message = "警告阈值不能超过100")
@DecimalMax(value = "10000.00", message = "警告阈值不能超过10000")
private BigDecimal warningThreshold;
@Schema(description = "严重阈值(%)", required = true, example = "90.00")
@Schema(description = "严重阈值(CPU/MEMORY/DISK为%NETWORK为MB/s)", required = true, example = "90.00")
@NotNull(message = "严重阈值不能为空")
@DecimalMin(value = "0.00", message = "严重阈值必须大于等于0")
@DecimalMax(value = "100.00", message = "严重阈值不能超过100")
@DecimalMax(value = "10000.00", message = "严重阈值不能超过10000")
private BigDecimal criticalThreshold;
@Schema(description = "持续时长(分钟)", example = "5")

View File

@ -1,5 +1,6 @@
package com.qqchen.deploy.backend.deploy.scheduler;
import com.qqchen.deploy.backend.deploy.dto.ServerInfoDTO;
import com.qqchen.deploy.backend.deploy.dto.ServerMonitorDataDTO;
import com.qqchen.deploy.backend.deploy.dto.ServerMonitorNotificationConfig;
import com.qqchen.deploy.backend.deploy.entity.Server;
@ -9,6 +10,7 @@ import com.qqchen.deploy.backend.deploy.repository.IServerAlertRuleRepository;
import com.qqchen.deploy.backend.deploy.repository.IServerRepository;
import com.qqchen.deploy.backend.deploy.service.IServerAlertService;
import com.qqchen.deploy.backend.deploy.service.IServerMonitorService;
import com.qqchen.deploy.backend.deploy.service.IServerService;
import com.qqchen.deploy.backend.framework.dto.DiskUsageInfo;
import com.qqchen.deploy.backend.framework.ssh.ISSHCommandService;
import com.qqchen.deploy.backend.framework.ssh.SSHCommandServiceFactory;
@ -53,6 +55,9 @@ public class ServerMonitorScheduler {
@Resource
private INotificationService notificationService;
@Resource
private IServerService serverService;
/**
* 采集所有在线服务器的监控数据
* 此方法由定时任务管理系统调用
@ -151,17 +156,38 @@ public class ServerMonitorScheduler {
/**
* 检测服务器连接状态并采集监控数据
* 统一使用 ServerService.testConnection() 方法进行连接测试和状态更新
*/
private ServerMonitorDataDTO collectSingleServerWithStatusCheck(Server server, ServerMonitorNotificationConfig config) {
try {
// 尝试采集监控数据
return collectSingleServer(server);
} catch (Exception e) {
// 采集失败说明服务器无法连接离线
// 1. 调用统一的连接测试方法会自动更新服务器状态硬件信息等
ServerInfoDTO info = serverService.testConnection(server.getId());
// 2. 检查连接状态
if (!info.getConnected()) {
// 连接失败离线发送离线通知
log.error("服务器连接失败(离线): serverId={}, name={}, ip={}, error={}",
server.getId(), server.getServerName(), server.getHostIp(), info.getErrorMessage());
if (config != null && config.getNotificationChannelId() != null && config.getServerOfflineTemplateId() != null) {
try {
sendServerOfflineNotification(server, config);
} catch (Exception notifyError) {
log.error("发送服务器离线通知失败: serverId={}", server.getId(), notifyError);
}
}
return null;
}
// 3. 连接成功采集监控数据
return collectServerMonitorData(server);
} catch (Exception e) {
// 异常情况发送离线通知
log.error("服务器连接测试异常: serverId={}, name={}, ip={}, error={}",
server.getId(), server.getServerName(), server.getHostIp(), e.getMessage());
// 发送离线通知
if (config != null && config.getNotificationChannelId() != null && config.getServerOfflineTemplateId() != null) {
try {
sendServerOfflineNotification(server, config);
@ -204,9 +230,10 @@ public class ServerMonitorScheduler {
}
/**
* 采集单台服务器的监控数据
* 采集服务器监控数据CPU内存磁盘使用率
* 注意此方法仅负责采集监控数据不负责连接测试和状态更新
*/
private ServerMonitorDataDTO collectSingleServer(Server server) {
private ServerMonitorDataDTO collectServerMonitorData(Server server) throws Exception {
SSHClient sshClient = null;
ISSHCommandService sshService = null;
@ -270,7 +297,7 @@ public class ServerMonitorScheduler {
} catch (Exception e) {
log.error("采集服务器监控数据失败: serverId={}, serverName={}, error={}",
server.getId(), server.getServerName(), e.getMessage());
return null;
throw e; // 抛出异常让上层处理
} finally {
// 6. 关闭SSH连接
if (sshService != null && sshClient != null) {

View File

@ -1200,9 +1200,9 @@ CREATE TABLE deploy_server_alert_rule
-- 告警类型
alert_type VARCHAR(20) NOT NULL COMMENT '告警类型: CPU/MEMORY/DISK',
-- 阈值
warning_threshold DECIMAL(5,2) NOT NULL COMMENT '警告阈值(%)',
critical_threshold DECIMAL(5,2) NOT NULL COMMENT '严重阈值(%)',
-- 阈值支持百分比和绝对值CPU/MEMORY/DISK为%NETWORK为MB/s
warning_threshold DECIMAL(10,2) NOT NULL COMMENT '警告阈值',
critical_threshold DECIMAL(10,2) NOT NULL COMMENT '严重阈值',
-- 持续时间(避免误报)
duration_minutes INT DEFAULT 5 COMMENT '持续时长(分钟)',