Ver Fonte

监控告警定时任务处理

wangmiaomiao há 8 meses atrás
pai
commit
231a8edc6e

+ 5 - 9
src/main/java/com/slibra/monitor/config/ApplicationProperties.java

@@ -26,15 +26,11 @@ public class ApplicationProperties {
         private String accessKeyId;
         private String accessKeySecret;
         private String signName;
-        private String templateCooperation;
-        private String templateCodeMessage;
-        private String templateSysRegistPassed;
-        private String templateRegistPassed;
-        private String templateRegistUnpassed;
-        private String templateAccountFreezed;
-        private String templateAccountUnfreezed;
-        private String templateAccountReset;
-        private String templateRegistBuyer;
+        private String templateDeviceErr;
+        private String templateMysqlErr;
+        private String templateRedisErr;
+        private String templateBigModelErr;
+
     }
 
 

+ 130 - 7
src/main/java/com/slibra/monitor/controller/ScheduledTasks.java

@@ -1,19 +1,142 @@
 package com.slibra.monitor.controller;
 
+import com.slibra.monitor.config.ApplicationProperties;
+import com.slibra.monitor.util.AliSMSUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.net.telnet.TelnetClient;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
+import java.util.HashMap;
+import java.util.concurrent.TimeUnit;
+
 @Component
+@Slf4j
 public class ScheduledTasks {
 
-    @Scheduled(fixedRate = 5000) // 每5秒执行一次
-    public void reportCurrentTime() {
-        System.out.println("现在时间: " + System.currentTimeMillis() / 1000);
+    @Autowired
+    private ApplicationProperties applicationProperties;
+
+//    @Scheduled(fixedRate = 5000, timeUnit = TimeUnit.HOURS) // 每小时行一次
+//    public void reportCurrentTime() {
+//        System.out.println("现在时间: " + System.currentTimeMillis() / 1000);
+//    }
+//
+//    // 使用cron表达式
+//    @Scheduled(cron = "0 0/5 15,18 * * ?") // 每天的15点和18点,每隔5分钟执行一次
+//    public void fixedRateWithInitialDelay() {
+//        System.out.println("通过cron表达式执行的定时任务: " + System.currentTimeMillis() / 1000);
+//    }
+
+
+    /**
+     *
+     * 一小时执行一次:查看服务器是否关机
+     *
+     */
+//    @Scheduled(fixedRate = 1, timeUnit = TimeUnit.HOURS)
+    @Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES)
+    public void monitorDevice() {
+        boolean error = false;
+        String server = "192.168.9.54";
+        int port = 22;
+        try {
+            TelnetClient telnetClient = new TelnetClient();
+            telnetClient.connect(server, port);
+            // 发送命令、读取响应等操作
+            telnetClient.disconnect();
+        } catch (Exception e) {
+            log.error("服务器连接失败,请检查!");
+            error = true;
+        }
+        if(error)
+            AliSMSUtil.sendMsg(applicationProperties.getAliCloud().getTemplateDeviceErr(), "15075275358", new HashMap<>());
+        else
+            log.info("服务器正常~~~");
+    }
+
+    /**
+     *
+     * 一小时执行一次:查看数据库是否可以正常访问
+     *
+     */
+//    @Scheduled(fixedRate = 1, timeUnit = TimeUnit.HOURS)
+    @Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES)
+    public void monitorMysql() {
+        boolean error = false;
+        String server = "192.168.9.54";
+        int port = 3306;
+        try {
+            TelnetClient telnetClient = new TelnetClient();
+            telnetClient.connect(server, port);
+            // 发送命令、读取响应等操作
+            telnetClient.disconnect();
+        } catch (Exception e) {
+            log.error("Mysql连接失败,请检查!");
+            error = true;
+        }
+        if(error)
+            AliSMSUtil.sendMsg(applicationProperties.getAliCloud().getTemplateMysqlErr(), "15075275358", new HashMap<>());
+        else
+            log.info("Mysql正常~~~");
+    }
+
+
+    /**
+     *
+     * 一小时执行一次:查看Redis是否可以正常访问
+     *
+     */
+//    @Scheduled(fixedRate = 1, timeUnit = TimeUnit.HOURS)
+    @Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES)
+    public void monitorRedis() {
+        boolean error = false;
+        String server = "10.0.0.24";
+        int port = 6379;
+        try {
+            TelnetClient telnetClient = new TelnetClient();
+            telnetClient.connect(server, port);
+            // 发送命令、读取响应等操作
+            telnetClient.disconnect();
+        } catch (Exception e) {
+            log.error("Redis连接失败,请检查!");
+            error = true;
+        }
+        if(error)
+            AliSMSUtil.sendMsg(applicationProperties.getAliCloud().getTemplateRedisErr(), "15075275358", new HashMap<>());
+        else
+            log.info("Redis正常~~~");
     }
 
-    // 使用cron表达式
-    @Scheduled(cron = "0 0/5 15,18 * * ?") // 每天的15点和18点,每隔5分钟执行一次
-    public void fixedRateWithInitialDelay() {
-        System.out.println("通过cron表达式执行的定时任务: " + System.currentTimeMillis() / 1000);
+
+
+    /**
+     *
+     * 一小时执行一次:查看服大模型服务是否正常
+     *
+     */
+//    @Scheduled(fixedRate = 1, timeUnit = TimeUnit.HOURS)
+    @Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES)
+    public void monitorBigModel() {
+        boolean error = false;
+        String server = "10.0.0.24";
+        int port = 17070;
+        try {
+            TelnetClient telnetClient = new TelnetClient();
+            telnetClient.connect(server, port);
+            // 发送命令、读取响应等操作
+            telnetClient.disconnect();
+        } catch (Exception e) {
+            log.error("大模型服务连接失败,请检查!");
+            error = true;
+        }
+        if(error)
+            AliSMSUtil.sendMsg(applicationProperties.getAliCloud().getTemplateBigModelErr(), "15075275358", new HashMap<>());
+        else
+            log.info("大模型服务正常~~~");
     }
+
+
+
 }

+ 5 - 9
src/main/resources/application.yml

@@ -28,13 +28,9 @@ application:
     access-key-id: LTAI5tMYjV29Hy1XKn3d79YD
     access-key-secret: 48we5KZdx9Bu2OiJ9hmXhQTf2aCut0
     sign-name: 伏羲碳惠交易平台
-    templateCooperation: SMS_264860508
-    templateCodeMessage: SMS_461080515
-    templateSysRegistPassed: SMS_461065541
-    templateRegistPassed: SMS_461025520
-    templateRegistUnpassed: SMS_460935564
-    templateAccountFreezed: SMS_264190885
-    templateAccountUnfreezed: SMS_264030916
-    templateAccountReset: SMS_264030920
-    templateRegistBuyer: SMS_462425424
+    templateDeviceErr: SMS_470285101
+    templateMysqlErr: SMS_470285102
+    templateRedisErr: SMS_470085116
+    templateBigModelErr: SMS_470245095
+