六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

监控系统内存

[复制链接]

升级  67.67%

123

主题

123

主题

123

主题

举人

Rank: 3Rank: 3

积分
403
 楼主| 发表于 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();        }    }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表