|
|
|
|
@ -0,0 +1,150 @@
|
|
|
|
|
package com.nmg.gs.paramtomcatmonitor;
|
|
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
import java.net.Socket;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.nio.file.DirectoryIteratorException;
|
|
|
|
|
import java.nio.file.DirectoryStream;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.nio.file.attribute.FileTime;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.logging.FileHandler;
|
|
|
|
|
import java.util.logging.Level;
|
|
|
|
|
import java.util.logging.LogRecord;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
import java.util.logging.SimpleFormatter;
|
|
|
|
|
/**
|
|
|
|
|
* @author: shuguang
|
|
|
|
|
* @date: 2024年12月19日 10:25
|
|
|
|
|
* @description:
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
public class TomcatMonitor {
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = Logger.getLogger(TomcatMonitor.class.getName());
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void init() {
|
|
|
|
|
try {
|
|
|
|
|
// 创建logs目录
|
|
|
|
|
Path logsDir = Paths.get("logs");
|
|
|
|
|
if (!Files.exists(logsDir)) {
|
|
|
|
|
Files.createDirectory(logsDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置日志级别为 INFO
|
|
|
|
|
logger.setLevel(Level.ALL);
|
|
|
|
|
|
|
|
|
|
// 创建文件处理器
|
|
|
|
|
FileHandler fileHandler = new FileHandler("logs/tomcat_monitor.log", true);
|
|
|
|
|
fileHandler.setLevel(Level.ALL);
|
|
|
|
|
|
|
|
|
|
// 设置日志格式,包含时间戳
|
|
|
|
|
fileHandler.setFormatter(new SimpleFormatter() {
|
|
|
|
|
@Override
|
|
|
|
|
public synchronized String format(LogRecord lr) {
|
|
|
|
|
return String.format("%1$tF %1$tT | %2$s | %3$s%n", lr.getMillis(), lr.getLevel(), lr.getMessage());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 将处理器添加到日志器
|
|
|
|
|
logger.addHandler(fileHandler);
|
|
|
|
|
// 启动监控线程
|
|
|
|
|
new Thread(this::run).start();
|
|
|
|
|
// 清理旧日志文件
|
|
|
|
|
cleanOldLogFiles();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.log(Level.SEVERE, "Failed to initialize file handler", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void run() {
|
|
|
|
|
logger.info("Tomcat监控程序启动");
|
|
|
|
|
while (true) {
|
|
|
|
|
//if (!isTomcatRunning()) {
|
|
|
|
|
if (!testIpAndPort("127.0.0.1",9440,3000)) {
|
|
|
|
|
logger.warning("Tomcat未运行,尝试启动Tomcat...");
|
|
|
|
|
startTomcat();
|
|
|
|
|
} else {
|
|
|
|
|
logger.info("Tomcat正在运行");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
// 每隔一段时间检查一次,这里设置为300秒
|
|
|
|
|
Thread.sleep(300000);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
logger.log(Level.SEVERE, "线程中断", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static boolean testIpAndPort(String host, int port, int timeout) {
|
|
|
|
|
Socket s = new Socket();
|
|
|
|
|
boolean status = false;
|
|
|
|
|
try {
|
|
|
|
|
s.connect(new InetSocketAddress(host, port), timeout);
|
|
|
|
|
status = true;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.out.println(host + ":" + port + "无法访问!");
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
s.close();
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
System.out.println("关闭socket异常" + ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void startTomcat() {
|
|
|
|
|
try {
|
|
|
|
|
ProcessBuilder processBuilder = new ProcessBuilder(
|
|
|
|
|
"/bin/bash", "-c",
|
|
|
|
|
"sudo ldconfig; source /etc/profile; cd /usr/sg/tomcat9/apache-tomcat-9.0.73/bin/; sh startup.sh"
|
|
|
|
|
);
|
|
|
|
|
processBuilder.redirectErrorStream(true);
|
|
|
|
|
Process process = processBuilder.start();
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
logger.info(line);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.log(Level.SEVERE, "启动Tomcat失败", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private static void cleanOldLogFiles() {
|
|
|
|
|
Path logsDir = Paths.get("logs");
|
|
|
|
|
try (DirectoryStream<Path> stream = Files.newDirectoryStream(logsDir)) {
|
|
|
|
|
for (Path entry : stream) {
|
|
|
|
|
if (Files.isRegularFile(entry)) {
|
|
|
|
|
try {
|
|
|
|
|
// 获取文件的创建时间
|
|
|
|
|
FileTime creationTime = Files.getLastModifiedTime(entry);
|
|
|
|
|
// 计算时间差
|
|
|
|
|
long daysOld = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - creationTime.toMillis());
|
|
|
|
|
if (daysOld > 10) {
|
|
|
|
|
// 删除超过10天的文件
|
|
|
|
|
Files.delete(entry);
|
|
|
|
|
logger.info("已删除旧日志文件: " + entry.getFileName());
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.log(Level.SEVERE, "删除旧日志文件时出错: " + entry.getFileName(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException | DirectoryIteratorException e) {
|
|
|
|
|
logger.log(Level.SEVERE, "清理旧日志文件时出错", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|