Linux 端Tomcat监控程序(JAVA版)
Linux 端Tomcat监控程序(JAVA版)
1.一个判断服务是否启动的监控类
- package com.util;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.LineNumberReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.HashSet;
- import java.util.Set;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- publicclass RequestUtil {
- /**
- * 网站是否正常运转
- * @return
- */
- publicstaticboolean isWebSiteNormal(String urlStr){
- try {
- if(urlStr != null && urlStr.indexOf("://") <= 0 ){
- urlStr = "http://"+urlStr;
- }
- URL url = new URL(urlStr);
- URLConnection con = url.openConnection();
- BufferedReader in = new BufferedReader(new java.io.InputStreamReader(con
- .getInputStream()));
- con.setConnectTimeout(2000);
- con.setReadTimeout(6000);
- String s = "";
- while ((s = in.readLine()) != null) {
- if (s.length() > 0) {
- returntrue;
- }
- }
- in.close();
- } catch (Exception e) {
- LogUtil.writerE(e);
- }
- returnfalse;
- }
- /**
- * 查看Tomcat7服务是否正在运行
- * @return
- */
- publicstaticboolean isTomcatServiceAlive(){
- try {
- Process process = Runtime.getRuntime().exec("lsof -i tcp:80");
- InputStreamReader ir = new InputStreamReader(process.getInputStream());
- LineNumberReader input = new LineNumberReader(ir);
- String line;
- while ((line = input.readLine()) != null) {
- if(line != null && !"".equals(line)){
- returntrue;
- }
- }
- } catch (java.io.IOException e) {
- LogUtil.writerE(e);
- }
- returnfalse;
- }
- /**
- * 获取tomcat监听的进程
- * @return
- */
- publicstatic String getTomcatTcpIpListener(){
- StringBuffer str = new StringBuffer();
- try {
- Process process = Runtime.getRuntime().exec("lsof -i tcp:80");
- InputStreamReader ir = new InputStreamReader(process.getInputStream());
- LineNumberReader input = new LineNumberReader(ir);
- String line;
- while ((line = input.readLine()) != null) {
- if(line != null && !"".equals(line)){
- str.append(line).append("\n");
- }
- }
- } catch (java.io.IOException e) {
- LogUtil.writerE(e);
- }
- return str.toString();
- }
- /**
- * 获取tomcat监听的进程
- * @param content
- * @return
- */
- publicstatic Set<Integer> getUids(String content){
- Pattern pattern = Pattern.compile("java\\s*[0-9]*");
- Matcher matcher = pattern.matcher(content);
- Pattern pattern2 = Pattern.compile("[0-9]+");
- Set<Integer> uidSet = new HashSet<Integer>();
- while(matcher.find()){
- Matcher matcher2 = pattern2.matcher(matcher.group());
- if(matcher2.find()){
- if(matcher2.group() != null && !"".equals(matcher2.group())){
- uidSet.add(Integer.parseInt(matcher2.group()));
- }
- }
- }
- return uidSet;
- }
- publicstaticboolean isWebCanVisite(String url) {
- if(url != null && url.lastIndexOf("://") <= 0 ){
- url = "http://"+url;
- }
- LogUtil.writerINF("Check Url:"+url);
- String content = HttpRequestProxy.doGet(url,"utf-8");
- // Pattern pattern = Pattern.compile("<title>\\s*[^<>][^<>]*\\s*</title>");
- // Pattern pattern2 = Pattern.compile("[^x00-xff]*");
- if(content != null){
- returntrue;
- // Matcher matcher = pattern.matcher(content);
- // String temp = "";
- // while(matcher.find()){
- // temp = matcher.group();
- // temp = temp.trim();
- // if(temp.length() > 16){
- // temp = temp.substring(7, temp.length()-8);
- // Matcher matcher2=pattern2.matcher(temp);
- // if(matcher2.find()){
- // return true;
- // }
- // }
- // }
- }
- returnfalse;
- }
- }
2.一个Tomcat服务控制工具类
- package com.util;
- import java.util.Iterator;
- import java.util.Set;
- publicclass TomcatUtil {
- /**
- * 关闭tomcat7服务
- * @return
- */
- publicstaticboolean stopTomcat() {
- try {
- String commandstr = "/usr/local/tomcat7/bin/shutdown.sh";
- Process p;
- LogUtil.writerINF("Tomcat7 ShutDown");
- p = Runtime.getRuntime().exec(commandstr);
- if (p.waitFor() == 0){
- LogUtil.writerINF("Tomcat7 ShutDown over");
- returntrue;
- }else{
- LogUtil.writerINF("Tomcat7 ShutDown fail");
- returnfalse;
- }
- } catch (Exception e) {
- LogUtil.writerINF("Tomcat7 ShutDown fail");
- LogUtil.writerE(e);
- returnfalse;
- }
- }
- /**
- * 开启服务
- * @return
- */
- publicstaticboolean startTomcat() {
- try {
- String commandstr = "/usr/local/tomcat7/bin/startup.sh";
- Process p;
- LogUtil.writerINF("Tomcat7 startup");
- p = Runtime.getRuntime().exec(commandstr);
- if (p.waitFor() == 0){
- LogUtil.writerINF("Tomcat7 startup over");
- returntrue;
- }else{
- LogUtil.writerINF("Tomcat7 startup fail");
- returnfalse;
- }
- } catch (Exception e) {
- LogUtil.writerINF("Tomcat7 startup fail");
- LogUtil.writerE(e);
- returnfalse;
- }
- }
- /**
- * 杀掉进程
- * @param uidSet
- * @return
- */
- publicstaticint killUid(Set<Integer> uidSet){
- Iterator<Integer> it = uidSet.iterator();
- int successCount = 0 ;
- while(it.hasNext()){
- int uid = it.next();
- try {
- String commondStr = "kill -9 "+uid;
- Process p = Runtime.getRuntime().exec(commondStr);
- if (p.waitFor() == 0){
- LogUtil.writerINF("java "+uid+" be Killed");
- successCount ++;
- }else{
- LogUtil.writerINF("java "+uid+" be Kill Failed");
- }
- } catch (Exception e) {
- LogUtil.writerE(e);
- }
- }
- return successCount;
- }
- }
|
评论暂时关闭