整理下初始化数据表
This commit is contained in:
parent
798691d068
commit
ab052b14de
@ -3,7 +3,9 @@ package com.qqchen.deploy.backend.deploy.scheduler;
|
|||||||
import com.qqchen.deploy.backend.deploy.dto.ServerMonitorDataDTO;
|
import com.qqchen.deploy.backend.deploy.dto.ServerMonitorDataDTO;
|
||||||
import com.qqchen.deploy.backend.deploy.dto.ServerMonitorNotificationConfig;
|
import com.qqchen.deploy.backend.deploy.dto.ServerMonitorNotificationConfig;
|
||||||
import com.qqchen.deploy.backend.deploy.entity.Server;
|
import com.qqchen.deploy.backend.deploy.entity.Server;
|
||||||
|
import com.qqchen.deploy.backend.deploy.entity.ServerAlertRule;
|
||||||
import com.qqchen.deploy.backend.deploy.enums.ServerStatusEnum;
|
import com.qqchen.deploy.backend.deploy.enums.ServerStatusEnum;
|
||||||
|
import com.qqchen.deploy.backend.deploy.repository.IServerAlertRuleRepository;
|
||||||
import com.qqchen.deploy.backend.deploy.repository.IServerRepository;
|
import com.qqchen.deploy.backend.deploy.repository.IServerRepository;
|
||||||
import com.qqchen.deploy.backend.deploy.service.IServerAlertService;
|
import com.qqchen.deploy.backend.deploy.service.IServerAlertService;
|
||||||
import com.qqchen.deploy.backend.deploy.service.IServerMonitorService;
|
import com.qqchen.deploy.backend.deploy.service.IServerMonitorService;
|
||||||
@ -45,6 +47,9 @@ public class ServerMonitorScheduler {
|
|||||||
@Resource
|
@Resource
|
||||||
private IServerAlertService alertService;
|
private IServerAlertService alertService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private IServerAlertRuleRepository alertRuleRepository;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private INotificationService notificationService;
|
private INotificationService notificationService;
|
||||||
|
|
||||||
@ -116,17 +121,31 @@ public class ServerMonitorScheduler {
|
|||||||
log.info("监控数据已保存到数据库: count={}", monitorDataList.size());
|
log.info("监控数据已保存到数据库: count={}", monitorDataList.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. 检查告警规则
|
// 6. 检查告警规则(优化:只查询一次规则)
|
||||||
for (ServerMonitorDataDTO data : monitorDataList) {
|
if (!monitorDataList.isEmpty()) {
|
||||||
try {
|
// 一次性查询所有规则,避免 N 次数据库查询
|
||||||
alertService.checkAlertRules(data.getServerId(), data, config);
|
List<ServerAlertRule> allRules = alertRuleRepository.findAll();
|
||||||
} catch (Exception e) {
|
log.debug("开始检查告警规则: 服务器数={}, 规则数={}",
|
||||||
log.error("检查告警规则失败: serverId={}", data.getServerId(), e);
|
monitorDataList.size(), allRules.size());
|
||||||
|
|
||||||
|
for (ServerMonitorDataDTO data : monitorDataList) {
|
||||||
|
try {
|
||||||
|
alertService.checkAlertRules(data.getServerId(), data, allRules, config);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("检查告警规则失败: serverId={}", data.getServerId(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("服务器监控数据采集失败", e);
|
log.error("服务器监控数据采集失败", e);
|
||||||
|
} finally {
|
||||||
|
// 7. 自动清理历史监控数据
|
||||||
|
try {
|
||||||
|
cleanOldMonitorData();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("清理历史监控数据失败", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,9 @@ package com.qqchen.deploy.backend.deploy.service;
|
|||||||
|
|
||||||
import com.qqchen.deploy.backend.deploy.dto.ServerMonitorDataDTO;
|
import com.qqchen.deploy.backend.deploy.dto.ServerMonitorDataDTO;
|
||||||
import com.qqchen.deploy.backend.deploy.dto.ServerMonitorNotificationConfig;
|
import com.qqchen.deploy.backend.deploy.dto.ServerMonitorNotificationConfig;
|
||||||
|
import com.qqchen.deploy.backend.deploy.entity.ServerAlertRule;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 服务器告警服务接口
|
* 服务器告警服务接口
|
||||||
@ -9,7 +12,7 @@ import com.qqchen.deploy.backend.deploy.dto.ServerMonitorNotificationConfig;
|
|||||||
public interface IServerAlertService {
|
public interface IServerAlertService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查监控数据是否触发告警
|
* 检查监控数据是否触发告警(会自动查询规则)
|
||||||
*
|
*
|
||||||
* @param serverId 服务器ID
|
* @param serverId 服务器ID
|
||||||
* @param monitorData 监控数据
|
* @param monitorData 监控数据
|
||||||
@ -17,4 +20,16 @@ public interface IServerAlertService {
|
|||||||
*/
|
*/
|
||||||
void checkAlertRules(Long serverId, ServerMonitorDataDTO monitorData,
|
void checkAlertRules(Long serverId, ServerMonitorDataDTO monitorData,
|
||||||
ServerMonitorNotificationConfig config);
|
ServerMonitorNotificationConfig config);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查监控数据是否触发告警(传入已查询的规则,避免重复查询)
|
||||||
|
*
|
||||||
|
* @param serverId 服务器ID
|
||||||
|
* @param monitorData 监控数据
|
||||||
|
* @param allRules 所有告警规则(提前查询好的)
|
||||||
|
* @param config 通知配置(可选,为null则不发送通知)
|
||||||
|
*/
|
||||||
|
void checkAlertRules(Long serverId, ServerMonitorDataDTO monitorData,
|
||||||
|
List<ServerAlertRule> allRules,
|
||||||
|
ServerMonitorNotificationConfig config);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -48,14 +48,21 @@ public class ServerAlertServiceImpl implements IServerAlertService {
|
|||||||
@Override
|
@Override
|
||||||
public void checkAlertRules(Long serverId, ServerMonitorDataDTO monitorData,
|
public void checkAlertRules(Long serverId, ServerMonitorDataDTO monitorData,
|
||||||
ServerMonitorNotificationConfig config) {
|
ServerMonitorNotificationConfig config) {
|
||||||
|
// 查询所有规则并调用重载方法
|
||||||
|
List<ServerAlertRule> allRules = alertRuleRepository.findAll();
|
||||||
|
checkAlertRules(serverId, monitorData, allRules, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkAlertRules(Long serverId, ServerMonitorDataDTO monitorData,
|
||||||
|
List<ServerAlertRule> allRules,
|
||||||
|
ServerMonitorNotificationConfig config) {
|
||||||
if (monitorData == null) {
|
if (monitorData == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询该服务器的所有启用的告警规则(包括全局规则)
|
// 过滤并检查适用于该服务器的规则
|
||||||
List<ServerAlertRule> rules = alertRuleRepository.findAll();
|
for (ServerAlertRule rule : allRules) {
|
||||||
|
|
||||||
for (ServerAlertRule rule : rules) {
|
|
||||||
// 过滤:只检查全局规则或匹配的服务器规则
|
// 过滤:只检查全局规则或匹配的服务器规则
|
||||||
if (rule.getServerId() != null && !rule.getServerId().equals(serverId)) {
|
if (rule.getServerId() != null && !rule.getServerId().equals(serverId)) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@ -726,12 +726,22 @@ INSERT INTO `deploy-ease-platform`.`deploy_team_environment_notification_config`
|
|||||||
-- =====================================================
|
-- =====================================================
|
||||||
|
|
||||||
-- 服务器分类
|
-- 服务器分类
|
||||||
INSERT INTO deploy_server_category (id, name, code, icon, description, sort, enabled, create_by, create_time, update_by, update_time, version, deleted) VALUES
|
INSERT INTO `deploy-ease-platform`.`deploy_server_category` (`id`, `name`, `code`, `icon`, `description`, `sort`, `enabled`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (1, 'Web服务器', 'WEB_SERVER', 'server', 'Web应用服务器、前端服务器', 1, 1, 'system', NOW(), 'system', NOW(), 1, 0);
|
||||||
(1, 'Web服务器', 'WEB_SERVER', 'server', 'Web应用服务器、前端服务器', 1, 1, 'system', NOW(), 'system', NOW(), 1, 0),
|
INSERT INTO `deploy-ease-platform`.`deploy_server_category` (`id`, `name`, `code`, `icon`, `description`, `sort`, `enabled`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (2, '数据库服务器', 'DATABASE_SERVER', 'database', '数据库服务器、缓存服务器', 2, 1, 'system', NOW(), 'system', NOW(), 1, 0);
|
||||||
(2, '数据库服务器', 'DATABASE_SERVER', 'database', '数据库服务器、缓存服务器', 2, 1, 'system', NOW(), 'system', NOW(), 1, 0),
|
INSERT INTO `deploy-ease-platform`.`deploy_server_category` (`id`, `name`, `code`, `icon`, `description`, `sort`, `enabled`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (3, '中间件服务器', 'MIDDLEWARE_SERVER', 'cluster', '消息队列、搜索引擎等中间件', 3, 1, 'system', NOW(), 'system', NOW(), 1, 0);
|
||||||
(3, '中间件服务器', 'MIDDLEWARE_SERVER', 'cluster', '消息队列、搜索引擎等中间件', 3, 1, 'system', NOW(), 'system', NOW(), 1, 0),
|
INSERT INTO `deploy-ease-platform`.`deploy_server_category` (`id`, `name`, `code`, `icon`, `description`, `sort`, `enabled`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (4, '应用服务器', 'APP_SERVER', 'cloud-server', '业务应用服务器', 4, 1, 'system', NOW(), 'system', NOW(), 1, 0);
|
||||||
(4, '应用服务器', 'APP_SERVER', 'cloud-server', '业务应用服务器', 4, 1, 'system', NOW(), 'system', NOW(), 1, 0),
|
INSERT INTO `deploy-ease-platform`.`deploy_server_category` (`id`, `name`, `code`, `icon`, `description`, `sort`, `enabled`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (5, '其他', 'OTHER', 'hdd', '其他类型服务器', 99, 1, 'system', NOW(), 'system', NOW(), 1, 0);
|
||||||
(5, '其他', 'OTHER', 'hdd', '其他类型服务器', 99, 1, 'system', NOW(), 'system', NOW(), 1, 0);
|
INSERT INTO `deploy-ease-platform`.`deploy_server_category` (`id`, `name`, `code`, `icon`, `description`, `sort`, `enabled`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (6, '深圳本地服务器', 'shenzhen_locals', '', '', 0, 1, 'admin', NOW(), 'admin', NOW(), 1, 0);
|
||||||
|
INSERT INTO `deploy-ease-platform`.`deploy_server_category` (`id`, `name`, `code`, `icon`, `description`, `sort`, `enabled`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (7, '国产化', 'localization', '', '', 0, 1, 'dengqichen', NOW(), 'dengqichen', NOW(), 1, 0);
|
||||||
|
INSERT INTO `deploy-ease-platform`.`deploy_server_category` (`id`, `name`, `code`, `icon`, `description`, `sort`, `enabled`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (8, '华为云服务器', 'CLOUD', 'Cloud', '', 0, 1, 'admin', NOW(), 'admin', NOW(), 1, 0);
|
||||||
|
|
||||||
|
|
||||||
|
INSERT INTO `deploy-ease-platform`.`deploy_server` (`id`, `server_name`, `host_ip`, `ssh_port`, `ssh_user`, `auth_type`, `ssh_password`, `ssh_private_key`, `ssh_passphrase`, `category_id`, `os_type`, `os_version`, `hostname`, `status`, `description`, `cpu_cores`, `memory_size`, `disk_size`, `disk_info`, `tags`, `last_connect_time`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (1, 'SY测试环境服务器', '192.168.1.82', 22, 'root', 'PASSWORD', 'FQbCHbafPmGdPDpS', '', '', 6, 'LINUX', 'CentOS Linux 7 (Core)', 'engine-k8s', 'ONLINE', '', 128, 314, 1532, NULL, '[\"SY测试环境-临时用\", \"K8S\"]', NULL, 'admin', NOW(), 'admin', NOW(), 7, 0);
|
||||||
|
INSERT INTO `deploy-ease-platform`.`deploy_server` (`id`, `server_name`, `host_ip`, `ssh_port`, `ssh_user`, `auth_type`, `ssh_password`, `ssh_private_key`, `ssh_passphrase`, `category_id`, `os_type`, `os_version`, `hostname`, `status`, `description`, `cpu_cores`, `memory_size`, `disk_size`, `disk_info`, `tags`, `last_connect_time`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (2, '国产化38(APP1)', '124.127.238.38', 22, 'root', 'PASSWORD', '@1sdgCq123', '', '', 7, 'LINUX', 'Kylin Linux Advanced Server V10 (Lance)', 'app01', 'ONLINE', '', 32, 63, 201, NULL, NULL, NULL, 'dengqichen', NOW(), 'dengqichen', NOW(), 3, 0);
|
||||||
|
INSERT INTO `deploy-ease-platform`.`deploy_server` (`id`, `server_name`, `host_ip`, `ssh_port`, `ssh_user`, `auth_type`, `ssh_password`, `ssh_private_key`, `ssh_passphrase`, `category_id`, `os_type`, `os_version`, `hostname`, `status`, `description`, `cpu_cores`, `memory_size`, `disk_size`, `disk_info`, `tags`, `last_connect_time`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (3, '国产化(APP2)', '124.127.238.39', 22, 'root', 'PASSWORD', '@1sdgCq123', '', '', 7, 'LINUX', 'Kylin Linux Advanced Server V10 (Lance)', 'app02', 'ONLINE', '', 32, 63, 201, '[{\"totalSize\": 200, \"fileSystem\": \"xfs\", \"mountPoint\": \"/\"}, {\"totalSize\": 1, \"fileSystem\": \"vfat\", \"mountPoint\": \"/boot/efi\"}]', NULL, NOW(), 'dengqichen', NOW(), 'dengqichen', NOW(), 2, 0);
|
||||||
|
INSERT INTO `deploy-ease-platform`.`deploy_server` (`id`, `server_name`, `host_ip`, `ssh_port`, `ssh_user`, `auth_type`, `ssh_password`, `ssh_private_key`, `ssh_passphrase`, `category_id`, `os_type`, `os_version`, `hostname`, `status`, `description`, `cpu_cores`, `memory_size`, `disk_size`, `disk_info`, `tags`, `last_connect_time`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (4, '国产化(DBServer)', '219.142.42.183', 22, 'root', 'PASSWORD', '@1sdgCq123', '', '', 7, 'LINUX', 'Kylin Linux Advanced Server V10 (Lance)', 'dbserver', 'ONLINE', '', 8, 31, 501, '[{\"totalSize\": 500, \"fileSystem\": \"xfs\", \"mountPoint\": \"/\"}, {\"totalSize\": 1, \"fileSystem\": \"vfat\", \"mountPoint\": \"/boot/efi\"}]', NULL, NOW(), 'dengqichen', NOW(), 'admin', NOW(), 3, 0);
|
||||||
|
INSERT INTO `deploy-ease-platform`.`deploy_server` (`id`, `server_name`, `host_ip`, `ssh_port`, `ssh_user`, `auth_type`, `ssh_password`, `ssh_private_key`, `ssh_passphrase`, `category_id`, `os_type`, `os_version`, `hostname`, `status`, `description`, `cpu_cores`, `memory_size`, `disk_size`, `disk_info`, `tags`, `last_connect_time`, `create_by`, `create_time`, `update_by`, `update_time`, `version`, `deleted`) VALUES (5, '华为云116', '172.16.0.116', 22, 'root', 'PASSWORD', 'lianyu_123', '', '', 8, 'LINUX', 'CentOS Linux 7 (Core)', 'mysql', 'ONLINE', '', 8, 31, 533, '[{\"totalSize\": 40, \"fileSystem\": \"ext4\", \"mountPoint\": \"/\"}, {\"totalSize\": 493, \"fileSystem\": \"ext4\", \"mountPoint\": \"/mnt/data\"}]', NULL, NOW(), 'admin', NOW(), 'admin', NOW(), 4, 0);
|
||||||
|
|
||||||
|
|
||||||
-- =====================================================
|
-- =====================================================
|
||||||
-- 通知模板初始数据
|
-- 通知模板初始数据
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user