重写实例状态检测方法

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.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.concurrent.*;
/**
@@ -50,6 +48,9 @@ public class CheckInstancesStatus {
@Resource
private RedisCache redisCache;
@Resource
private RedisTemplate redisTemplate;
/**
* 检查实例状态并且发送对应的提醒
*/
@@ -96,7 +97,9 @@ public class CheckInstancesStatus {
container.setContainerState(SystemConstant.CONTAINER_STATE_EXITED);
// 判断 Redis 里面有没有,如果有就不需要提醒,如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_CONTAINER_IDS);
if (!CollectionUtils.isEmpty(set)) {
if (CollectionUtils.isEmpty(set)) {
set = new HashSet<>();
}
// 如果 Redis 里面有,说明已经发送过了未 check 的通知,所以不需要发送,直接返回
if (set.contains(container.getId().toString())) {
return container;
@@ -104,13 +107,19 @@ public class CheckInstancesStatus {
// 根据实例对应的提醒方式进行提醒
if (container.getNotify().equals(SystemConstant.NOTIFY_NO)) {
return container;
} else {
}
if (container.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) {
notificationService.sendContainerOfflineNotification(container.getId());
}
}
}
// 存入 Redis
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);
}
return container;
@@ -145,7 +154,7 @@ public class CheckInstancesStatus {
}
//通过Ping的方式判断是否在线
if (StringUtils.hasText(vm.getServerAddress())) {
if (UrlUtil.isHostOnline(vm.getServerAddress())){
if (UrlUtil.isHostOnline(vm.getServerAddress())) {
log.info("[实例状态检测]:与{}建立连接成功", vm.getName());
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
return;
@@ -160,7 +169,9 @@ public class CheckInstancesStatus {
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_OFFLINE);
//判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_VM_IDS);
if (!CollectionUtils.isEmpty(set)) {
if (CollectionUtils.isEmpty(set)) {
set = new HashSet<>();
}
//如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回
if (set.contains(vm.getId().toString())) {
return;
@@ -168,13 +179,19 @@ public class CheckInstancesStatus {
//根据实例对应的提醒方式进行提醒
if (vm.getNotify().equals(SystemConstant.NOTIFY_NO)) {
return;
} else {
notificationService.sendVmOfflineNotification(vm.getId());
}
}
}
//存入redis
if (vm.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) {
notificationService.sendContainerOfflineNotification(vm.getId());
set.add(vm.getId().toString());
}
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());
}
}
redisCache.setCacheSet(RedisConstant.NOTIFY_VM_IDS, set);
}
});
@@ -200,7 +217,9 @@ public class CheckInstancesStatus {
}
//判断Redis里面有没有 如果有就不需要提醒 如果没有就提醒
Set<String> set = redisCache.getCacheSet(RedisConstant.NOTIFY_HOST_IDS);
if (!CollectionUtils.isEmpty(set)) {
if (CollectionUtils.isEmpty(set)) {
set = new HashSet<>();
}
//如果redis里面有 说明已经发送过了未check的通知 所以不需要发送 直接返回
if (set.contains(hostMachine.getId().toString())) {
return;
@@ -208,13 +227,19 @@ public class CheckInstancesStatus {
//根据实例对应的提醒方式进行提醒
if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_NO)) {
return;
} else {
notificationService.sendHostOfflineNotification(hostMachine.getId());
}
}
}
//存入redis
if (hostMachine.getNotify().equals(SystemConstant.NOTIFY_BROWSER)) {
notificationService.sendContainerOfflineNotification(hostMachine.getId());
set.add(hostMachine.getId().toString());
}
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());
}
}
redisCache.setCacheSet(RedisConstant.NOTIFY_HOST_IDS, set);
}
});
@@ -17,18 +17,54 @@ public interface NotificationService extends IService<Notification> {
*/
void sendContainerOfflineNotification(Long id);
/**
* 发送容器掉线通知和邮件
* @param id
*/
void sendContainerOfflineNotificationEmail(Long id);
/**
* 发送容器掉线邮件
* @param id
*/
void sendContainerOfflineEmail(Long id);
/**
* 发送虚拟机掉线通知
* @param id
*/
void sendVmOfflineNotification(Long id);
/**
* 发送虚拟机掉线邮件
* @param id
*/
void sendVmOfflineEmail(Long id);
/**
* 发送虚拟机掉线通知和邮件
* @param id
*/
void sendVmOfflineNotificationEmail(Long id);
/**
* 发送物理机掉线通知
* @param 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
private RedisTemplate redisTemplate;
@Override
public void sendContainerOfflineNotification(Long id) {
Notification notification = new Notification();
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("容器掉线")
.setType(SystemConstant.OFFLINE_NOTIFICATION)
.setSendType(container.getNotify())
@@ -75,7 +70,14 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
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);
String time = LocalDateTime.now().toString();
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) {
Notification notification = new Notification();
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("虚拟机掉线")
.setType(SystemConstant.OFFLINE_NOTIFICATION)
.setSendType(vm.getNotify())
@@ -104,7 +100,8 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
log.info("[掉线提醒]:发送浏览器提醒:{}", content);
}
private void sendVmOfflineEmail(Long id) {
@Override
public void sendVmOfflineEmail(Long id) {
HostMachine vm = hostMachineService.getById(id);
String time = LocalDateTime.now().toString();
String content = String.format("发现虚拟机[ %s ]掉线---- %s", vm.getName(), time);
@@ -112,16 +109,16 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
log.info("[掉线提醒]:发送邮件提醒:{}", content);
}
@Override
public void sendVmOfflineNotificationEmail(Long id) {
sendVmOfflineNotification(id);
sendVmOfflineEmail(id);
}
@Override
public void sendHostOfflineNotification(Long id) {
Notification notification = new Notification();
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("物理机掉线")
.setType(SystemConstant.OFFLINE_NOTIFICATION)
.setSendType(host.getNotify())
@@ -133,6 +130,21 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
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
public ResponseResult getNotificationList(Integer tab, Integer perPage, Integer currentPage) {
LambdaQueryWrapper<Notification> notificationWrapper = new LambdaQueryWrapper<>();
@@ -219,13 +231,6 @@ public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Not
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 {
Socket socket = new Socket(serverName, port);
log.info("[实例状态检测]" + serverName + "服务已经开启");
log.info("[实例状态检测]" + serverName + ":" + port + "服务已经开启");
socket.close();
return true;
}
+9 -4
View File
@@ -3,7 +3,7 @@ server:
spring:
#MySQL配置
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
password: rootroot
driver-class-name: com.mysql.cj.jdbc.Driver
@@ -14,7 +14,7 @@ spring:
#Redis配置
redis:
host: 192.168.1.103
database: 3
database: 2
#RabbitMq配置
rabbitmq:
host: 192.168.1.103
@@ -38,10 +38,11 @@ management:
health:
show-details: ALWAYS
mybatis-plus:
#打印SQL日志 建议生产环境关掉 否则日志爆炸
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
logic-delete-field: delFlag
@@ -53,3 +54,7 @@ pagehelper:
reasonable: true
support-methods-arguments: true
page-size-zero: true
logging:
level:
com.mzaxd.noodles: info
@@ -501,7 +501,7 @@ class NoodlesApplicationTests {
}
//通过Ping的方式判断是否在线
if (StringUtils.hasText(vm.getServerAddress())) {
if (UrlUtil.isHostOnline(vm.getServerAddress())){
if (UrlUtil.isHostOnline(vm.getServerAddress())) {
log.info("[实例状态检测]:与{}建立连接成功", vm.getName());
vm.setHostMachineState(SystemConstant.HOST_MACHINE_STATE_ONLINE);
return;
@@ -629,4 +629,59 @@ class NoodlesApplicationTests {
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);
}
});
}
}