java linux磁盘_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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值