重写实例状态检测方法

This commit is contained in:
2023-03-23 12:10:30 +08:00
parent f2d313721f
commit e30c8e597c
6 changed files with 201 additions and 75 deletions
@@ -13,16 +13,14 @@ import com.mzaxd.noodles.util.CpuUtil;
import com.mzaxd.noodles.util.RedisCache; import com.mzaxd.noodles.util.RedisCache;
import com.mzaxd.noodles.util.UrlUtil; import com.mzaxd.noodles.util.UrlUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.*; import java.util.concurrent.*;
/** /**
@@ -50,6 +48,9 @@ public class CheckInstancesStatus {
@Resource @Resource
private RedisCache redisCache; private RedisCache redisCache;
@Resource
private RedisTemplate redisTemplate;
/** /**
* 检查实例状态并且发送对应的提醒 * 检查实例状态并且发送对应的提醒
*/ */
@@ -96,21 +97,29 @@ public class CheckInstancesStatus {
container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED); container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED);
// 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒 // 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS); Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS);
if (!CollectionUtils.isEmpty(set)) { if (CollectionUtils.isEmpty(set)) {
// 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回 set = new HashSet<>();
if (set.contains(container.getId().toString())) { }
// 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回
if (set.contains(container.getId().toString())) {
return container;
} else {
// 根据实例对应的提醒方式进行提醒
if (container.getNotify().equals(SystemConstant.NOTIFY_NO)) {
return container; return container;
} else { }
// 根据实例对应的提醒方式进行提醒 if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) {
if (container.getNotify().equals(SystemConstant.NOTIFY_NO)) { notificationService.sendContainerOfflineNotification(container.getId());
return container; set.add(container.getId().toString());
} else { }
notificationService.sendContainerOfflineNotification(container.getId()); if (container.getNotify().equals(SystemConstant.NOTIFY_EMAIL)) {
} notificationService.sendContainerOfflineEmail(container.getId());
}
if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER_EMAIL)) {
notificationService.sendContainerOfflineNotificationEmail(container.getId());
set.add(container.getId().toString());
} }
} }
// 存入 Redis
set.add(container.getId().toString());
redisCache.setCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS, set); redisCache.setCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS, set);
} }
return container; return container;
@@ -145,7 +154,7 @@ public class CheckInstancesStatus {
} }
//通过Ping的方式判断是否在线 //通过Ping的方式判断是否在线
if (StringUtils.hasText(vm.getServerAddress())) { if (StringUtils.hasText(vm.getServerAddress())) {
if (UrlUtil.isHostOnline(vm.getServerAddress())){ if (UrlUtil.isHostOnline(vm.getServerAddress())) {
log.info("[实例状态检测]:与{}建立连接成功", vm.getName()); log.info("[实例状态检测]:与{}建立连接成功", vm.getName());
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE); vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
return; return;
@@ -160,21 +169,29 @@ public class CheckInstancesStatus {
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE); vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE);
//判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒 //判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_VM_IDS); Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_VM_IDS);
if (!CollectionUtils.isEmpty(set)) { if (CollectionUtils.isEmpty(set)) {
//如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回 set = new HashSet<>();
if (set.contains(vm.getId().toString())) { }
//如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回
if (set.contains(vm.getId().toString())) {
return;
} else {
//根据实例对应的提醒方式进行提醒
if (vm.getNotify().equals(SystemConstant.NOTIFY_NO)) {
return; return;
} else { }
//根据实例对应的提醒方式进行提醒 if (vm.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) {
if (vm.getNotify().equals(SystemConstant.NOTIFY_NO)) { notificationService.sendContainerOfflineNotification(vm.getId());
return; set.add(vm.getId().toString());
} else { }
notificationService.sendVmOfflineNotification(vm.getId()); if (vm.getNotify().equals(SystemConstant.NOTIFY_EMAIL)) {
} notificationService.sendContainerOfflineEmail(vm.getId());
}
if (vm.getNotify().equals(SystemConstant.NOTIFY_BROWSER_EMAIL)) {
notificationService.sendContainerOfflineNotificationEmail(vm.getId());
set.add(vm.getId().toString());
} }
} }
//存入redis
set.add(vm.getId().toString());
redisCache.setCacheSet(RedisConstant.NOTIFY_VM_IDS, set); redisCache.setCacheSet(RedisConstant.NOTIFY_VM_IDS, set);
} }
}); });
@@ -200,21 +217,29 @@ public class CheckInstancesStatus {
} }
//判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒 //判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_HOST_IDS); Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_HOST_IDS);
if (!CollectionUtils.isEmpty(set)) { if (CollectionUtils.isEmpty(set)) {
//如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回 set = new HashSet<>();
if (set.contains(hostMachine.getId().toString())) { }
//如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回
if (set.contains(hostMachine.getId().toString())) {
return;
} else {
//根据实例对应的提醒方式进行提醒
if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_NO)) {
return; return;
} else { }
//根据实例对应的提醒方式进行提醒 if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) {
if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_NO)) { notificationService.sendContainerOfflineNotification(hostMachine.getId());
return; set.add(hostMachine.getId().toString());
} else { }
notificationService.sendHostOfflineNotification(hostMachine.getId()); if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_EMAIL)) {
} notificationService.sendContainerOfflineEmail(hostMachine.getId());
}
if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_BROWSER_EMAIL)) {
notificationService.sendContainerOfflineNotificationEmail(hostMachine.getId());
set.add(hostMachine.getId().toString());
} }
} }
//存入redis
set.add(hostMachine.getId().toString());
redisCache.setCacheSet(RedisConstant.NOTIFY_HOST_IDS, set); redisCache.setCacheSet(RedisConstant.NOTIFY_HOST_IDS, set);
} }
}); });
@@ -17,18 +17,54 @@ public interface NotificationService extends IService<Notification> {
*/ */
void sendContainerOfflineNotification(Long id); void sendContainerOfflineNotification(Long id);
/**
* 发送容器掉线通知和邮件
* @param id
*/
void sendContainerOfflineNotificationEmail(Long id);
/**
* 发送容器掉线邮件
* @param id
*/
void sendContainerOfflineEmail(Long id);
/** /**
* 发送虚拟机掉线通知 * 发送虚拟机掉线通知
* @param id * @param id
*/ */
void sendVmOfflineNotification(Long id); void sendVmOfflineNotification(Long id);
/**
* 发送虚拟机掉线邮件
* @param id
*/
void sendVmOfflineEmail(Long id);
/**
* 发送虚拟机掉线通知和邮件
* @param id
*/
void sendVmOfflineNotificationEmail(Long id);
/** /**
* 发送物理机掉线通知 * 发送物理机掉线通知
* @param id * @param id
*/ */
void sendHostOfflineNotification(Long id); void sendHostOfflineNotification(Long id);
/**
* 发送物理机掉线邮件
* @param id
*/
void sendHostOfflineEmail(Long id);
/**
* 发送物理机掉线通知和邮件
* @param id
*/
void sendHostOfflineNotificationEmail(Long id);
/** /**
* 提醒列表 * 提醒列表
@@ -55,15 +55,10 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
@Resource @Resource
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
@Override
public void sendContainerOfflineNotification(Long id) { public void sendContainerOfflineNotification(Long id) {
Notification notification = new Notification(); Notification notification = new Notification();
Container container = containerService.getById(id); Container container = containerService.getById(id);
Integer notify = container.getNotify();
//判断发送方式
if (notify.equals(SystemConstant.NOTIFY_BROWSER_EMAIL) || notify.equals(SystemConstant.NOTIFY_EMAIL)) {
//发送邮件
sendContainerOfflineEmail(id);
}
notification.setTitle("容器掉线") notification.setTitle("容器掉线")
.setType(SystemConstant.OFFLINE_NOTIFICATION) .setType(SystemConstant.OFFLINE_NOTIFICATION)
.setSendType(container.getNotify()) .setSendType(container.getNotify())
@@ -75,7 +70,14 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
log.info("[掉线提醒]:发送浏览器提醒:{}", content); log.info("[掉线提醒]:发送浏览器提醒:{}", content);
} }
private void sendContainerOfflineEmail(Long id) { @Override
public void sendContainerOfflineNotificationEmail(Long id) {
sendContainerOfflineNotification(id);
sendContainerOfflineEmail(id);
}
@Override
public void sendContainerOfflineEmail(Long id) {
Container container = containerService.getById(id); Container container = containerService.getById(id);
String time = LocalDateTime.now().toString(); String time = LocalDateTime.now().toString();
String content = String.format("发现容器[ %s ]掉线---- %s", container.getName(), time); String content = String.format("发现容器[ %s ]掉线---- %s", container.getName(), time);
@@ -87,12 +89,6 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
public void sendVmOfflineNotification(Long id) { public void sendVmOfflineNotification(Long id) {
Notification notification = new Notification(); Notification notification = new Notification();
HostMachine vm = hostMachineService.getById(id); HostMachine vm = hostMachineService.getById(id);
Integer notify = vm.getNotify();
//判断发送方式
if (notify.equals(SystemConstant.NOTIFY_BROWSER_EMAIL) || notify.equals(SystemConstant.NOTIFY_EMAIL)) {
//发送邮件
sendVmOfflineEmail(id);
}
notification.setTitle("虚拟机掉线") notification.setTitle("虚拟机掉线")
.setType(SystemConstant.OFFLINE_NOTIFICATION) .setType(SystemConstant.OFFLINE_NOTIFICATION)
.setSendType(vm.getNotify()) .setSendType(vm.getNotify())
@@ -104,7 +100,8 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
log.info("[掉线提醒]:发送浏览器提醒:{}", content); log.info("[掉线提醒]:发送浏览器提醒:{}", content);
} }
private void sendVmOfflineEmail(Long id) { @Override
public void sendVmOfflineEmail(Long id) {
HostMachine vm = hostMachineService.getById(id); HostMachine vm = hostMachineService.getById(id);
String time = LocalDateTime.now().toString(); String time = LocalDateTime.now().toString();
String content = String.format("发现虚拟机[ %s ]掉线---- %s", vm.getName(), time); String content = String.format("发现虚拟机[ %s ]掉线---- %s", vm.getName(), time);
@@ -112,16 +109,16 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
log.info("[掉线提醒]:发送邮件提醒:{}", content); log.info("[掉线提醒]:发送邮件提醒:{}", content);
} }
@Override
public void sendVmOfflineNotificationEmail(Long id) {
sendVmOfflineNotification(id);
sendVmOfflineEmail(id);
}
@Override @Override
public void sendHostOfflineNotification(Long id) { public void sendHostOfflineNotification(Long id) {
Notification notification = new Notification(); Notification notification = new Notification();
HostMachine host = hostMachineService.getById(id); HostMachine host = hostMachineService.getById(id);
Integer notify = host.getNotify();
//判断发送方式
if (notify.equals(SystemConstant.NOTIFY_BROWSER_EMAIL) || notify.equals(SystemConstant.NOTIFY_EMAIL)) {
//发送邮件
sendHostOfflineEmail(id);
}
notification.setTitle("物理机掉线") notification.setTitle("物理机掉线")
.setType(SystemConstant.OFFLINE_NOTIFICATION) .setType(SystemConstant.OFFLINE_NOTIFICATION)
.setSendType(host.getNotify()) .setSendType(host.getNotify())
@@ -133,6 +130,21 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
log.info("[掉线提醒]:发送浏览器提醒:{}", content); log.info("[掉线提醒]:发送浏览器提醒:{}", content);
} }
@Override
public void sendHostOfflineEmail(Long id) {
HostMachine host = hostMachineService.getById(id);
String time = LocalDateTime.now().toString();
String content = String.format("发现物理机[ %s ]掉线---- %s", host.getName(), time);
MailUtil.send(systemSettingUtils.getMailAccount(), CollUtil.newArrayList(systemSettingUtils.getMailTarget()), "Noodles掉线提醒", content, false);
log.info("[掉线提醒]:发送邮件提醒:{}", content);
}
@Override
public void sendHostOfflineNotificationEmail(Long id) {
sendHostOfflineNotification(id);
sendHostOfflineEmail(id);
}
@Override @Override
public ResponseResult getNotificationList(Integer tab, Integer perPage, Integer currentPage) { public ResponseResult getNotificationList(Integer tab, Integer perPage, Integer currentPage) {
LambdaQueryWrapper<Notification> notificationWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<Notification> notificationWrapper = new LambdaQueryWrapper<>();
@@ -219,13 +231,6 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
return ResponseResult.okResult(count(notificationLambdaQueryWrapper)); return ResponseResult.okResult(count(notificationLambdaQueryWrapper));
} }
private void sendHostOfflineEmail(Long id) {
HostMachine host = hostMachineService.getById(id);
String time = LocalDateTime.now().toString();
String content = String.format("发现物理机[ %s ]掉线---- %s", host.getName(), time);
MailUtil.send(systemSettingUtils.getMailAccount(), CollUtil.newArrayList(systemSettingUtils.getMailTarget()), "Noodles掉线提醒", content, false);
log.info("[掉线提醒]:发送邮件提醒:{}", content);
}
} }
@@ -51,7 +51,7 @@ public class UrlUtil {
public static boolean isServiceOnline(String serverName, int port) throws IOException { public static boolean isServiceOnline(String serverName, int port) throws IOException {
Socket socket = new Socket(serverName, port); Socket socket = new Socket(serverName, port);
log.info("[实例状态检测]" + serverName + "服务已经开启"); log.info("[实例状态检测]" + serverName + ":" + port + "服务已经开启");
socket.close(); socket.close();
return true; return true;
} }
+9 -4
View File
@@ -3,7 +3,7 @@ server:
spring: spring:
#MySQL配置 #MySQL配置
datasource: datasource:
url: jdbc:mysql://192.168.1.103:3307/noodles?characterEncoding=utf-8&serverTimezone=Asia/Shanghai url: jdbc:mysql://localhost:3306/noodles?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root username: root
password: rootroot password: rootroot
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
@@ -14,7 +14,7 @@ spring:
#Redis配置 #Redis配置
redis: redis:
host: 192.168.1.103 host: 192.168.1.103
database: 3 database: 2
#RabbitMq配置 #RabbitMq配置
rabbitmq: rabbitmq:
host: 192.168.1.103 host: 192.168.1.103
@@ -38,10 +38,11 @@ management:
health: health:
show-details: ALWAYS show-details: ALWAYS
mybatis-plus: mybatis-plus:
#打印SQL日志 建议生产环境关掉 否则日志爆炸 #打印SQL日志 建议生产环境关掉 否则日志爆炸
configuration: # configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config: global-config:
db-config: db-config:
logic-delete-field: delFlag logic-delete-field: delFlag
@@ -53,3 +54,7 @@ pagehelper:
reasonable: true reasonable: true
support-methods-arguments: true support-methods-arguments: true
page-size-zero: true page-size-zero: true
logging:
level:
com.mzaxd.noodles: info
@@ -501,7 +501,7 @@ class NoodlesApplicationTests {
} }
//通过Ping的方式判断是否在线 //通过Ping的方式判断是否在线
if (StringUtils.hasText(vm.getServerAddress())) { if (StringUtils.hasText(vm.getServerAddress())) {
if (UrlUtil.isHostOnline(vm.getServerAddress())){ if (UrlUtil.isHostOnline(vm.getServerAddress())) {
log.info("[实例状态检测]:与{}建立连接成功", vm.getName()); log.info("[实例状态检测]:与{}建立连接成功", vm.getName());
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE); vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
return; return;
@@ -629,4 +629,59 @@ class NoodlesApplicationTests {
System.out.println("解析失败"); System.out.println("解析失败");
} }
} }
@Test
public void containerCheck() {
List<Container> containers = containerService.list();
containers.forEach(container -> {
try {
if (!StringUtils.hasText(container.getWebUi()) && !StringUtils.hasText(container.getServerAddress())) {
container.setContainerState(SystemConstant.CONTAINER_STATE_UNKNOWN);
return;
}
//通过Socket(IP+端口)判断是否在线
if (StringUtils.hasText(container.getServerAddress())) {
if (UrlUtil.isServiceOnline(UrlUtil.getHostname(container.getServerAddress()), UrlUtil.getPort(container.getServerAddress()))) {
container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING);
return;
}
}
//通过HTTP请求判断是否在线
HttpRequest.get(container.getWebUi()).setConnectionTimeout(5000).execute(true);
log.info("[实例状态检测]:与{}建立连接成功", container.getName());
container.setContainerState(SystemConstant.CONTAINER_STATE_RUNNING);
} catch (Exception exception) {
log.info("[实例状态检测]:与{}建立连接失败,状态转为离线", container.getName());
container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED);
// 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS);
if (CollectionUtils.isEmpty(set)) {
set = new HashSet<>();
}
// 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回
if (set.contains(container.getId().toString())) {
return;
} else {
// 根据实例对应的提醒方式进行提醒
if (container.getNotify().equals(SystemConstant.NOTIFY_NO)) {
return;
}
if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) {
notificationService.sendContainerOfflineNotification(container.getId());
set.add(container.getId().toString());
}
if (container.getNotify().equals(SystemConstant.NOTIFY_EMAIL)) {
notificationService.sendContainerOfflineEmail(container.getId());
}
if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER_EMAIL)) {
notificationService.sendContainerOfflineNotificationEmail(container.getId());
set.add(container.getId().toString());
}
}
redisCache.setCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS, set);
}
});
}
} }