首页 > Java开发 > Sigar获取网卡流量信息

Sigar获取网卡流量信息

在网络应用中,经常要对网卡的流量进行统计分析。

和网卡相关的统计数据主要有:网卡信息(ip地址、子网掩码、mac地址等)、网卡的吞吐量、流量等等。

当然了使用sigar也可以获得这些数据。

下面通过例子说明:

[java][/java]

view plaincopy

  1. package lab.sigar;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.hyperic.sigar.NetInterfaceConfig;
  5. import org.hyperic.sigar.NetInterfaceStat;
  6. import org.hyperic.sigar.Sigar;
  7. import org.hyperic.sigar.SigarException;
  8. import com.thoughtworks.xstream.XStream;
  9. /**
  10.  * 网卡信息、接口数据、流量
  11.  *
  12.  * 使用Sigar获得网卡信息
  13.  *
  14.  */
  15. public class NetInterfaceData {
  16.     private NetInterfaceConfig config;
  17.     private NetInterfaceStat stat;
  18.     private long rxbps;
  19.     private long txbps;
  20.     public NetInterfaceData() {}
  21.     public void populate(Sigar sigar, String name)
  22.         throws SigarException {
  23.         config = sigar.getNetInterfaceConfig(name);
  24.         try {
  25.             long start = System.currentTimeMillis();
  26.             NetInterfaceStat statStart = sigar.getNetInterfaceStat(name);
  27.             long rxBytesStart = statStart.getRxBytes();
  28.             long txBytesStart = statStart.getTxBytes();
  29.             Thread.sleep(1000);
  30.             long end = System.currentTimeMillis();
  31.             NetInterfaceStat statEnd = sigar.getNetInterfaceStat(name);
  32.             long rxBytesEnd = statEnd.getRxBytes();
  33.             long txBytesEnd = statEnd.getTxBytes();
  34.             rxbps = (rxBytesEnd - rxBytesStart)*8/(end-start)*1000;
  35.             txbps = (txBytesEnd - txBytesStart)*8/(end-start)*1000;
  36.             stat = sigar.getNetInterfaceStat(name);
  37.         } catch (SigarException e) {
  38.         } catch (Exception e) {
  39.         }
  40.     }
  41.     public static NetInterfaceData gather(Sigar sigar, String name)
  42.         throws SigarException {
  43.         NetInterfaceData data = new NetInterfaceData();
  44.         data.populate(sigar, name);
  45.         return data;
  46.     }
  47.     public NetInterfaceConfig getConfig() {
  48.         return config;
  49.     }
  50.     public NetInterfaceStat getStat() {
  51.         return stat;
  52.     }
  53.     public long getRxbps() {
  54.         return rxbps;
  55.     }
  56.     public long getTxbps() {
  57.         return txbps;
  58.     }
  59.     public static void main(String[] args) throws Exception {
  60.         Sigar sigar = new Sigar();
  61.         String[] netIfs = sigar.getNetInterfaceList();
  62.         List netIfList = new ArrayList();
  63.         for ( String name:netIfs ) {
  64.             NetInterfaceData netIfData1 = NetInterfaceData.gather(sigar, name);
  65.             netIfList.add(netIfData1);
  66.         }
  67.         XStream xstream = new XStream();
  68.         xstream.alias("NetInterfaceDatas", List.class);
  69.         xstream.alias("NetInterfaceData", NetInterfaceData.class);
  70.         System.out.println(xstream.toXML(netIfList));
  71.     }
  72. }

输出:

[html][/html]

view plaincopy

  1. <NetInterfaceDatas>
  2.   <NetInterfaceData>
  3.     <config>
  4.       <name>eth0</name>
  5.       <hwaddr>00:FF:AE:0F:32:92</hwaddr>
  6.       <type>Ethernet</type>
  7.       <description>TAP-Win32 Adapter V9 #2 - ????ü??????ò?????</description>
  8.       <address>0.0.0.0</address>
  9.       <destination>0.0.0.0</destination>
  10.       <broadcast>255.255.255.255</broadcast>
  11.       <netmask>0.0.0.0</netmask>
  12.       <flags>2050</flags>
  13.       <mtu>1500</mtu>
  14.       <metric>0</metric>
  15.     </config>
  16.     <stat>
  17.       <rxBytes>0</rxBytes>
  18.       <rxPackets>0</rxPackets>
  19.       <rxErrors>0</rxErrors>
  20.       <rxDropped>0</rxDropped>
  21.       <rxOverruns>-1</rxOverruns>
  22.       <rxFrame>-1</rxFrame>
  23.       <txBytes>0</txBytes>
  24.       <txPackets>0</txPackets>
  25.       <txErrors>0</txErrors>
  26.       <txDropped>0</txDropped>
  27.       <txOverruns>-1</txOverruns>
  28.       <txCollisions>-1</txCollisions>
  29.       <txCarrier>-1</txCarrier>
  30.       <speed>10000000</speed>
  31.     </stat>
  32.     <rxbps>0</rxbps>
  33.     <txbps>0</txbps>
  34.   </NetInterfaceData>
  35.   <NetInterfaceData>
  36.     <config>
  37.       <name>eth1</name>
  38.       <hwaddr>00:FF:C8:0A:AE:37</hwaddr>
  39.       <type>Ethernet</type>
  40.       <description>eetrust VAdapter - ????ü??????ò?????</description>
  41.       <address>0.0.0.0</address>
  42.       <destination>0.0.0.0</destination>
  43.       <broadcast>255.255.255.255</broadcast>
  44.       <netmask>0.0.0.0</netmask>
  45.       <flags>2050</flags>
  46.       <mtu>1500</mtu>
  47.       <metric>0</metric>
  48.     </config>
  49.     <stat>
  50.       <rxBytes>0</rxBytes>
  51.       <rxPackets>0</rxPackets>
  52.       <rxErrors>0</rxErrors>
  53.       <rxDropped>0</rxDropped>
  54.       <rxOverruns>-1</rxOverruns>
  55.       <rxFrame>-1</rxFrame>
  56.       <txBytes>0</txBytes>
  57.       <txPackets>0</txPackets>
  58.       <txErrors>0</txErrors>
  59.       <txDropped>0</txDropped>
  60.       <txOverruns>-1</txOverruns>
  61.       <txCollisions>-1</txCollisions>
  62.       <txCarrier>-1</txCarrier>
  63.       <speed>10000000</speed>
  64.     </stat>
  65.     <rxbps>0</rxbps>
  66.     <txbps>0</txbps>
  67.   </NetInterfaceData>
  68.   <NetInterfaceData>
  69.     <config>
  70.       <name>eth2</name>
  71.       <hwaddr>00:22:68:5D:C8:52</hwaddr>
  72.       <type>Ethernet</type>
  73.       <description>Realtek PCIe GBE Family Controller - ????ü??????ò?????</description>
  74.       <address>10.3.43.63</address>
  75.       <destination>0.0.0.0</destination>
  76.       <broadcast>10.3.43.255</broadcast>
  77.       <netmask>255.255.255.0</netmask>
  78.       <flags>2115</flags>
  79.       <mtu>1500</mtu>
  80.       <metric>0</metric>
  81.     </config>
  82.     <stat>
  83.       <rxBytes>177337765</rxBytes>
  84.       <rxPackets>295449</rxPackets>
  85.       <rxErrors>0</rxErrors>
  86.       <rxDropped>0</rxDropped>
  87.       <rxOverruns>-1</rxOverruns>
  88.       <rxFrame>-1</rxFrame>
  89.       <txBytes>27724399</txBytes>
  90.       <txPackets>203267</txPackets>
  91.       <txErrors>0</txErrors>
  92.       <txDropped>0</txDropped>
  93.       <txOverruns>-1</txOverruns>
  94.       <txCollisions>-1</txCollisions>
  95.       <txCarrier>-1</txCarrier>
  96.       <speed>100000000</speed>
  97.     </stat>
  98.     <rxbps>2000</rxbps>
  99.     <txbps>2000</txbps>
  100.   </NetInterfaceData>
  101.   <NetInterfaceData>
  102.     <config>
  103.       <name>lo0</name>
  104.       <hwaddr>00:00:00:00:00:00</hwaddr>
  105.       <type>Local Loopback</type>
  106.       <description>MS TCP Loopback interface</description>
  107.       <address>127.0.0.1</address>
  108.       <destination>0.0.0.0</destination>
  109.       <broadcast>0.0.0.0</broadcast>
  110.       <netmask>255.0.0.0</netmask>
  111.       <flags>73</flags>
  112.       <mtu>1520</mtu>
  113.       <metric>0</metric>
  114.     </config>
  115.     <stat>
  116.       <rxBytes>36535999</rxBytes>
  117.       <rxPackets>350095</rxPackets>
  118.       <rxErrors>0</rxErrors>
  119.       <rxDropped>0</rxDropped>
  120.       <rxOverruns>-1</rxOverruns>
  121.       <rxFrame>-1</rxFrame>
  122.       <txBytes>36535999</txBytes>
  123.       <txPackets>350031</txPackets>
  124.       <txErrors>0</txErrors>
  125.       <txDropped>0</txDropped>
  126.       <txOverruns>-1</txOverruns>
  127.       <txCollisions>-1</txCollisions>
  128.       <txCarrier>-1</txCarrier>
  129.       <speed>10000000</speed>
  130.     </stat>
  131.     <rxbps>1000</rxbps>
  132.     <txbps>1000</txbps>
  133.   </NetInterfaceData>
  134. </NetInterfaceDatas>

下载地址:

本站下站sigar lib包

最新的Sigar官方包hyperic-sigar-1.6.4


本文固定链接: http://www.devba.com/index.php/archives/2770.html | 开发吧

报歉!评论已关闭.