用Java获得CPU,内存使用率

IMonitorService接口:
public interface IMonitorService {
public MonitorInfoBean getMonitorInfoBean() throws Exception;
}
MonitorInfoBean类:
/*采集系统存取数据JavaBean*/
public class MonitorInfoBean implements Comparable<MonitorInfoBean> {
/** 操作系统. */
private String osName;
/** 总的物理内存. */
private float totalMemorySize;
/** 已使用的物理内存. */
private float usedMemory;
/** cpu使用率. */
private double cpuRatio;
/** 主机IP地址 */
private String mIpAddress;
/** 数据存储时间 */
private String dDateTime;
/**内存使用率*/
private float memoryRatio;
/**linux下Buffers内存*/
private float buffersMemory;
/**linux下Cached内存*/
private float cachedMemory;

public float getBuffersMemory() {
return buffersMemory;
}
public float getCachedMemory() {
return cachedMemory;
}
public String getDDateTime() {
return dDateTime;
}
public void setDDateTime(String dateTime) {
dDateTime = dateTime;
}
public String getMIpAddress() {
return mIpAddress;
}
public void setMIpAddress(String ipAddress) {
mIpAddress = ipAddress;
}
public String getOsName() {
return osName;
}
public void setOsName(String osName) {
this.osName = osName;
}
public float getTotalMemorySize() {
return totalMemorySize;
}
public void setTotalMemorySize(float totalMemorySize) {
this.totalMemorySize = totalMemorySize;
}
public float getUsedMemory() {
return usedMemory;
}
public void setUsedMemory(long usedMemory) {
this.usedMemory = usedMemory;
}
public double getCpuRatio() {
return cpuRatio;
}
public void setCpuRatio(double cpuRatio) {
this.cpuRatio = cpuRatio;
}
public int compareTo(MonitorInfoBean m) {
String stra = this.getDDateTime();
String strb = m.getDDateTime();
Timestamp a = Timestamp.valueOf(stra);
Timestamp b = Timestamp.valueOf(strb);
if (a.before(b)) {
return -1;
} else if (a.after(b)) {
return 1;
} else {
return 0;
}
}
public float getMemoryRatio() {
return memoryRatio;
}
public void setMemoryRatio(float memoryRatio) {
this.memoryRatio = memoryRatio;
}
public void setUsedMemory(float usedMemory) {
this.usedMemory = usedMemory;
}
public void setBuffersMemory(float buffersMemory) {
this.buffersMemory = buffersMemory;
}
public void setCachedMemory(float cachedMemory) {
this.cachedMemory = cachedMemory;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.StringTokenizer;
import sun.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
/*采集系统采集数据实现类*/
/**
* @author Administrator
*
*/
public class MonitorServiceImpll implements IMonitorService {
private static final int CPUTIME = 30;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
private static String osVersion = null;
private String mIpAddress = null;
private String dDateTime = null;
private float totalMemorySize = 0.0f;
private float buffersMemory = 0.0f;
private float cachedMemory = 0.0f;
private float usedMemory = 0.0f;
private float memoryRatio;
/**
* 获得当前的监控对象.
*
* @return 返回构造好的监控对象
* @throws Exception
* @author YINLIANG
*/
public MonitorInfoBean getMonitorInfoBean() throws Exception {
int kb = 1024;
CountDate ddate = new CountDate();
osVersion = System.getProperty("os.version");
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
// 操作系统
String osName = System.getProperty("os.name");
// 主机IP
if (osName.toLowerCase().startsWith("windows")) {
mIpAddress = this.getWindowsIp();
} else {
mIpAddress = this.getLinuxIP();
}
if (osName.toLowerCase().startsWith("windows")) {
// 总的物理内存
float totalPhysicalMemorySize = osmxb.getTotalPhysicalMemorySize()
/ kb;
float usedPhysicalMemorySize = (osmxb.getTotalPhysicalMemorySize() - osmxb
.getFreePhysicalMemorySize())
/ kb;
totalMemorySize = Float.parseFloat(String.format("%.1f",
totalPhysicalMemorySize));
// 已使用的物理内存
usedMemory = Float.parseFloat(String.format("%.1f",
usedPhysicalMemorySize));
// windows内存使用率
memoryRatio = Float.parseFloat(String.format("%.1f",
(usedMemory / totalMemorySize) * 100));
} else {
float[] result = null;
result = getLinuxMemInfo();
totalMemorySize = Float
.parseFloat(String.format("%.1f", result[0]));
buffersMemory = Float.parseFloat(String.format("%.1f", result[1]));
cachedMemory = Float.parseFloat(String.format("%.1f", result[2]));
usedMemory = totalMemorySize - result[3];
// linux内存使用率
memoryRatio = Float
.parseFloat(String
.format(
"%.1f",
((usedMemory - (cachedMemory + buffersMemory)) / totalMemorySize) * 100));
}
// 获得cpu频率
double cpuRatio = 0;
if (osName.toLowerCase().startsWith("windows")) {
cpuRatio = this.getCpuRatioForWindows();
} else {
cpuRatio = this.getCpuRateForLinux();
}
/* 取得数据时间 */
dDateTime = ddate.getCurrentYMDHMS();
// 构造返回对象
MonitorInfoBean infoBean = new MonitorInfoBean();
infoBean.setOsName(osName);
infoBean.setCpuRatio(cpuRatio);
infoBean.setMIpAddress(mIpAddress);
infoBean.setDDateTime(dDateTime);
infoBean.setBuffersMemory(buffersMemory);
infoBean.setCachedMemory(cachedMemory);
infoBean.setUsedMemory(usedMemory);
infoBean.setTotalMemorySize(totalMemorySize);
infoBean.setMemoryRatio(memoryRatio);
return infoBean;
}
/**
* 获得Linux下IP地址.
*
* @return 返回Linux下IP地址
* @author yinliang
*/
public String getLinuxIP() {
String ip = "";
try {
Enumeration<?> e1 = (Enumeration<?>) NetworkInterface
.getNetworkInterfaces();
while (e1.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) e1.nextElement();
if (!ni.getName().equals("eth0")) {
continue;
} else {
Enumeration<?> e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
InetAddress ia = (InetAddress) e2.nextElement();
if (ia instanceof Inet6Address)
continue;
ip = ia.getHostAddress();
}
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
System.exit(-1);
}
return ip;
}
public String getWindowsIp() {
String ip = "";
try {
ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return ip;
}
发布了966 篇原创文章 · 获赞 11 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/xiaoyaGrace/article/details/105285328
今日推荐