登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

学无止境

一点积累,与大家分享

 
 
 

日志

 
 

java 监控Windows平台下 Tomcat 实例  

2010-12-22 16:39:17|  分类: java技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
iimport java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.Properties;


public class Listener {
   
    private java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private  void keepTomcatAlive(int port,int threadi) throws NullPointerException {
        Properties prop = new Properties();
        try {
            prop.load(getClass().getResourceAsStream("port.properties"));
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //增加一个启动检测功能
        boolean isAlive = false ;
       
        int msize = 500000;
        String smsize = prop.get(port+".msize")==null?"":(String)prop.get(port+".msize");
        msize = Integer.parseInt(smsize);
        String command =  prop.get(port+".tomcat.startup")==null?"":(String)prop.get(port+".tomcat.startup");
        String s;
        String t = new String("java.exe");
        boolean isTomcatAlive = false;
       
        java.io.BufferedReader in;
       
        int pid  =-1;
        String memsize = null ;
        try {
            java.lang.Process p = java.lang.Runtime.getRuntime().exec("tasklist -fo list");

            in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));

            while ((s = in.readLine()) != null) {
                 
                if (s.indexOf(t)>0) {
                    s = in.readLine().toLowerCase();
                    if(s.indexOf("pid")>-1)
                    {
                        String spid = s.substring("pid:".length());
                        spid = spid.trim() ;
                        spid = spid.replace(",", "");
                        pid = Integer.parseInt(spid) ;
                        //System.out.println(s+"   "+pid);
                    }
                    in.readLine() ;
                    in.readLine();
                    s = in.readLine();
                    if(s.indexOf("内存使用 :")>-1)
                    {
                        memsize = s.substring("内存使用 :".length(),s.length()-1);
                        memsize = memsize.replace(",", "");
                        //System.out.println(s+"   ");
                    }
                    int _tmpsize = Integer.parseInt(memsize.trim());
                    boolean flag = isTaskPort(pid,port) ;
                    if(flag){
                        if(_tmpsize>msize)
                        {
                            System.out.println("====="+sdf.format(new Date())+"执行当前第"+(threadi+1)+"个Tomcat "+port+" 端口监听========");
                           
                            System.out.println("当前内存:"+_tmpsize+" 大于设定内存: "+msize+" pid:"+pid+" "+(flag?"是":"非")+" 待监控的java进程");
                            //判断是否要
                            isTomcatAlive = true;
                                 
                            break;
                             
                        } 
                        isAlive = true ;
                         
                    }
                   
                   
                }
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
       
        if (isTomcatAlive) {
            System.out.println("重启 Tomcat pid:"+pid+"  port:"+port);
            System.out.println("<" + sdf.format(new Date()) + "> Tomcat is alive but not response!");
            stopTomcat(pid);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            startTomcat(command);
            System.out.println("=================================================================");
          
        }else
        {
            if(!isAlive) //没有启动
            {
                System.out.println("<" + sdf.format(new Date()) + "> Tomcat is not alive !");               
                startTomcat(command);   
                System.out.println("=================================================================");
            }
        }
       
    }
   
    public static boolean isTaskPort(int pid ,int port)
    {
        String s ="";
        boolean flag = false;
        java.io.BufferedReader in;
        try {
            java.lang.Process p = java.lang.Runtime.getRuntime().exec("netstat -ano");

            in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
            while ((s = in.readLine()) != null) {

                if (s.indexOf(":"+String.valueOf(port)+" ")>0&&s.indexOf(String.valueOf(pid))>0) {
                   
                      flag =  true ;
                }
            }
            in.close();
            return flag ;
        } catch (Exception e) {
            e.printStackTrace();
            return flag ;
        }
    }

    public  void stopTomcat(int pid) {
        try {
            java.lang.Process p = java.lang.Runtime.getRuntime().exec("taskkill -pid "+pid+" -f");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
            String s;
            //成功: 已终止 PID 为 1592 的进程。
            String t = "成功";
            boolean restart = false;
            while ((s = in.readLine()) != null) {
                if (s.indexOf(t) != -1) {
                    restart = true;
                    break;
                }
            }
            System.out.println("<" + sdf.format(new Date()) + "> Tomcat is stop " + (restart ? "OK" : "ERROR"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public  void startTomcat(String  command) {
 
        try {
            //String command = "cmd.exe /C start /min C:\\apache-6.0.29-houtai\\bin\\run.bat";
            java.lang.Process p = java.lang.Runtime.getRuntime().exec(command);
            boolean restart = true ;
 
            System.out.println("<" + new Date() + "> Tomcat is start " + (restart ? "OK" : "ERROR"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Listener listener = new Listener();
        //启动 java Listener 8080 80
        while (true) {
            try {
                for(int i = 0 ;i<args.length ;i++)
                {
                    listener.keepTomcatAlive(Integer.parseInt(args[i]),i);
                    Thread.sleep(2000);
                }
                Thread.sleep(10000);
            } catch (Exception ex) {

            }
        }
    }
}


port.properties 内容格式范例
8080.tomcat.startup=cmd.exe /C start /min C:\\apache-6.0.29-houtai\\bin\\run.bat
8080.msize=120000
7080.tomcat.startup=cmd.exe /C start /min C:\\a-tomcat\\bin\\run.bat
7080.msize=2000

tomcat\bin 目录下run.bat范例
@echo off
set CATALINA_HOME=E:\\servers\\tomcat
E:\\servers\\tomcat\\bin\\startup.bat
  评论这张
 
阅读(1054)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018