java獲取linux的磁盤空間,磁盤利用率

package com.hotpot.boos.interaction.ftpUtil.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;

/**
 * 採集磁盤IO使用率
 */
public class IoUsage{

    private static Logger log = Logger.getLogger(IoUsage.class);
    private static IoUsage INSTANCE = new IoUsage();
    
    private IoUsage(){
    
    }
    
    public static IoUsage getInstance(){
        return INSTANCE;
    }
    
    /**
     * @Purpose:採集磁盤IO使用率
     * @param args
     * @return float,磁盤IO使用率,小於1
     * @throws Exception 
     */
    public float get() throws Exception {
        log.info("開始收集磁盤IO使用率");
        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 (float) ((usedhd/totalhd) * 100);
    }

    /**
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws Exception {
        while(true){
            System.out.println(IoUsage.getInstance().get());
            Thread.sleep(5000);
        }
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章