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);
}
}
}