📄 sigarifaceinfo.java
字号:
/* * SigarIfaceInfo.java * * Created on March 14, 2005, 12:29 PM */package net.hyperic.sigar.cmd;import java.util.Arrays;import java.util.Collection;import net.hyperic.sigar.Sigar;import net.hyperic.sigar.SigarException;import net.hyperic.sigar.NetInterfaceConfig;import net.hyperic.sigar.NetInterfaceStat;import net.hyperic.sigar.NetFlags;/** * * @author Sean */public class SigarIfaceInfo extends SigarCommandBase { int _iCurrIfaceIndex; int _iCurrIstatIndex; /** Creates a new instance of SigarIfaceInfo */ public SigarIfaceInfo() { super(); resetIterators(); } /** * */ public int getIfaceCount() throws SigarException { String[] ifNameArr; try { ifNameArr = this.proxy.getNetInterfaceList(); } catch (SigarException e) { throw e; } return ifNameArr.length; } /** * */ public NetInterfaceConfig getIface(int iIndex) throws SigarException { String[] ifNameArr; NetInterfaceConfig ifConfig; try { ifNameArr = this.proxy.getNetInterfaceList(); } catch (SigarException e) { throw e; } if (iIndex < 0 || iIndex > ifNameArr.length || ifNameArr.length == 0) { return null; } try { ifConfig = this.sigar.getNetInterfaceConfig(ifNameArr[iIndex]); } catch (SigarException e) { throw e; } return ifConfig; } /** * */ public NetInterfaceConfig getNextIface() throws SigarException { NetInterfaceConfig ifConfig; try { ifConfig = getIface( _iCurrIfaceIndex ); } catch (SigarException e) { _iCurrIfaceIndex = 0; throw e; } if (ifConfig == null) { _iCurrIfaceIndex = 0; return null; } _iCurrIfaceIndex++; return ifConfig; } /** * */ public void resetIterators() { _iCurrIfaceIndex = 0; _iCurrIstatIndex = 0; } /** * */ public NetInterfaceStat getIfaceStat(int iIndex) throws SigarException { String[] ifNameArr; NetInterfaceStat ifStat; try { ifNameArr = this.proxy.getNetInterfaceList(); } catch (SigarException e) { throw e; } if (iIndex < 0 || iIndex > ifNameArr.length || ifNameArr.length == 0) { return null; } try { ifStat = this.sigar.getNetInterfaceStat(ifNameArr[iIndex]); } catch (SigarException e) { throw e; } return ifStat; } /** * */ public NetInterfaceStat getNextIfaceStat() throws SigarException { NetInterfaceStat ifStat; try { ifStat = getIfaceStat( _iCurrIstatIndex ); } catch (SigarException e) { _iCurrIstatIndex = 0; throw e; } if (ifStat == null) { _iCurrIstatIndex = 0; return null; } _iCurrIstatIndex++; return ifStat; } /** * */ public void output() throws SigarException { try { output(null); } catch (SigarException e) { throw e; } } /** * */ public void output(String[] args) throws SigarException { try { int iMax = getIfaceCount(); for (int i = 0; i<iMax; i++) { NetInterfaceConfig ifConfig = getIface(i); long lFlags = ifConfig.getFlags(); // cannot assume ethernet String encap = (lFlags & NetFlags.IFF_LOOPBACK) > 0 ? "Local Loopback" : "Ethernet"; String hwaddr = ""; if (!NetFlags.NULL_HWADDR.equals(ifConfig.getHwaddr())) { hwaddr = " HWaddr " + ifConfig.getHwaddr(); } println(ifConfig.getName() + "\t" + "Link encap:" + encap + hwaddr); String ptp = ""; if ((lFlags & NetFlags.IFF_POINTOPOINT) > 0) { ptp = " P-t-P:" + ifConfig.getDestination(); } String bcast = ""; if ((lFlags & NetFlags.IFF_BROADCAST) > 0) { bcast = " Bcast:" + ifConfig.getBroadcast(); } println("\t" + "inet addr:" + ifConfig.getAddress() + ptp + //unlikely bcast + " Mask:" + ifConfig.getNetmask()); println("\t" + NetFlags.getIfFlagsString(lFlags) + " MTU:" + ifConfig.getMtu() + " Metric:" + ifConfig.getMetric()); try { NetInterfaceStat ifStat = getIfaceStat(i); println("\t" + "RX packets:" + ifStat.getRxPackets() + " errors:" + ifStat.getRxErrors() + " dropped:" + ifStat.getRxDropped() + " overruns:" + ifStat.getRxOverruns() + " frame:" + ifStat.getRxFrame()); println("\t" + "TX packets:" + ifStat.getTxPackets() + " errors:" + ifStat.getTxErrors() + " dropped:" + ifStat.getTxDropped() + " overruns:" + ifStat.getTxOverruns() + " carrier:" + ifStat.getTxCarrier()); println("\t" + "collisions:" + ifStat.getTxCollisions()); long rxBytes = ifStat.getRxBytes(); long txBytes = ifStat.getTxBytes(); println("\t" + "RX bytes:" + rxBytes + " (" + Sigar.formatSize(rxBytes) + ")" + " " + "TX bytes:" + txBytes + " (" + Sigar.formatSize(txBytes) + ")"); } catch (SigarException e) { continue; } println(""); } } catch (SigarException e) { throw e; } } public static void main( String[] args ) { try { SigarIfaceInfo info = new SigarIfaceInfo(); info.output(); } catch ( Exception ex ) { System.out.println( ex.getMessage() ); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -