前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >手写tomcat监控工具---宕机重启 原

手写tomcat监控工具---宕机重启 原

作者头像
wuweixiang
发布2018-08-14 14:26:07
1K0
发布2018-08-14 14:26:07
举报
文章被收录于专栏:吴伟祥吴伟祥

使用前提: 1、打成jar包,并在控制台运行:java -jar TomcatMonitorUtil.jar 2、需要配置tomcat环境变量 

#TOMCAT启动路径
startup.bat=E:/wwx/apache-tomcat-9.0.7/bin/startup.bat
#TOMCAT关闭路径
shutdown.bat=E:/wwx/apache-tomcat-9.0.7/bin/shutdown.bat





#测试连接总次数
testTotalCount=3
#测试连接间隔时间,单位为秒
testIntervalTime=3




#端口80 项目名zagame
zaGameHttp=http://127.0.0.1/zagame/userLoginByTel.json
#端口80 项目名zagame-server
zaGameServerHttp=http://127.0.0.1/games/startServer
stopZaGameServerHttp=http://127.0.0.1/games/stopServer




#日志文件目录
logDir=E:/wwx/data/eventlogs/games
#限制LOG文件大小防止溢出
logSize=10
import java.io.*;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ProjectLaunchUtil
 *
 * @author weixiang.wu
 * @date 2018 -05-04 17:08
 */
public class TomcatMonitorUtil {
	private static String startupBat;
	private static String shutdownBat;
	private static int testIntervalTime;
	private static int testTotalCount;
	private static String zaGameHttp;
	private static String zaGameServerHttp;
	private static String logDir;
	private static Long logSize;
	private static String stopZaGameServerHttp;
	private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	static {
		InputStream in = TomcatMonitorUtil.class.getResourceAsStream("config.properties");
		Properties p = new Properties();
		try {
			p.load(in);
			startupBat = p.getProperty("startup.bat");
			shutdownBat = p.getProperty("shutdown.bat");
			testIntervalTime = Integer.parseInt(p.getProperty("testIntervalTime"));
			testTotalCount = Integer.parseInt(p.getProperty("testTotalCount"));
			zaGameHttp = p.getProperty("zaGameHttp");
			zaGameServerHttp = p.getProperty("zaGameServerHttp");
			logDir = p.getProperty("logDir");
			stopZaGameServerHttp = p.getProperty("stopZaGameServerHttp");
			logSize = Long.valueOf(p.getProperty("logSize"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private TomcatMonitorUtil() {
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
				fixedThreadPool.execute(new Runnable() {

					@Override
					public void run() {
						System.err.println("正在监控中...");
						while (true) {
							int testCount = 0;
							// 测试tomcat是否需要重新启动
							boolean isRun = ping();
							while (!isRun) {
								testCount++;
								System.err.println("尝试连接第 [" + testCount + "] 次失败");
								if (testCount >= testTotalCount) {
									break;
								}
								isRun = ping();
							}

							if (!isRun) {
								try {
									//关闭tomcat服务
									Runtime.getRuntime().exec(shutdownBat);

									System.out.println(df.format(new Date()) + ":关闭tomcat服务,稍等5秒钟");
									Thread.sleep(5000);
									//启动tomcat服务
									System.out.println(df.format(new Date()) + ":测试连接失败,正在重启tomcat");
									Process process = Runtime.getRuntime().exec(startupBat);
									final InputStream in = process.getInputStream();
									BufferedReader br = new BufferedReader(new InputStreamReader(in));
									StringBuilder buf = new StringBuilder();
									String line = null;
									while ((line = br.readLine()) != null) {
										buf.append(line);
									}
									System.out.println("输出结果为:" + buf);
									System.out.println(df.format(new Date()) + ":重启tomcat成功");
									String stop = pingHttpGet(stopZaGameServerHttp, "POST");
									if (stop == null || pingHttpGet(stopZaGameServerHttp, "POST").contains("关闭")) {
										System.out.println(df.format(new Date()) + ":关闭SOCKET成功");
									}
									String start = pingHttpGet(zaGameServerHttp, "GET");
									if (start!=null||start.contains("成功")) {
										System.out.println(df.format(new Date()) + ":重启SOCKET成功");
									}else {
										System.out.println(df.format(new Date()) + ":重启SOCKET发生异常");
									}
								} catch (Exception e) {
									System.out.println("重启tomcat异常,请查看错误信息。。。。。");
									System.out.println(e);

								}
							}

							try {
								System.err.println(df.format(new Date()) + "\t通讯正常");
								Thread.sleep(testIntervalTime * 1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				});
			}
			// 执行任务前的延迟时间    执行周期
		}, 1000, Integer.MAX_VALUE);
	}

	private boolean ping() {
		/* 测试games项目是否连接正常 */
		String zaserver = pingHttpGet(zaGameServerHttp, "GET");
		if (zaserver == null || !"成功".contains(zaserver)) {
			return Boolean.FALSE;
		}
		System.err.println("一、测试games.zhuoan.servers.ui项目连接正常");

		/* 测试zagame项目是否连接正常 */
		String zagameMes = pingHttpGet(zaGameHttp, "GET");
		if (zagameMes == null) {
			return Boolean.FALSE;
		}
		System.err.println("二、测试zagame项目连接正常");

		// 扫描日志目录大小,并再一次限制文件总大小
		System.err.println("三、扫描日志目录大小,并再一次限制文件总大小");
		readfiles(logDir);

		return Boolean.TRUE;
	}

	private void readfiles(String logDir) {
		File directory = new File(logDir);
		File[] files = directory.listFiles();
		if (files == null) {
			return;
		}
		Long totalSize = 0L;
		// 如果日志文件大小总和 > 11G
		boolean flag = getLongSize(files, totalSize);
		if (flag) {
			for (int i = files.length; i > 0; i--) {
				if (files[i].getName().contains("application")) {
					//删除application文件
					boolean delete = files[i].delete();
					if (!delete) {
						continue;
					}
					flag = getLongSize(files, totalSize);
					if (!flag) {
						return;
					}
				}
			}
		}
	}

	private boolean getLongSize(File[] files, Long totalSize) {
		for (int i = 0; i < files.length; i++) {
			if (files[i].getName().contains("application")) {
				totalSize += files[i].length();
			}
		}
		Long size = totalSize / (1024 * 1024 * 1024);
		System.err.println("\tapplication日志共" + size + "Gib");
		return size > logSize;
	}

	private String pingHttpGet(String httpURL, String method) {
		HttpURLConnection connection = null;
		InputStream is;
		BufferedReader br;
		// 返回结果字符串
		String result = null;
		try {
			// 创建远程url连接对象
			URL url = new URL(httpURL);
			// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
			connection = (HttpURLConnection) url.openConnection();
			// 设置连接方式:get
			connection.setRequestMethod(method);
			// 设置连接主机服务器的超时时间:15000毫秒
			connection.setConnectTimeout(15000);
			// 设置读取远程返回的数据时间:60000毫秒
			connection.setReadTimeout(60000);
			// 发送请求
			connection.connect();
			// 通过connection连接,获取输入流
			if (connection.getResponseCode() == 200) {
				is = connection.getInputStream();
				// 封装输入流is,并指定字符集
				br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
				// 存放数据
				StringBuffer sbf = new StringBuffer();
				String temp;
				while ((temp = br.readLine()) != null) {
					sbf.append(temp);
					sbf.append("\r\n");
				}
				result = sbf.toString();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			assert connection != null;
			connection.disconnect();// 关闭远程连接
		}
		return result;
	}


	/**
	 * The entry point of application.
	 *
	 * @param args the input arguments
	 */
	public static void main(String[] args) throws FileNotFoundException {
		File file = new File(logDir);
		if (!file.exists() && !file.isDirectory()) {
			System.err.println("请确保项目先启动,即文件目录生成");
		}
		PrintStream out = new PrintStream(logDir + "/TomcatMonitor.log");
		System.setOut(out);
		/* 通过args搞事 */
		if (doSomthing(args)) {
			return;
		}
		/* 启动TOMCAT监控 */
		new TomcatMonitorUtil();
	}

	private static boolean doSomthing(String[] args) {
		try {
			switch (args[0]) {
				/* 关闭tomcat的参数 */
				case "close":
					Runtime.getRuntime().exec(shutdownBat);
					System.out.println(df.format(new Date()) + ": 手动关闭Tomcat");
					break;
				default:
					break;
			}
			return Boolean.TRUE;
		} catch (Exception e) {
			System.err.println("您可以通过发送参数来进行某些操作");
		}
		return Boolean.FALSE;
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档