Java获取系统信息(cpu,内存,硬盘,进程等)的相关方法

package com.cmmb.util;  
  
import java.io.*;  
  
  
/**   
 * linux 下cpu 内存 磁盘 jvm的使用监控   
 * @author avery_leo   
 *   
 */    
public class DiskSpace {  
        /**   
         * 获取cpu使用情况   
         * @return   
         * @throws Exception   
         */    
        public double getCpuUsage() throws Exception {     
            double cpuUsed = 0;     
        
            Runtime rt = Runtime.getRuntime();     
            Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令     
             
        
            BufferedReader in = null;     
            try {     
                in = new BufferedReader(new InputStreamReader(p.getInputStream()));     
                String str = null;     
                String[] strArray = null;     
                 
                while ((str = in.readLine()) != null) {     
                    int m = 0;     
                         
                    if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&     
                             
                    strArray = str.split(" ");     
                        for (String tmp : strArray) {     
                            if (tmp.trim().length() == 0)     
                                continue;     
                            if (++m == 9) {// 第9列为CPU的使用百分比(RedHat     
                                     
                                cpuUsed += Double.parseDouble(tmp);     
        
                            }     
        
                        }     
        
                    }     
                }     
            } catch (Exception e) {     
                e.printStackTrace();     
            } finally {     
                in.close();     
            }     
            return cpuUsed;     
        }     
    /**   
     * 内存监控   
     * @return   
     * @throws Exception   
     */    
        public double getMemUsage() throws Exception {     
        
            double menUsed = 0;     
            Runtime rt = Runtime.getRuntime();     
            Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令     
             
        
            BufferedReader in = null;     
            try {     
                in = new BufferedReader(new InputStreamReader(p.getInputStream()));     
                String str = null;     
                String[] strArray = null;     
                 
                while ((str = in.readLine()) != null) {     
                    int m = 0;     
                         
                    if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&     
                        //      
                        // System.out.println("------------------3-----------------");     
                        strArray = str.split(" ");     
                        for (String tmp : strArray) {     
                            if (tmp.trim().length() == 0)     
                                continue;     
        
                            if (++m == 10) {     
                                // 9)--第10列为mem的使用百分比(RedHat 9)     
        
                                menUsed += Double.parseDouble(tmp);     
        
                            }     
                        }     
        
                    }     
                }     
            } catch (Exception e) {     
                e.printStackTrace();     
            } finally {     
                in.close();     
            }     
            return menUsed;     
        }     
        
        /**  
         * 获取磁盘空间大小  
         *  
         * @return  
         * @throws Exception  
         */   
        public double getDeskUsage() throws Exception {   
            double totalHD = 0;   
            double usedHD = 0;   
            Runtime rt = Runtime.getRuntime();   
            Process p = rt.exec("df -hl /home");//df -hl 查看硬盘空间   
  
             
        
            BufferedReader in = null;   
            try {   
                in = new BufferedReader(new InputStreamReader(p.getInputStream()));   
                String str = null;   
                String[] strArray = null;   
                while ((str = in.readLine()) != null) {   
                    int m = 0;   
                        strArray = str.split(" ");   
                        for (String tmp : strArray) {   
                            if (tmp.trim().length() == 0)   
                                continue;   
                            ++m;   
                          System.out.println("----tmp----" + tmp);   
                            if (tmp.indexOf("G") != -1) {   
                                if (m == 2) {   
                                  System.out.println("---G----" + tmp);   
                                    if (!tmp.equals("") && !tmp.equals("0"))   
                                        totalHD += Double.parseDouble(tmp   
                                                .substring(0, tmp.length() - 1)) * 1024;   
        
                                }   
                                if (m == 3) {   
                                  System.out.println("---G----" + tmp);   
                                    if (!tmp.equals("none") && !tmp.equals("0"))   
                                        usedHD += Double.parseDouble(tmp.substring(   
                                                0, tmp.length() - 1)) * 1024;   
        
                                }   
                            }   
                            if (tmp.indexOf("M") != -1) {   
                                if (m == 2) {   
                                  System.out.println("---M---" + tmp);   
                                    if (!tmp.equals("") && !tmp.equals("0"))   
                                        totalHD += Double.parseDouble(tmp   
                                                .substring(0, tmp.length() - 1));   
        
                                }   
                                if (m == 3) {   
                                  System.out.println("---M---" + tmp);   
                                    if (!tmp.equals("none") && !tmp.equals("0"))   
                                        usedHD += Double.parseDouble(tmp.substring(   
                                                0, tmp.length() - 1));   
                                     System.out.println("----3----" + usedHD);   
                                }   
                            }   
                                 
                        }   
        
                  }   
            } catch (Exception e) {   
                e.printStackTrace();   
            } finally {   
                in.close();   
            }   
                 //上面写在userd和total写反了,懒得改了,就反着用了  
            System.out.println("----totalHD----" + usedHD);  
            System.out.println("----usedHD----" + totalHD);  
            return (totalHD / usedHD) * 100;   
        }   
   
        
        public static void main(String[] args) throws Exception {     
            DiskSpace cpu = new DiskSpace();     
            System.out.println("---------------cpu used:" + cpu.getCpuUsage() + "%");     
            System.out.println("---------------mem used:" + cpu.getMemUsage() + "%");     
            System.out.println("---------------HD used:" + cpu.getDeskUsage() + "%");     
            System.out.println("------------jvm监控----------------------");     
            Runtime lRuntime = Runtime.getRuntime();     
            System.out.println("--------------Free Momery:" + lRuntime.freeMemory()+"K");     
            System.out.println("--------------Max Momery:" + lRuntime.maxMemory()+"K");     
            System.out.println("--------------Total Momery:" + lRuntime.totalMemory()+"K");     
            System.out.println("---------------Available Processors :"    
                    + lRuntime.availableProcessors());     
        }   
}  

 

来源:http://dongisland.iteye.com/blog/1118788

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值