Cómo usar el código Java para leer la información de uso del disco del host Linux e interceptar el sistema de archivos y el estado de uso al mismo tiempo. Colóquelo en el mapa para obtener el valor clave.
paquete com.cmmb.util;
importar java.io.*
/**
* memoria de la CPU en Linux Monitoreo del uso de disco jvm
* @author avery_leo
*
*/
clase pública DiskSpace {
/**
* Obtener uso de CPU
* @return
* @throws Excepción
*/
public double getcpuUsage() lanza una excepción {
double cpuUsed = 0;
Tiempo de ejecución rt = Runtime.getRuntime()
Proceso p = rt .exec ("top -b -n 1"); // Llama al comando "superior" del sistema
BufferedReader in = null
try {
in =; new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null
String[] strArray = null
while (( str = in.readLine()) != null) {
int m =
if (str.indexOf(" R ") != -1) { // Analice solo los procesos en ejecución, excepto el proceso superior en síamp;amp;
strArray = str.split(" "
for (String tmp: strArray) {
<); p> if (tmp.trim().length() == 0)continue
if ( m == 9) {// Columna 9 Es el porcentaje de CPU uso (RedHat
cpuUsed = Double.parseDouble(tmp);
} <
/p>
}
}
}
} catch (Excepción e) {
e.printStackTrace();
} finalmente {
in.close();
}
return cpuUsed; /p>
/**
* Monitoreo de memoria
* @return
* @throws Excepción
*/
public double getMemUsage() lanza una excepción {
double menUsed = 0;
Runtime rt = Runtime.getRuntime(); p = rt.exec("top -b -n 1"); // Llama al comando "superior" del sistema
BufferedReader in = null
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null
String[] strArray = null; p> while ((str = in.readLine()) != null) {
int m =
if (str.indexOf(" R ") != - 1) {//Solo analiza los procesos en ejecución, excepto el proceso superior en síamp;amp;
//
// System.out.println("----- -- -----------3-----------------");
strArray = str.split(" ") ;
for (String tmp : strArray) {
if (tmp.trim().length() == 0)
continuar
si
( m == 10) {
// 9)--La décima columna es el porcentaje de uso de mem (RedHat 9)
menUsed = Double.parseDouble(tmp); /p> p>
}
}
}
}
} captura (Excepción e) {
e.printStackTrace();
} finalmente {
in.close();
}
return menUsed
p>
}
/**
* Obtener el tamaño del espacio en disco
*
* @return
* @throws Excepción
*/
public double getDeskUsage() throws Exception {
double totalhd = 0;
doble usadohd = 0;
Tiempo de ejecución rt = Runtime.getRuntime();
Proceso p = rt.exec("df -hl /home "); //df -hl Ver espacio en el disco duro
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p. getInputStream()));
String str = nulo;
String[] strArray = nulo
while ((str = in.readLine()) != nulo) {
int m = 0;
strArray = str.split(" "); for (String tmp: strArray) {
if (tmp. trim().length() == 0)
continuar
m;
System.out.println("----tmp----" tmp
if (tmp.indexOf("G") != -1); ) {
if (m == 2) {
System.out.println("---G----" tmp); if (!tmp.equals("") amp;amp; !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") amp; !tmp.equals("0"))
usedhd = Double.parseDouble(tmp.substring(
0, tmp.length() - 1)) * 1024; p> if (tmp.indexOf("M") != -1) {
if (m == 2) {
System.out.println("- -- M---" tmp);
if (!tmp.equals("") amp; !tmp.equals("0"))
totalhd = Double.parseDouble( tmp
.substring(0, tmp.length() - 1));
}
if (m == 3) {
System.out.println ("---M---" tmp);
if (!tmp.equals("none") amp; !tmp.equals("0"))
usedhd = Double.parseDouble(tmp.substring(
0, tmp.length() - 1));
System.out.println("----3 -- --" usedhd);
}
}
}
}
} catch ( Excepción e ) {
e.printStackTrace();
} finalmente {
in.close();
}
p>//El nombre de usuario y el total escritos arriba se escribieron al revés. Era demasiado vago para cambiarlos, así que los usé al revés
System.out.println("----totalhd- ---" usedhd );
System.out.println("----usedhd----" totalhd);
return (totalhd / usedhd) * 100;
}
public static void main(String[] args) lanza una excepción {
DiskSpace cpu = new DiskSpace(); System.out.println ("---------------cpu utilizado:" cpu.getcpuUsage() "");
System.out.println("-); ----- ---------mem utilizado:" cpu.getMemUsage() "");
System.out.println("--------- ---- -HD
usado: " cpu.getDeskUsage() "");
System.out.println("------------monitoreo jvm---------- ------------");
Tiempo de ejecución lRuntime = Runtime.getRuntime();
System.out.println("----- ---------Momery libre: " lRuntime.freeMemory() "K");
System.out.println("------------- -Max Momery: " lRuntime.maxMemory() "K");
System.out.println("--------------Total Momery: " lRuntime.totalMemory () "K");
System.out.println("---------------Procesadores disponibles:"
lRuntime.availableProcessors ());
}
}