package lab.sigar; import java.util.ArrayList; import java.util.List; import org.hyperic.sigar.NetInterfaceConfig; import org.hyperic.sigar.NetInterfaceStat; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarException; import com.thoughtworks.xstream.XStream; /** * 网卡信息、接口数据、流量 * * 使用Sigar获得网卡信息 * */ public class NetInterfaceData { private NetInterfaceConfig config; private NetInterfaceStat stat; private long rxbps; private long txbps; public NetInterfaceData() {} public void populate(Sigar sigar, String name) throws SigarException { config = sigar.getNetInterfaceConfig(name); try { long start = System.currentTimeMillis(); NetInterfaceStat statStart = sigar.getNetInterfaceStat(name); long rxBytesStart = statStart.getRxBytes(); long txBytesStart = statStart.getTxBytes(); Thread.sleep(1000); long end = System.currentTimeMillis(); NetInterfaceStat statEnd = sigar.getNetInterfaceStat(name); long rxBytesEnd = statEnd.getRxBytes(); long txBytesEnd = statEnd.getTxBytes(); rxbps = (rxBytesEnd - rxBytesStart)*8/(end-start)*1000; txbps = (txBytesEnd - txBytesStart)*8/(end-start)*1000; stat = sigar.getNetInterfaceStat(name); } catch (SigarException e) { } catch (Exception e) { } } public static NetInterfaceData gather(Sigar sigar, String name) throws SigarException { NetInterfaceData data = new NetInterfaceData(); data.populate(sigar, name); return data; } public NetInterfaceConfig getConfig() { return config; } public NetInterfaceStat getStat() { return stat; } public long getRxbps() { return rxbps; } public long getTxbps() { return txbps; } public static void main(String[] args) throws Exception { Sigar sigar = new Sigar(); String[] netIfs = sigar.getNetInterfaceList(); List netIfList = new ArrayList(); for ( String name:netIfs ) { NetInterfaceData netIfData1 = NetInterfaceData.gather(sigar, name); netIfList.add(netIfData1); } XStream xstream = new XStream(); xstream.alias("NetInterfaceDatas", List.class); xstream.alias("NetInterfaceData", NetInterfaceData.class); System.out.println(xstream.toXML(netIfList)); } }
输出:
<NetInterfaceDatas> <NetInterfaceData> <config> <name>eth0</name> <hwaddr>00:FF:AE:0F:32:92</hwaddr> <type>Ethernet</type> <description>TAP-Win32 Adapter V9 #2 - ????ü??????ò?????</description> <address>0.0.0.0</address> <destination>0.0.0.0</destination> <broadcast>255.255.255.255</broadcast> <netmask>0.0.0.0</netmask> <flags>2050</flags> <mtu>1500</mtu> <metric>0</metric> </config> <stat> <rxBytes>0</rxBytes> <rxPackets>0</rxPackets> <rxErrors>0</rxErrors> <rxDropped>0</rxDropped> <rxOverruns>-1</rxOverruns> <rxFrame>-1</rxFrame> <txBytes>0</txBytes> <txPackets>0</txPackets> <txErrors>0</txErrors> <txDropped>0</txDropped> <txOverruns>-1</txOverruns> <txCollisions>-1</txCollisions> <txCarrier>-1</txCarrier> <speed>10000000</speed> </stat> <rxbps>0</rxbps> <txbps>0</txbps> </NetInterfaceData> <NetInterfaceData> <config> <name>eth1</name> <hwaddr>00:FF:C8:0A:AE:37</hwaddr> <type>Ethernet</type> <description>eetrust VAdapter - ????ü??????ò?????</description> <address>0.0.0.0</address> <destination>0.0.0.0</destination> <broadcast>255.255.255.255</broadcast> <netmask>0.0.0.0</netmask> <flags>2050</flags> <mtu>1500</mtu> <metric>0</metric> </config> <stat> <rxBytes>0</rxBytes> <rxPackets>0</rxPackets> <rxErrors>0</rxErrors> <rxDropped>0</rxDropped> <rxOverruns>-1</rxOverruns> <rxFrame>-1</rxFrame> <txBytes>0</txBytes> <txPackets>0</txPackets> <txErrors>0</txErrors> <txDropped>0</txDropped> <txOverruns>-1</txOverruns> <txCollisions>-1</txCollisions> <txCarrier>-1</txCarrier> <speed>10000000</speed> </stat> <rxbps>0</rxbps> <txbps>0</txbps> </NetInterfaceData> <NetInterfaceData> <config> <name>eth2</name> <hwaddr>00:22:68:5D:C8:52</hwaddr> <type>Ethernet</type> <description>Realtek PCIe GBE Family Controller - ????ü??????ò?????</description> <address>10.3.43.63</address> <destination>0.0.0.0</destination> <broadcast>10.3.43.255</broadcast> <netmask>255.255.255.0</netmask> <flags>2115</flags> <mtu>1500</mtu> <metric>0</metric> </config> <stat> <rxBytes>177337765</rxBytes> <rxPackets>295449</rxPackets> <rxErrors>0</rxErrors> <rxDropped>0</rxDropped> <rxOverruns>-1</rxOverruns> <rxFrame>-1</rxFrame> <txBytes>27724399</txBytes> <txPackets>203267</txPackets> <txErrors>0</txErrors> <txDropped>0</txDropped> <txOverruns>-1</txOverruns> <txCollisions>-1</txCollisions> <txCarrier>-1</txCarrier> <speed>100000000</speed> </stat> <rxbps>2000</rxbps> <txbps>2000</txbps> </NetInterfaceData> <NetInterfaceData> <config> <name>lo0</name> <hwaddr>00:00:00:00:00:00</hwaddr> <type>Local Loopback</type> <description>MS TCP Loopback interface</description> <address>127.0.0.1</address> <destination>0.0.0.0</destination> <broadcast>0.0.0.0</broadcast> <netmask>255.0.0.0</netmask> <flags>73</flags> <mtu>1520</mtu> <metric>0</metric> </config> <stat> <rxBytes>36535999</rxBytes> <rxPackets>350095</rxPackets> <rxErrors>0</rxErrors> <rxDropped>0</rxDropped> <rxOverruns>-1</rxOverruns> <rxFrame>-1</rxFrame> <txBytes>36535999</txBytes> <txPackets>350031</txPackets> <txErrors>0</txErrors> <txDropped>0</txDropped> <txOverruns>-1</txOverruns> <txCollisions>-1</txCollisions> <txCarrier>-1</txCarrier> <speed>10000000</speed> </stat> <rxbps>1000</rxbps> <txbps>1000</txbps> </NetInterfaceData> </NetInterfaceDatas>下载地址: 见内容底部