자 바 는 CPU 사용률, 하드디스크 크기, 네트워크 카드 상태, 시스템 정보 등 을 포함 한 시스템 정 보 를 얻는다
15051 단어 자바
singar 프로젝트 사이트: http://support.hyperic.com/display/SIGAR/Home
코드:
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarNotImplementedException;
import org.hyperic.sigar.Swap;
public class SysInfo {
// 1.CPU
// a)CPU ( : )
public static int getCpuCount() throws SigarException {
Sigar sigar = new Sigar();
try {
return sigar.getCpuInfoList().length;
} finally {
sigar.close();
}
}
// b)CPU ( :HZ) CPU
public void getCpuTotal() {
Sigar sigar = new Sigar();
CpuInfo[] infos;
try {
infos = sigar.getCpuInfoList();
for (int i = 0; i < infos.length; i++) {// CPU CPU
CpuInfo info = infos[i];
System.out.println("mhz=" + info.getMhz());// CPU MHz
System.out.println("vendor=" + info.getVendor());// CPU , :Intel
System.out.println("model=" + info.getModel());// CPU , :Celeron
System.out.println("cache size=" + info.getCacheSize());//
}
} catch (SigarException e) {
e.printStackTrace();
}
}
// c)CPU 、 、 、 ( :100%)
public void testCpuPerc() {
Sigar sigar = new Sigar();
// , CPU
CpuPerc cpu;
try {
cpu = sigar.getCpuPerc();
printCpuPerc(cpu);
} catch (SigarException e) {
e.printStackTrace();
}
// , CPU CPU
CpuPerc cpuList[] = null;
try {
cpuList = sigar.getCpuPercList();
} catch (SigarException e) {
e.printStackTrace();
return;
}
for (int i = 0; i < cpuList.length; i++) {
printCpuPerc(cpuList[i]);
}
}
private void printCpuPerc(CpuPerc cpu) {
System.out.println("User :" + CpuPerc.format(cpu.getUser()));//
System.out.println("Sys :" + CpuPerc.format(cpu.getSys()));//
System.out.println("Wait :" + CpuPerc.format(cpu.getWait()));//
System.out.println("Nice :" + CpuPerc.format(cpu.getNice()));//
System.out.println("Idle :" + CpuPerc.format(cpu.getIdle()));//
System.out.println("Total :" + CpuPerc.format(cpu.getCombined()));//
}
// 2.
public void getPhysicalMemory() {
// a)
Sigar sigar = new Sigar();
Mem mem;
try {
mem = sigar.getMem();
//
System.out.println("Total = " + mem.getTotal() / 1024L + "K av");
//
System.out.println("Used = " + mem.getUsed() / 1024L + "K used");
//
System.out.println("Free = " + mem.getFree() / 1024L + "K free");
// b)
Swap swap = sigar.getSwap();
//
System.out.println("Total = " + swap.getTotal() / 1024L + "K av");
//
System.out.println("Used = " + swap.getUsed() / 1024L + "K used");
//
System.out.println("Free = " + swap.getFree() / 1024L + "K free");
} catch (SigarException e) {
e.printStackTrace();
}
}
// 3.
// a) :
public String getPlatformName() {
String hostname = "";
try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (Exception exc) {
Sigar sigar = new Sigar();
try {
hostname = sigar.getNetInfo().getHostName();
} catch (SigarException e) {
hostname = "localhost.unknown";
} finally {
sigar.close();
}
}
return hostname;
}
// b)
public void testGetOSInfo() {
OperatingSystem OS = OperatingSystem.getInstance();
// : 386、486、586 x86
System.out.println("OS.getArch() = " + OS.getArch());
System.out.println("OS.getCpuEndian() = " + OS.getCpuEndian());//
System.out.println("OS.getDataModel() = " + OS.getDataModel());//
//
System.out.println("OS.getDescription() = " + OS.getDescription());
System.out.println("OS.getMachine() = " + OS.getMachine());//
//
System.out.println("OS.getName() = " + OS.getName());
System.out.println("OS.getPatchLevel() = " + OS.getPatchLevel());//
//
System.out.println("OS.getVendor() = " + OS.getVendor());
//
System.out.println("OS.getVendorCodeName() = " + OS.getVendorCodeName());
//
System.out.println("OS.getVendorName() = " + OS.getVendorName());
//
System.out.println("OS.getVendorVersion() = " + OS.getVendorVersion());
//
System.out.println("OS.getVersion() = " + OS.getVersion());
}
// c)
public void testWho() {
try {
Sigar sigar = new Sigar();
org.hyperic.sigar.Who[] who = sigar.getWhoList();
if (who != null && who.length > 0) {
for (int i = 0; i < who.length; i++) {
System.out.println("
~~~~~~~~~" + String.valueOf(i) + "~~~~~~~~~");
org.hyperic.sigar.Who _who = who[i];
System.out.println("getDevice() = " + _who.getDevice());
System.out.println("getHost() = " + _who.getHost());
System.out.println("getTime() = " + _who.getTime());
//
System.out.println("getUser() = " + _who.getUser());
}
}
} catch (SigarException e) {
e.printStackTrace();
}
}
// 4. ( )
// a) ( sigar.getFileSystemList() FileSystem , ):
public void testFileSystemInfo() throws Exception {
Sigar sigar = new Sigar();
FileSystem fslist[] = sigar.getFileSystemList();
// String dir = System.getProperty("user.home");//
for (int i = 0; i < fslist.length; i++) {
System.out.println("
~~~~~~~~~~" + i + "~~~~~~~~~~");
FileSystem fs = fslist[i];
//
System.out.println("fs.getDevName() = " + fs.getDevName());
//
System.out.println("fs.getDirName() = " + fs.getDirName());
System.out.println("fs.getFlags() = " + fs.getFlags());//
// , FAT32、NTFS
System.out.println("fs.getSysTypeName() = " + fs.getSysTypeName());
// , 、 、
System.out.println("fs.getTypeName() = " + fs.getTypeName());
//
System.out.println("fs.getType() = " + fs.getType());
FileSystemUsage usage = null;
try {
usage = sigar.getFileSystemUsage(fs.getDirName());
} catch (SigarException e) {
if (fs.getType() == 2)
throw e;
continue;
}
switch (fs.getType()) {
case 0: // TYPE_UNKNOWN :
break;
case 1: // TYPE_NONE
break;
case 2: // TYPE_LOCAL_DISK :
//
System.out.println(" Total = " + usage.getTotal() + "KB");
//
System.out.println(" Free = " + usage.getFree() + "KB");
//
System.out.println(" Avail = " + usage.getAvail() + "KB");
//
System.out.println(" Used = " + usage.getUsed() + "KB");
double usePercent = usage.getUsePercent() * 100D;
//
System.out.println(" Usage = " + usePercent + "%");
break;
case 3:// TYPE_NETWORK :
break;
case 4:// TYPE_RAM_DISK :
break;
case 5:// TYPE_CDROM :
break;
case 6:// TYPE_SWAP :
break;
}
System.out.println(" DiskReads = " + usage.getDiskReads());
System.out.println(" DiskWrites = " + usage.getDiskWrites());
}
return;
}
// 5.
// a)
public String getFQDN() {
Sigar sigar = null;
try {
return InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
try {
sigar = new Sigar();
return sigar.getFQDN();
} catch (SigarException ex) {
return null;
} finally {
sigar.close();
}
}
}
// b) IP
public String getDefaultIpAddress() {
String address = null;
try {
address = InetAddress.getLocalHost().getHostAddress();
// IP ,
// Sigar
if (!NetFlags.LOOPBACK_ADDRESS.equals(address)) {
return address;
}
} catch (UnknownHostException e) {
// hostname not in DNS or /etc/hosts
}
Sigar sigar = new Sigar();
try {
address = sigar.getNetInterfaceConfig().getAddress();
} catch (SigarException e) {
address = NetFlags.LOOPBACK_ADDRESS;
} finally {
sigar.close();
}
return address;
}
// c) MAC
public String getMAC() {
Sigar sigar = null;
try {
sigar = new Sigar();
String[] ifaces = sigar.getNetInterfaceList();
String hwaddr = null;
for (int i = 0; i < ifaces.length; i++) {
NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
|| NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
continue;
}
/*
* , MAC , ( ) Collection
* , for MAC 。
*/
hwaddr = cfg.getHwaddr();
break;
}
return hwaddr != null ? hwaddr : null;
} catch (Exception e) {
return null;
} finally {
if (sigar != null)
sigar.close();
}
}
// d)
public void testNetIfList() throws Exception {
Sigar sigar = new Sigar();
String ifNames[] = sigar.getNetInterfaceList();
for (int i = 0; i < ifNames.length; i++) {
String name = ifNames[i];
NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
print("
name = " + name);//
print("Address = " + ifconfig.getAddress());// IP
print("Netmask = " + ifconfig.getNetmask());//
if ((ifconfig.getFlags() & 1L) <= 0L) {
print("!IFF_UP...skipping getNetInterfaceStat");
continue;
}
try {
NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
print("RxPackets = " + ifstat.getRxPackets());//
print("TxPackets = " + ifstat.getTxPackets());//
print("RxBytes = " + ifstat.getRxBytes());//
print("TxBytes = " + ifstat.getTxBytes());//
print("RxErrors = " + ifstat.getRxErrors());//
print("TxErrors = " + ifstat.getTxErrors());//
print("RxDropped = " + ifstat.getRxDropped());//
print("TxDropped = " + ifstat.getTxDropped());//
} catch (SigarNotImplementedException e) {
} catch (SigarException e) {
print(e.getMessage());
}
}
}
void print(String msg) {
System.out.println(msg);
}
// e)
public void getEthernetInfo() {
Sigar sigar = null;
try {
sigar = new Sigar();
String[] ifaces = sigar.getNetInterfaceList();
for (int i = 0; i < ifaces.length; i++) {
NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
|| NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
continue;
}
System.out.println("cfg.getAddress() = " + cfg.getAddress());// IP
System.out.println("cfg.getBroadcast() = " + cfg.getBroadcast());//
System.out.println("cfg.getHwaddr() = " + cfg.getHwaddr());// MAC
System.out.println("cfg.getNetmask() = " + cfg.getNetmask());//
System.out.println("cfg.getDescription() = " + cfg.getDescription());//
System.out.println("cfg.getType() = " + cfg.getType());//
System.out.println("cfg.getDestination() = " + cfg.getDestination());
System.out.println("cfg.getFlags() = " + cfg.getFlags());//
System.out.println("cfg.getMetric() = " + cfg.getMetric());
System.out.println("cfg.getMtu() = " + cfg.getMtu());
System.out.println("cfg.getName() = " + cfg.getName());
System.out.println();
}
} catch (Exception e) {
System.out.println("Error while creating GUID" + e);
} finally {
if (sigar != null)
sigar.close();
}
}
public static void main(String[] arg) {
try {
SysInfo s = new SysInfo();
s.getCpuTotal();
s.getPhysicalMemory();
s.testGetOSInfo();
s.testWho();
s.testFileSystemInfo();
s.testNetIfList();
s.getEthernetInfo();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.