废话不多说,能用为主;sigar可以获取所有信息,难得弄了,cpu用它,其他自己获取就难得改了,sigar-x86-winnt.dll文件,它要放在jdk的bin目录下
主要涉及到 ManagementFactory,FileSystemView,Runtime,System,OperatingSystemMXBean 等类 本次代码写的较次,也是学习别人帖子和网上的东西弄的,瞧不起也不要喷,谢谢。
package monitor.services;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.filechooser.FileSystemView;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import monitor.entity.CPUEntity;
import monitor.entity.Disk;
import monitor.util.ParameterUtil;
import com.sun.management.OperatingSystemMXBean;
public class SystemServicesImpl implements SystemServices {
private Sigar sigar = new Sigar(); //创建sigar对象,获取cpu各项信息;
/**
* 计算各个磁盘的大小、已用空间、剩余空间; 只获取本地磁盘信息,不获取U盘等外设信息
* @return
*/
public List<Disk> diskSpaceGather() {
List<Disk> diskList = new ArrayList<Disk>();
FileSystemView fileSystemView = FileSystemView.getFileSystemView();
File[] roots = File.listRoots();
for (File file : roots) {
Disk disk = new Disk();
if (ParameterUtil.LOCAL_DISK_NAME.equals(fileSystemView
.getSystemTypeDescription(file))) {
disk.setDiskName(file.getAbsolutePath());
disk.setFree(file.getFreeSpace() / ParameterUtil.GB);
disk.setTotal(file.getTotalSpace() / ParameterUtil.GB);
disk.setUsed(file.getUsableSpace() / ParameterUtil.GB);
diskList.add(disk);
}
}
return diskList;
}
/**
* 计算物理内存空间大小,包括总的大小,已使用,剩余量
*/
public Map<String,Object> physicalMemoryGather() {
Map<String,Object> hashMap=new HashMap<String,Object>();
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
String osName = System.getProperty("os.name");
long totalMemory = osmxb.getTotalPhysicalMemorySize()/ ParameterUtil.MB;
long freeMemory = osmxb.getFreePhysicalMemorySize()/ ParameterUtil.MB;
long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize())/ ParameterUtil.MB;
hashMap.put("osName", osName);
hashMap.put("total", totalMemory);
hashMap.put("free", freeMemory);
hashMap.put("used", usedMemory);
return hashMap;
}
public List<CPUEntity> CPUInfoGather(){
List<CPUEntity> list = new ArrayList<CPUEntity>();
CpuPerc[] cpus;
CpuPerc total;
try {
cpus = this.sigar.getCpuPercList(); //获取每一个cpu核使用情况
total=this.sigar.getCpuPerc(); //获取cpu整体使用情况;
for (int i = 0; i < cpus.length; i++){
CPUEntity cpu=new CPUEntity();
cpu.setCpuName("CPU"+i);
cpu.setUseTime(CpuPerc.format(cpus[i].getUser()));// 用户态使用的CPU百分比
cpu.setSysTime(CpuPerc.format(cpus[i].getSys()));// 系统态使用的CPU百分比
cpu.setIdleTime(CpuPerc.format(cpus[i].getIdle()));//CPU空闲时间
list.add(cpu);
}
CPUEntity CPUTotal=new CPUEntity();
CPUTotal.setCpuName("CPUTotal");
CPUTotal.setUseTime(CpuPerc.format(total.getUser()));
CPUTotal.setSysTime(CpuPerc.format(total.getSys()));
CPUTotal.setIdleTime(CpuPerc.format(total.getIdle()));
list.add(CPUTotal);
} catch (SigarException e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
SystemServicesImpl service=new SystemServicesImpl();
Map<String,Object> memory=service.physicalMemoryGather();
List<Disk> list=service.diskSpaceGather();
System.out.println((String)memory.get("osName")+" "+"total "+(Long)memory.get("total")+"GB,free "+(Long)memory.get("free")+"GB,used "+(Long)memory.get("used")+"GB");
for(Iterator iterator = list.iterator();iterator.hasNext();)
{
Disk disk=(Disk) iterator.next();
disk.toString();
}
for(Iterator iterator = service.CPUInfoGather().iterator();iterator.hasNext();)
{
CPUEntity cpu=(CPUEntity) iterator.next();
System.out.println(cpu.getCpuName()+":"+cpu.getUseTime());
}
}
}
主要涉及到 ManagementFactory,FileSystemView,Runtime,System,OperatingSystemMXBean 等类 本次代码写的较次,也是学习别人帖子和网上的东西弄的,瞧不起也不要喷,谢谢。
package monitor.services;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.filechooser.FileSystemView;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import monitor.entity.CPUEntity;
import monitor.entity.Disk;
import monitor.util.ParameterUtil;
import com.sun.management.OperatingSystemMXBean;
public class SystemServicesImpl implements SystemServices {
private Sigar sigar = new Sigar(); //创建sigar对象,获取cpu各项信息;
/**
* 计算各个磁盘的大小、已用空间、剩余空间; 只获取本地磁盘信息,不获取U盘等外设信息
* @return
*/
public List<Disk> diskSpaceGather() {
List<Disk> diskList = new ArrayList<Disk>();
FileSystemView fileSystemView = FileSystemView.getFileSystemView();
File[] roots = File.listRoots();
for (File file : roots) {
Disk disk = new Disk();
if (ParameterUtil.LOCAL_DISK_NAME.equals(fileSystemView
.getSystemTypeDescription(file))) {
disk.setDiskName(file.getAbsolutePath());
disk.setFree(file.getFreeSpace() / ParameterUtil.GB);
disk.setTotal(file.getTotalSpace() / ParameterUtil.GB);
disk.setUsed(file.getUsableSpace() / ParameterUtil.GB);
diskList.add(disk);
}
}
return diskList;
}
/**
* 计算物理内存空间大小,包括总的大小,已使用,剩余量
*/
public Map<String,Object> physicalMemoryGather() {
Map<String,Object> hashMap=new HashMap<String,Object>();
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
String osName = System.getProperty("os.name");
long totalMemory = osmxb.getTotalPhysicalMemorySize()/ ParameterUtil.MB;
long freeMemory = osmxb.getFreePhysicalMemorySize()/ ParameterUtil.MB;
long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize())/ ParameterUtil.MB;
hashMap.put("osName", osName);
hashMap.put("total", totalMemory);
hashMap.put("free", freeMemory);
hashMap.put("used", usedMemory);
return hashMap;
}
public List<CPUEntity> CPUInfoGather(){
List<CPUEntity> list = new ArrayList<CPUEntity>();
CpuPerc[] cpus;
CpuPerc total;
try {
cpus = this.sigar.getCpuPercList(); //获取每一个cpu核使用情况
total=this.sigar.getCpuPerc(); //获取cpu整体使用情况;
for (int i = 0; i < cpus.length; i++){
CPUEntity cpu=new CPUEntity();
cpu.setCpuName("CPU"+i);
cpu.setUseTime(CpuPerc.format(cpus[i].getUser()));// 用户态使用的CPU百分比
cpu.setSysTime(CpuPerc.format(cpus[i].getSys()));// 系统态使用的CPU百分比
cpu.setIdleTime(CpuPerc.format(cpus[i].getIdle()));//CPU空闲时间
list.add(cpu);
}
CPUEntity CPUTotal=new CPUEntity();
CPUTotal.setCpuName("CPUTotal");
CPUTotal.setUseTime(CpuPerc.format(total.getUser()));
CPUTotal.setSysTime(CpuPerc.format(total.getSys()));
CPUTotal.setIdleTime(CpuPerc.format(total.getIdle()));
list.add(CPUTotal);
} catch (SigarException e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
SystemServicesImpl service=new SystemServicesImpl();
Map<String,Object> memory=service.physicalMemoryGather();
List<Disk> list=service.diskSpaceGather();
System.out.println((String)memory.get("osName")+" "+"total "+(Long)memory.get("total")+"GB,free "+(Long)memory.get("free")+"GB,used "+(Long)memory.get("used")+"GB");
for(Iterator iterator = list.iterator();iterator.hasNext();)
{
Disk disk=(Disk) iterator.next();
disk.toString();
}
for(Iterator iterator = service.CPUInfoGather().iterator();iterator.hasNext();)
{
CPUEntity cpu=(CPUEntity) iterator.next();
System.out.println(cpu.getCpuName()+":"+cpu.getUseTime());
}
}
}