netcome 发表于 2013-1-15 02:25:40

监控系统内存

public CollectorThread(int second,String path,String pid,String name)    {      this.second = second;      this.pid = pid;      this.name = name;      String osname = System.getProperty("os.name");      System.setProperty("PERFMON_PATH",path);      if("SunOS".equals(osname))      {            os = "solaris";            command = "ps -p "+pid+" -o pid,pcpu,pmem,osz,rss";      }      if("AIX".equals(osname))      {            os = "aix";            command = "ps v "+pid;      }      if("Linux".equals(osname))      {            os = "linux";            command = "ps u "+pid;      }      if(osname.indexOf("Window")>=0)      {            os = "window";            command = "java";      }      setPriority(Thread.MIN_PRIORITY);    }public void execute()    {      try      {            Process p = rt.exec(command);            InputStream is = p.getInputStream();            InputStreamReader isr = new InputStreamReader(is);            BufferedReader br = new BufferedReader(isr);            String line = br.readLine();            line = br.readLine();            if(line == null || "".equals(line))                return;            StringTokenizer st = new StringTokenizer(line);            String ts = null;            String cpu = null;            String mem = null;            String vmem = null;            if("solaris".equals(os))            {                ts = formatDate(new Date());                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  cpu = st.nextToken();                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  vmem = st.nextToken();                if(st.hasMoreTokens())                  mem = st.nextToken();            }            if("aix".equals(os))            {                ts = formatDate(new Date());                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  vmem = st.nextToken();                if(st.hasMoreTokens())                  mem = st.nextToken();                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  cpu = st.nextToken();            }            if("linux".equals(os))            {                ts = formatDate(new Date());                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  cpu = st.nextToken();                if(st.hasMoreTokens())                  st.nextToken();                if(st.hasMoreTokens())                  vmem = st.nextToken();                if(st.hasMoreTokens())                  mem = st.nextToken();            }            if(mem != null && !"".equals(mem))                mem = ""+Long.parseLong(mem)*1024;            if(vmem != null && !"".equals(vmem))                vmem = ""+Long.parseLong(vmem)*1024;            if(name != null && !"".equals(name))            {                String file = path+"/perfmon_cpu_"+name+".tsv";                FileOutputStream fos = new FileOutputStream(file,true);                fos.write(("\""+ts+"\"\t"+"\""+cpu+"\"\r\n").getBytes());                fos.close();                file = path+"/perfmon_mem_"+name+".tsv";                fos = new FileOutputStream(file,true);                fos.write(("\""+ts+"\"\t"+"\""+mem+"\"\r\n").getBytes());                fos.close();                file = path+"/perfmon_vmem_"+name+".tsv";                fos = new FileOutputStream(file,true);                fos.write(("\""+ts+"\"\t"+"\""+vmem+"\"\r\n").getBytes());                fos.close();            }            else            {                String file = path+"/perfmon_cpu_"+pid+".tsv";                FileOutputStream fos = new FileOutputStream(file,true);                fos.write(("\""+ts+"\"\t"+"\""+cpu+"\"\r\n").getBytes());                fos.close();                file = path+"/perfmon_mem_"+pid+".tsv";                fos = new FileOutputStream(file,true);                fos.write(("\""+ts+"\"\t"+"\""+mem+"\"\r\n").getBytes());                fos.close();                file = path+"/perfmon_vmem_"+pid+".tsv";                fos = new FileOutputStream(file,true);                fos.write(("\""+ts+"\"\t"+"\""+vmem+"\"\r\n").getBytes());                fos.close();            }            is.close();            p = null;         }      catch(Exception e)      {            e.printStackTrace();      }    }
页: [1]
查看完整版本: 监控系统内存