package com.cnm.collect.impl;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
import com.cnm.collect.ISystemInfo;
import com.cnm.collect.util.CnmConstant;
import com.cnm.target.domain.FileSysInfo;
import com.cnm.target.domain.SysCpuInfo;
import com.cnm.target.domain.SysMemInfo;
import com.cnm.target.domain.SysNetInfo;
import com.cnm.target.domain.SysSwapInfo;
import com.cnm.target.domain.SystemInfo;
public class SystemInfoImpl implements ISystemInfo {
private int getCpuCount() {
// TODO Auto-generated method stub
int sum = 0;
Sigar sigar = new Sigar();
try {
sum = sigar.getCpuInfoList().length;
} catch (SigarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sigar.close();
}
return sum;
}
private Map getCpuDetail() {
// TODO Auto-generated method stub
Map cpuDetail = new HashMap();
Sigar sigar = new Sigar();
CpuInfo[] infos;
try {
infos = sigar.getCpuInfoList();
for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
CpuInfo info = infos[i];
cpuDetail.put(CnmConstant.MAINFREQUENCY, info.getMhz());// CPU的总量MHz
cpuDetail.put(CnmConstant.VENDOR, info.getVendor());// 获得CPU的厂商,如:Intel
cpuDetail.put(CnmConstant.CATEGORY, info.getModel());// 获得CPU的类别,如:Celeron
cpuDetail.put(CnmConstant.CACHE, info.getCacheSize());// 缓冲存储器数量
}
} catch (SigarException e) {
e.printStackTrace();
} finally {
sigar.close();
}
return cpuDetail;
}
@Override
public List<SysCpuInfo> getCpuPerc() {
// TODO Auto-generated method stub
SysCpuInfo cpuPerc = null;
List<SysCpuInfo> cpus = new ArrayList<SysCpuInfo>();
Sigar sigar = new Sigar();
CpuPerc cpuList[] = null;
try {
cpuList = sigar.getCpuPercList();
} catch (SigarException e) {
e.printStackTrace();
} finally {
sigar.close();
}
for (int i = 0; i < cpuList.length; i++) {
cpuPerc = new SysCpuInfo();
cpuPerc.setName("cpu_" + i);
cpuPerc.setUserPerc(removePercent(CpuPerc.format(cpuList[i].getUser())));
cpuPerc.setFreePerc(removePercent(CpuPerc.format(cpuList[i].getIdle())));
cpuPerc.setNicePerc(removePercent(CpuPerc.format(cpuList[i].getNice())));
cpuPerc.setSumPerc(removePercent(CpuPerc.format(cpuList[i].getCombined())));
cpuPerc.setSysPerc(removePercent(CpuPerc.format(cpuList[i].getSys())));
cpuPerc.setWaitPerc(removePercent(CpuPerc.format(cpuList[i].getWait())));
cpuPerc.setCollTime(System.currentTimeMillis() + "");
cpus.add(cpuPerc);
}
return cpus;
}
private String removePercent(String value) {
String strs[] = value.split("%");
return strs[0];
}
@Override
public SysMemInfo getPhysicalMemory() {
// TODO Auto-generated method stub
DecimalFormat df = new DecimalFormat("#0.00");
Sigar sigar = new Sigar();
Mem mem;
SysMemInfo mems = new SysMemInfo();
try {
mem = sigar.getMem();
mems.setUseMem(df.format((float) mem.getUsed() / 1024 / 1024 / 1024));
mems.setFreeMem(df.format((float) mem.getFree() / 1024 / 1024 / 1024));
} catch (SigarException e) {
e.printStackTrace();
} finally {
sigar.close();
}
return mems;
}
@Override
public SysSwapInfo getSwapMemory() {
// TODO Auto-generated method stub
SysSwapInfo swapMemory = new SysSwapInfo();
DecimalFormat df = new DecimalFormat("#0.00");
Sigar sigar = new Sigar();
try {
// b)系统页面文件交换区信息
Swap swap = sigar.getSwap();
swapMemory
.setFreeSwap(df.format((float) swap.getFree() / 1024 / 1024 / 1024));
swapMemory
.setUseSwap(df.format((float) swap.getUsed() / 1024 / 1024 / 1024));
swapMemory
.setTotalSwap(df.format((float) swap.getTotal() / 1024 / 1024 / 1024));
} catch (SigarException e) {
e.printStackTrace();
} finally {
sigar.close();
}
return swapMemory;
}
@Override
public List<FileSysInfo> getFileSystemInfo() {
// TODO Auto-generated method stub
Sigar sigar = new Sigar();
List<FileSysInfo> fileSysInfos = new ArrayList();
FileSysInfo fileSys = null;
try {
FileSystem fslist[] = sigar.getFileSystemList();
DecimalFormat df = new DecimalFormat("#0.00");
for (int i = 0; i < fslist.length; i++) {
fileSys = new FileSysInfo();
FileSystem fs = fslist[i];
// 分区的盘符名称
fileSys.setDevName(fs.getDevName());
fileSys.setDirName(fs.getDirName());
fileSys.setTypeName(fs.getSysTypeName());
fileSys.setSysTypeName(fs.getSysTypeName());
fileSys.setType(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 : 本地硬盘
// 文件系统总大小
if (df.format((float) usage.getTotal() / 1024 / 1024).equals("")) {
}
else
{
fileSys.setTotalFile(df.format((float) usage.getTotal() / 1024 / 1024));
}
// 文件系统剩余大小
fileSys.setFreeFile(df.format((float) usage.getFree() / 1024 / 1024));
// 文件系统可用大小
fileSys.setAvailFile(df.format((float) usage.getAvail() / 1024 / 1024));
// 文件系统已经使用量
fileSys.setUsedFile(df.format((float) usage.getUsed() / 1024 / 1024));
double usePercent = usage.getUsePercent() * 100D;
// 文件系统资源的利用率
fileSys.setUsePercent(usePercent + "");
break;
case 3:// TYPE_NETWORK :网络
break;
case 4:// TYPE_RAM_DISK :闪存
break;
case 5:// TYPE_CDROM :光驱
break;
case 6:// TYPE_SWAP :页面交换
break;
}
fileSys.setDiskReads(usage.getDiskReads() + "");
fileSys.setDiskWrites(usage.getDiskWrites() + "");
fileSys.setCollTime(System.currentTimeMillis() + "");
if (fileSys.getTotalFile()!=null ) {
fileSysInfos.add(fileSys);
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return fileSysInfos;
}
private Map getOSInfo() {
// TODO Auto-generated method stub
OperatingSystem OS = OperatingSystem.getInstance();
Map OSInfo = new HashMap();
try {
OSInfo.put(CnmConstant.HOSTNAME, InetAddress.getLocalHost()
.getHostName());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OSInfo.put(CnmConstant.OSARCH, OS.getArch());
OSInfo.put(CnmConstant.OSDATAMODEL, OS.getDataModel());
OSInfo.put(CnmConstant.OSDESC, OS.getDescription());
OSInfo.put(CnmConstant.OSPATCHLEVEL, OS.getPatchLevel());
OSInfo.put(CnmConstant.OSVENDOR, OS.getVendor());
OSInfo.put(CnmConstant.OSVENDORNAME, OS.getVendorName());
OSInfo.put(CnmConstant.OSVENDORVERSION, OS.getVendorVersion());
OSInfo.put(CnmConstant.OSVERSION, OS.getVersion());
return OSInfo;
}
private String getFQDN() {
// TODO Auto-generated method stub
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();
}
}
}
private String getDefaultIpAddress() {
// TODO Auto-generated method stub
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;
}
private String getMAC() {
// TODO Auto-generated method stub
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();
}
}
@Override
public List<SysNetInfo> getNetIfList() {
// TODO Auto-generated method stub
Sigar sigar = new Sigar();
List<SysNetInfo> netList = new ArrayList();
SysNetInfo sni = null;
try {
String ifNames[] = sigar.getNetInterfaceList();
for (int i = 0; i < ifNames.length; i++) {
sni = new SysNetInfo();
String name = ifNames[i];
NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
sni.setNetName(name);
sni.setIpAddress(ifconfig.getAddress());
sni.setNetmask(ifconfig.getNetmask());
if ((ifconfig.getFlags() & 1L) <= 0L) {
System.out
.println("!IFF_UP...skipping getNetInterfaceStat");
continue;
}
NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
sni.setRxPackets(ifstat.getRxPackets() + "");
sni.setTxPackets(ifstat.getTxPackets() + "");
sni.setRxBytes(ifstat.getRxBytes() + "");
sni.setTxBytes(ifstat.getTxBytes() + "");
sni.setRxErrors(ifstat.getRxErrors() + "");
sni.setTxErrors(ifstat.getTxErrors() + "");
sni.setRxDropped(ifstat.getRxDropped() + "");
sni.setTxDropped(ifstat.getTxDropped() + "");
netList.add(sni);
}
} catch (SigarNotImplementedException e) {
} catch (SigarException e) {
e.printStackTrace();
}
return netList;
}
@Override
public SystemInfo getSystemInfo() {
// TODO Auto-generated method stub
SystemInfo sinfo = new SystemInfo();
sinfo.setCpuCount(this.getCpuCount() + "");
Map cpuInfos = this.getCpuDetail();
sinfo.setCpuMainFrequency(cpuInfos.get(CnmConstant.MAINFREQUENCY)
.toString());
sinfo.setCpuVendor(cpuInfos.get(CnmConstant.VENDOR).toString());
sinfo.setCpuCategory(cpuInfos.get(CnmConstant.CATEGORY).toString());
sinfo.setCpuCache(cpuInfos.get(CnmConstant.CACHE).toString());
Map osInfos = this.getOSInfo();
sinfo.setHostname(osInfos.get(CnmConstant.HOSTNAME).toString());
sinfo.setOsArch(osInfos.get(CnmConstant.OSARCH).toString());
sinfo.setOsDataModel(osInfos.get(CnmConstant.OSDATAMODEL).toString());
sinfo.setOsDescription(osInfos.get(CnmConstant.OSDESC).toString());
sinfo.setOsVendor(osInfos.get(CnmConstant.OSVENDOR).toString());
sinfo.setOsVendorName(osInfos.get(CnmConstant.OSVENDORNAME).toString());
sinfo.setOsVendorVersion(osInfos.get(CnmConstant.OSVENDORVERSION)
.toString());
sinfo.setOsVersion(osInfos.get(CnmConstant.OSVERSION).toString());
sinfo.setOsPatchLevel(osInfos.get(CnmConstant.OSPATCHLEVEL).toString());
sinfo.setMac(this.getMAC());
sinfo.setDefaultIpAddress(this.getDefaultIpAddress());
sinfo.setFqdn(this.getFQDN());
DecimalFormat df = new DecimalFormat("#0.00");
Sigar sigar = new Sigar();
Mem mem;
try {
mem = sigar.getMem();
sinfo.setTotalMem(df.format((float) mem.getTotal() / 1024 / 1024 / 1024));
} catch (SigarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sinfo;
}
}
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
import com.cnm.collect.ISystemInfo;
import com.cnm.collect.util.CnmConstant;
import com.cnm.target.domain.FileSysInfo;
import com.cnm.target.domain.SysCpuInfo;
import com.cnm.target.domain.SysMemInfo;
import com.cnm.target.domain.SysNetInfo;
import com.cnm.target.domain.SysSwapInfo;
import com.cnm.target.domain.SystemInfo;
public class SystemInfoImpl implements ISystemInfo {
private int getCpuCount() {
// TODO Auto-generated method stub
int sum = 0;
Sigar sigar = new Sigar();
try {
sum = sigar.getCpuInfoList().length;
} catch (SigarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
sigar.close();
}
return sum;
}
private Map getCpuDetail() {
// TODO Auto-generated method stub
Map cpuDetail = new HashMap();
Sigar sigar = new Sigar();
CpuInfo[] infos;
try {
infos = sigar.getCpuInfoList();
for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
CpuInfo info = infos[i];
cpuDetail.put(CnmConstant.MAINFREQUENCY, info.getMhz());// CPU的总量MHz
cpuDetail.put(CnmConstant.VENDOR, info.getVendor());// 获得CPU的厂商,如:Intel
cpuDetail.put(CnmConstant.CATEGORY, info.getModel());// 获得CPU的类别,如:Celeron
cpuDetail.put(CnmConstant.CACHE, info.getCacheSize());// 缓冲存储器数量
}
} catch (SigarException e) {
e.printStackTrace();
} finally {
sigar.close();
}
return cpuDetail;
}
@Override
public List<SysCpuInfo> getCpuPerc() {
// TODO Auto-generated method stub
SysCpuInfo cpuPerc = null;
List<SysCpuInfo> cpus = new ArrayList<SysCpuInfo>();
Sigar sigar = new Sigar();
CpuPerc cpuList[] = null;
try {
cpuList = sigar.getCpuPercList();
} catch (SigarException e) {
e.printStackTrace();
} finally {
sigar.close();
}
for (int i = 0; i < cpuList.length; i++) {
cpuPerc = new SysCpuInfo();
cpuPerc.setName("cpu_" + i);
cpuPerc.setUserPerc(removePercent(CpuPerc.format(cpuList[i].getUser())));
cpuPerc.setFreePerc(removePercent(CpuPerc.format(cpuList[i].getIdle())));
cpuPerc.setNicePerc(removePercent(CpuPerc.format(cpuList[i].getNice())));
cpuPerc.setSumPerc(removePercent(CpuPerc.format(cpuList[i].getCombined())));
cpuPerc.setSysPerc(removePercent(CpuPerc.format(cpuList[i].getSys())));
cpuPerc.setWaitPerc(removePercent(CpuPerc.format(cpuList[i].getWait())));
cpuPerc.setCollTime(System.currentTimeMillis() + "");
cpus.add(cpuPerc);
}
return cpus;
}
private String removePercent(String value) {
String strs[] = value.split("%");
return strs[0];
}
@Override
public SysMemInfo getPhysicalMemory() {
// TODO Auto-generated method stub
DecimalFormat df = new DecimalFormat("#0.00");
Sigar sigar = new Sigar();
Mem mem;
SysMemInfo mems = new SysMemInfo();
try {
mem = sigar.getMem();
mems.setUseMem(df.format((float) mem.getUsed() / 1024 / 1024 / 1024));
mems.setFreeMem(df.format((float) mem.getFree() / 1024 / 1024 / 1024));
} catch (SigarException e) {
e.printStackTrace();
} finally {
sigar.close();
}
return mems;
}
@Override
public SysSwapInfo getSwapMemory() {
// TODO Auto-generated method stub
SysSwapInfo swapMemory = new SysSwapInfo();
DecimalFormat df = new DecimalFormat("#0.00");
Sigar sigar = new Sigar();
try {
// b)系统页面文件交换区信息
Swap swap = sigar.getSwap();
swapMemory
.setFreeSwap(df.format((float) swap.getFree() / 1024 / 1024 / 1024));
swapMemory
.setUseSwap(df.format((float) swap.getUsed() / 1024 / 1024 / 1024));
swapMemory
.setTotalSwap(df.format((float) swap.getTotal() / 1024 / 1024 / 1024));
} catch (SigarException e) {
e.printStackTrace();
} finally {
sigar.close();
}
return swapMemory;
}
@Override
public List<FileSysInfo> getFileSystemInfo() {
// TODO Auto-generated method stub
Sigar sigar = new Sigar();
List<FileSysInfo> fileSysInfos = new ArrayList();
FileSysInfo fileSys = null;
try {
FileSystem fslist[] = sigar.getFileSystemList();
DecimalFormat df = new DecimalFormat("#0.00");
for (int i = 0; i < fslist.length; i++) {
fileSys = new FileSysInfo();
FileSystem fs = fslist[i];
// 分区的盘符名称
fileSys.setDevName(fs.getDevName());
fileSys.setDirName(fs.getDirName());
fileSys.setTypeName(fs.getSysTypeName());
fileSys.setSysTypeName(fs.getSysTypeName());
fileSys.setType(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 : 本地硬盘
// 文件系统总大小
if (df.format((float) usage.getTotal() / 1024 / 1024).equals("")) {
}
else
{
fileSys.setTotalFile(df.format((float) usage.getTotal() / 1024 / 1024));
}
// 文件系统剩余大小
fileSys.setFreeFile(df.format((float) usage.getFree() / 1024 / 1024));
// 文件系统可用大小
fileSys.setAvailFile(df.format((float) usage.getAvail() / 1024 / 1024));
// 文件系统已经使用量
fileSys.setUsedFile(df.format((float) usage.getUsed() / 1024 / 1024));
double usePercent = usage.getUsePercent() * 100D;
// 文件系统资源的利用率
fileSys.setUsePercent(usePercent + "");
break;
case 3:// TYPE_NETWORK :网络
break;
case 4:// TYPE_RAM_DISK :闪存
break;
case 5:// TYPE_CDROM :光驱
break;
case 6:// TYPE_SWAP :页面交换
break;
}
fileSys.setDiskReads(usage.getDiskReads() + "");
fileSys.setDiskWrites(usage.getDiskWrites() + "");
fileSys.setCollTime(System.currentTimeMillis() + "");
if (fileSys.getTotalFile()!=null ) {
fileSysInfos.add(fileSys);
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return fileSysInfos;
}
private Map getOSInfo() {
// TODO Auto-generated method stub
OperatingSystem OS = OperatingSystem.getInstance();
Map OSInfo = new HashMap();
try {
OSInfo.put(CnmConstant.HOSTNAME, InetAddress.getLocalHost()
.getHostName());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OSInfo.put(CnmConstant.OSARCH, OS.getArch());
OSInfo.put(CnmConstant.OSDATAMODEL, OS.getDataModel());
OSInfo.put(CnmConstant.OSDESC, OS.getDescription());
OSInfo.put(CnmConstant.OSPATCHLEVEL, OS.getPatchLevel());
OSInfo.put(CnmConstant.OSVENDOR, OS.getVendor());
OSInfo.put(CnmConstant.OSVENDORNAME, OS.getVendorName());
OSInfo.put(CnmConstant.OSVENDORVERSION, OS.getVendorVersion());
OSInfo.put(CnmConstant.OSVERSION, OS.getVersion());
return OSInfo;
}
private String getFQDN() {
// TODO Auto-generated method stub
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();
}
}
}
private String getDefaultIpAddress() {
// TODO Auto-generated method stub
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;
}
private String getMAC() {
// TODO Auto-generated method stub
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();
}
}
@Override
public List<SysNetInfo> getNetIfList() {
// TODO Auto-generated method stub
Sigar sigar = new Sigar();
List<SysNetInfo> netList = new ArrayList();
SysNetInfo sni = null;
try {
String ifNames[] = sigar.getNetInterfaceList();
for (int i = 0; i < ifNames.length; i++) {
sni = new SysNetInfo();
String name = ifNames[i];
NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
sni.setNetName(name);
sni.setIpAddress(ifconfig.getAddress());
sni.setNetmask(ifconfig.getNetmask());
if ((ifconfig.getFlags() & 1L) <= 0L) {
System.out
.println("!IFF_UP...skipping getNetInterfaceStat");
continue;
}
NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
sni.setRxPackets(ifstat.getRxPackets() + "");
sni.setTxPackets(ifstat.getTxPackets() + "");
sni.setRxBytes(ifstat.getRxBytes() + "");
sni.setTxBytes(ifstat.getTxBytes() + "");
sni.setRxErrors(ifstat.getRxErrors() + "");
sni.setTxErrors(ifstat.getTxErrors() + "");
sni.setRxDropped(ifstat.getRxDropped() + "");
sni.setTxDropped(ifstat.getTxDropped() + "");
netList.add(sni);
}
} catch (SigarNotImplementedException e) {
} catch (SigarException e) {
e.printStackTrace();
}
return netList;
}
@Override
public SystemInfo getSystemInfo() {
// TODO Auto-generated method stub
SystemInfo sinfo = new SystemInfo();
sinfo.setCpuCount(this.getCpuCount() + "");
Map cpuInfos = this.getCpuDetail();
sinfo.setCpuMainFrequency(cpuInfos.get(CnmConstant.MAINFREQUENCY)
.toString());
sinfo.setCpuVendor(cpuInfos.get(CnmConstant.VENDOR).toString());
sinfo.setCpuCategory(cpuInfos.get(CnmConstant.CATEGORY).toString());
sinfo.setCpuCache(cpuInfos.get(CnmConstant.CACHE).toString());
Map osInfos = this.getOSInfo();
sinfo.setHostname(osInfos.get(CnmConstant.HOSTNAME).toString());
sinfo.setOsArch(osInfos.get(CnmConstant.OSARCH).toString());
sinfo.setOsDataModel(osInfos.get(CnmConstant.OSDATAMODEL).toString());
sinfo.setOsDescription(osInfos.get(CnmConstant.OSDESC).toString());
sinfo.setOsVendor(osInfos.get(CnmConstant.OSVENDOR).toString());
sinfo.setOsVendorName(osInfos.get(CnmConstant.OSVENDORNAME).toString());
sinfo.setOsVendorVersion(osInfos.get(CnmConstant.OSVENDORVERSION)
.toString());
sinfo.setOsVersion(osInfos.get(CnmConstant.OSVERSION).toString());
sinfo.setOsPatchLevel(osInfos.get(CnmConstant.OSPATCHLEVEL).toString());
sinfo.setMac(this.getMAC());
sinfo.setDefaultIpAddress(this.getDefaultIpAddress());
sinfo.setFqdn(this.getFQDN());
DecimalFormat df = new DecimalFormat("#0.00");
Sigar sigar = new Sigar();
Mem mem;
try {
mem = sigar.getMem();
sinfo.setTotalMem(df.format((float) mem.getTotal() / 1024 / 1024 / 1024));
} catch (SigarException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sinfo;
}
}