Sigar网络速度

3
这个库非常适合获取CPU、RAM和其他信息。然而,我无法弄清楚如何获取网络速度。也就是说,在一段时间内通过网络卡发送和接收的数据量有多少?有办法能给我这些数据吗?
这可能是我正在寻找的东西吗?
Tcp stat = sigar.getTcp();
stat.getInSegs() + " segments received"
stat.getOutSegs() + " segments send out"

如果Sigar没有提供解决方案,是否有其他库可供使用?
感谢您的帮助。
1个回答

3

输出:

{默认网关=193.171.36.1,主DNS=140.78.2.62,备用DNS=140.78.3.62,域名=,主机名=Schneider-PC}

{名称=eth0,描述=WAN Miniport(IPv6),度量=0,类型=以太网,地址=0.0.0.0,MTU=1500,子网掩码=0.0.0.0,标志=65,广播=0.0.0.0,硬件地址=34:F8:20:52:41:53,目标=0.0.0.0}

总接收(download):4.7K

总发送(upload):154


总接收(download):1.4K

总发送(upload):180

代码:

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

/**
 * @author Headgrowe
 * 
 */
public class NetworkData {

    static Map<String, Long> rxCurrentMap = new HashMap<String, Long>();
    static Map<String, List<Long>> rxChangeMap = new HashMap<String, List<Long>>();
    static Map<String, Long> txCurrentMap = new HashMap<String, Long>();
    static Map<String, List<Long>> txChangeMap = new HashMap<String, List<Long>>();
    private static Sigar sigar;

    /**
     * @throws InterruptedException
     * @throws SigarException
     * 
     */
    public NetworkData(Sigar s) throws SigarException, InterruptedException {
        sigar = s;
        getMetric();
        System.out.println(networkInfo());
        Thread.sleep(1000);     
    }

    public static void main(String[] args) throws SigarException,
            InterruptedException {
        new NetworkData(new Sigar());
        NetworkData.newMetrickThread();
    }

    public static String networkInfo() throws SigarException {
        String info = sigar.getNetInfo().toString();
        info += "\n"+ sigar.getNetInterfaceConfig().toString();
        return info;
    }
    
    public static String getDefaultGateway() throws SigarException {
        return sigar.getNetInfo().getDefaultGateway();
    }

    public static void newMetricThread() throws SigarException, InterruptedException {
        while (true) {
            Long[] m = getMetric();
            long totalrx = m[0];
            long totaltx = m[1];
            System.out.print("totalrx(download): ");
            System.out.println("\t" + Sigar.formatSize(totalrx));
            System.out.print("totaltx(upload): ");
            System.out.println("\t" + Sigar.formatSize(totaltx));
            System.out.println("-----------------------------------");
            Thread.sleep(1000);
        }

    }

    public static Long[] getMetric() throws SigarException {
        for (String ni : sigar.getNetInterfaceList()) {
            // System.out.println(ni);
            NetInterfaceStat netStat = sigar.getNetInterfaceStat(ni);
            NetInterfaceConfig ifConfig = sigar.getNetInterfaceConfig(ni);
            String hwaddr = null;
            if (!NetFlags.NULL_HWADDR.equals(ifConfig.getHwaddr())) {
                hwaddr = ifConfig.getHwaddr();
            }
            if (hwaddr != null) {
                long rxCurrenttmp = netStat.getRxBytes();
                saveChange(rxCurrentMap, rxChangeMap, hwaddr, rxCurrenttmp, ni);
                long txCurrenttmp = netStat.getTxBytes();
                saveChange(txCurrentMap, txChangeMap, hwaddr, txCurrenttmp, ni);
            }
        }
        long totalrx = getMetricData(rxChangeMap);
        long totaltx = getMetricData(txChangeMap);
        for (List<Long> l : rxChangeMap.values())
            l.clear();
        for (List<Long> l : txChangeMap.values())
            l.clear();
        return new Long[] { totalrx, totaltx };
    }

    private static long getMetricData(Map<String, List<Long>> rxChangeMap) {
        long total = 0;
        for (Entry<String, List<Long>> entry : rxChangeMap.entrySet()) {
            int average = 0;
            for (Long l : entry.getValue()) {
                average += l;
            }
            total += average / entry.getValue().size();
        }
        return total;
    }

    private static void saveChange(Map<String, Long> currentMap,
            Map<String, List<Long>> changeMap, String hwaddr, long current,
            String ni) {
        Long oldCurrent = currentMap.get(ni);
        if (oldCurrent != null) {
            List<Long> list = changeMap.get(hwaddr);
            if (list == null) {
                list = new LinkedList<Long>();
                changeMap.put(hwaddr, list);
            }
            list.add((current - oldCurrent));
        }
        currentMap.put(ni, current);
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接