[size=medium]这个是我写的系统采集类实现了CPU个数,CPU频率,内存信息,磁盘信息,CPU利用率,内存利用率,磁盘利用率,系统启动时间的获得
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.fenghuo.jsnmp.logic.task;
import java.io.*;
import java.util.StringTokenizer;
/**
* 用于执行linux命令
* @author huangxf
*
*/
public class LinuxExec {
/**
* 获取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");//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();
}
return (usedHD / totalHD) * 100;
}
/**
* 获得CPU的个数
* @return
* @throws java.lang.Exception
*/
public int getIntCpuNum() throws Exception {
int rs = 0;
Runtime rt = Runtime.getRuntime();
// Process p = rt.exec("cat /proc/cpuinfo | grep processor | wc -l");//获得CPU个数
Process p = rt.exec("cat /proc/cpuinfo");//获得CPU个数
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strarr = null;
while ((str = in.readLine()) != null) {
strarr = str.split(":");
if (strarr[0].trim().equals("processor")) {
rs = Integer.valueOf(strarr[1].trim())+1;
}
}
// String str = in.readLine();
// System.out.println("============================================");
// System.out.println(str);
// str = str.trim();
// System.out.println(str);
// System.out.println("============================================");
// rs = Integer.valueOf(str);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return rs;
}
/**
* 获得内存的大小
* @return
* @throws java.lang.Exception
*/
public int getIntMemMax() throws Exception {
int rs = 0;
Runtime rt = Runtime.getRuntime();
// Process p = rt.exec("cat /proc/meminfo|grep -i \"^memtotal\"|cut -d\":\" -f2|cut -d\"k\" -f1");//获得内存大小
Process p = rt.exec("cat /proc/meminfo");//获得内存大小
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
int[] result = new int[4];
String str = null;
StringTokenizer token = null;
while ((str = in.readLine()) != null) {
token = new StringTokenizer(str);
if (!token.hasMoreTokens()) {
continue;
}
str = token.nextToken();
if (!token.hasMoreTokens()) {
continue;
}
if (str.equalsIgnoreCase("MemTotal:")) {
result[0] = Integer.parseInt(token.nextToken());
rs = result[0];
} else if (str.equalsIgnoreCase("MemFree:")) {
result[1] = Integer.parseInt(token.nextToken());
} else if (str.equalsIgnoreCase("SwapTotal:")) {
result[2] = Integer.parseInt(token.nextToken());
} else if (str.equalsIgnoreCase("SwapFree:")) {
result[3] = Integer.parseInt(token.nextToken());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return rs / 1000;
}
/**
* 获得的CPUHMZ
* @return
* @throws java.lang.Exception
*/
public int getIntCpuHMZ() throws Exception {
int cpuhmz = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cat /proc/cpuinfo ");//读CPU信息文件
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strarr = null;
while ((str = in.readLine()) != null) {
strarr = str.split(":");
if (strarr.length > 0) {
if (strarr[0].trim().equals("cpu MHz")) {
cpuhmz = Double.valueOf(strarr[1].trim()).intValue();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuhmz;
}
/**
* 获取磁盘空间大小(总)
*
* @return
* @throws Exception
*/
public int getDeskAll() throws Exception {
double totalHD = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl");//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.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);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
Double d = new Double(totalHD);
return d.intValue();
}
/**
* 查询系统时间
* @return
* @throws java.io.IOException
*/
public long getSysStartTime() throws IOException, InterruptedException {
long statime = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("uptime");//查看系统启动时间
// System.out.println(p.exitValue());
// p.waitFor();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = in.readLine();
String[] strs = str.split(",");
String[] rss = strs[1].trim().split(" ");
if (rss.length == 1) {
String[] times = rss[0].split(":");
int hour = Integer.valueOf(times[0]);
int min = Integer.valueOf(times[1]);
statime = (hour * 60 + min) * 60 * 1000;
} else if (rss.length == 2) {
int min = Integer.valueOf(rss[0]);
statime = min * 60 * 1000;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return statime;
}
public void shutdownh() throws IOException {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("shutdown -h");
}
public void shutdownr() throws IOException {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("shutdown -r");
}
public static void main(String[] args) throws Exception {
LinuxExec cpu = new LinuxExec();
System.out.println("---------------cpu used:" + cpu.getCpuUsage() + "%");
System.out.println("---------------mem used:" + cpu.getMemUsage() + "%");
System.out.println("---------------HD used:" + cpu.getDeskUsage() + "%");
System.out.println("---------------CPU NUM:" + cpu.getIntCpuNum());
System.out.println("---------------CPU HMZ:" + cpu.getIntCpuHMZ());
System.out.println("---------------MEN MAX:" + cpu.getIntMemMax());
System.out.println("---------------DISK MAX:" + cpu.getDeskAll());
System.out.println("---------------STARTTIME:" + cpu.getSysStartTime());
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());
}
}[/size]
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.fenghuo.jsnmp.logic.task;
import java.io.*;
import java.util.StringTokenizer;
/**
* 用于执行linux命令
* @author huangxf
*
*/
public class LinuxExec {
/**
* 获取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");//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();
}
return (usedHD / totalHD) * 100;
}
/**
* 获得CPU的个数
* @return
* @throws java.lang.Exception
*/
public int getIntCpuNum() throws Exception {
int rs = 0;
Runtime rt = Runtime.getRuntime();
// Process p = rt.exec("cat /proc/cpuinfo | grep processor | wc -l");//获得CPU个数
Process p = rt.exec("cat /proc/cpuinfo");//获得CPU个数
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strarr = null;
while ((str = in.readLine()) != null) {
strarr = str.split(":");
if (strarr[0].trim().equals("processor")) {
rs = Integer.valueOf(strarr[1].trim())+1;
}
}
// String str = in.readLine();
// System.out.println("============================================");
// System.out.println(str);
// str = str.trim();
// System.out.println(str);
// System.out.println("============================================");
// rs = Integer.valueOf(str);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return rs;
}
/**
* 获得内存的大小
* @return
* @throws java.lang.Exception
*/
public int getIntMemMax() throws Exception {
int rs = 0;
Runtime rt = Runtime.getRuntime();
// Process p = rt.exec("cat /proc/meminfo|grep -i \"^memtotal\"|cut -d\":\" -f2|cut -d\"k\" -f1");//获得内存大小
Process p = rt.exec("cat /proc/meminfo");//获得内存大小
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
int[] result = new int[4];
String str = null;
StringTokenizer token = null;
while ((str = in.readLine()) != null) {
token = new StringTokenizer(str);
if (!token.hasMoreTokens()) {
continue;
}
str = token.nextToken();
if (!token.hasMoreTokens()) {
continue;
}
if (str.equalsIgnoreCase("MemTotal:")) {
result[0] = Integer.parseInt(token.nextToken());
rs = result[0];
} else if (str.equalsIgnoreCase("MemFree:")) {
result[1] = Integer.parseInt(token.nextToken());
} else if (str.equalsIgnoreCase("SwapTotal:")) {
result[2] = Integer.parseInt(token.nextToken());
} else if (str.equalsIgnoreCase("SwapFree:")) {
result[3] = Integer.parseInt(token.nextToken());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return rs / 1000;
}
/**
* 获得的CPUHMZ
* @return
* @throws java.lang.Exception
*/
public int getIntCpuHMZ() throws Exception {
int cpuhmz = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cat /proc/cpuinfo ");//读CPU信息文件
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strarr = null;
while ((str = in.readLine()) != null) {
strarr = str.split(":");
if (strarr.length > 0) {
if (strarr[0].trim().equals("cpu MHz")) {
cpuhmz = Double.valueOf(strarr[1].trim()).intValue();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuhmz;
}
/**
* 获取磁盘空间大小(总)
*
* @return
* @throws Exception
*/
public int getDeskAll() throws Exception {
double totalHD = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl");//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.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);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
Double d = new Double(totalHD);
return d.intValue();
}
/**
* 查询系统时间
* @return
* @throws java.io.IOException
*/
public long getSysStartTime() throws IOException, InterruptedException {
long statime = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("uptime");//查看系统启动时间
// System.out.println(p.exitValue());
// p.waitFor();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = in.readLine();
String[] strs = str.split(",");
String[] rss = strs[1].trim().split(" ");
if (rss.length == 1) {
String[] times = rss[0].split(":");
int hour = Integer.valueOf(times[0]);
int min = Integer.valueOf(times[1]);
statime = (hour * 60 + min) * 60 * 1000;
} else if (rss.length == 2) {
int min = Integer.valueOf(rss[0]);
statime = min * 60 * 1000;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return statime;
}
public void shutdownh() throws IOException {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("shutdown -h");
}
public void shutdownr() throws IOException {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("shutdown -r");
}
public static void main(String[] args) throws Exception {
LinuxExec cpu = new LinuxExec();
System.out.println("---------------cpu used:" + cpu.getCpuUsage() + "%");
System.out.println("---------------mem used:" + cpu.getMemUsage() + "%");
System.out.println("---------------HD used:" + cpu.getDeskUsage() + "%");
System.out.println("---------------CPU NUM:" + cpu.getIntCpuNum());
System.out.println("---------------CPU HMZ:" + cpu.getIntCpuHMZ());
System.out.println("---------------MEN MAX:" + cpu.getIntMemMax());
System.out.println("---------------DISK MAX:" + cpu.getDeskAll());
System.out.println("---------------STARTTIME:" + cpu.getSysStartTime());
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());
}
}[/size]