怎么用java代码读取linux主机的磁盘使用信息,同时截取出文件系统和已使用情况 放在map中可以得到keyvalu

文件系统容量已用可用已用%挂载点/dev/sda855G4.6G47G9%/udev367M4.0K367M1%/devtmpfs150M848K149M1%/runno... 文件系统 容量 已用 可用 已用% 挂载点
/dev/sda8 55G 4.6G 47G 9% /
udev 367M 4.0K 367M 1% /dev
tmpfs 150M 848K 149M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 375M 624K 374M 1% /run/shm
none 100M 40K 100M 1% /run/user
展开
 我来答
山水阿锐
2015-07-10 · TA获得超过34.3万个赞
知道顶级答主
回答量:23.7万
采纳率:91%
帮助的人:3.2亿
展开全部
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());
}
}
快又稳
2024-10-28 广告
在Linux环境下配置基于域名的虚拟主机,需安装Apache或Nginx等Web服务器,并编辑配置文件。以Apache为例,需创建虚拟主机配置文件,指定域名、文档根目录等,然后启用该配置文件并重启Apache服务。同样,Nginx也需在相应... 点击进入详情页
本回答由快又稳提供
圣鸾OJ
推荐于2016-02-04 · TA获得超过1629个赞
知道小有建树答主
回答量:1136
采纳率:96%
帮助的人:113万
展开全部
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());
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
龙城一木
推荐于2016-12-03
知道答主
回答量:57
采纳率:0%
帮助的人:25万
展开全部
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());
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式