|
@@ -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("大模型服务正常~~~");
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|