Sigar网速 [英] Sigar network speed

查看:261
本文介绍了Sigar网速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个lib对于获得cpu,ram和其他工作人员来说非常棒。但是,我无法弄清楚,如何获得网络速度。这意味着。在一段时间内发送和接收的数据量是多少?有没有办法给我这个数据?

this lib is kind of awesome to get cpu, ram and other staff. However, i can't figure out, how to get the network speed. that means. how much data is sended and received throw the network-cart in a amount of time? is there a way to give me this data?

这可能是我要搜索的内容吗?

is this probably what i am searching for?

Tcp stat = sigar.getTcp();
stat.getInSegs() + " segments received"
stat.getOutSegs() + " segments send out"

如果在sigar中没有办法,是否还有其他的lib?

if there is no way in sigar, is there a other lib to use?

感谢您的帮助

推荐答案

输出:


{DefaultGateway = 193.171.36.1,PrimaryDns = 140.78.2.62,SecondaryDns = 140.78.3.62,DomainName =,HostName = Schneider-PC}

{DefaultGateway=193.171.36.1, PrimaryDns=140.78.2.62,SecondaryDns=140.78.3.62, DomainName=, HostName=Schneider-PC}

{Name = eth0,Description = WAN Miniport(IPv6),Metric = 0 ,Type = Ethernet,Address = 0.0.0.0,Mtu = 1500,Netmask = 0.0.0.0,Flags = 65,Broadcast = 0.0.0.0,Hwaddr = 34:F8:20:52:41:53,Destination = 0.0.0.0 }

{Name=eth0, Description=WAN Miniport (IPv6), Metric=0, Type=Ethernet,Address=0.0.0.0, Mtu=1500, Netmask=0.0.0.0, Flags=65,Broadcast=0.0.0.0, Hwaddr=34:F8:20:52:41:53, Destination=0.0.0.0}

totalrx(下载):4.7K

totalrx(download): 4.7K

totaltx(上传):154

totaltx(upload): 154

totalrx(下载):1.4K

totalrx(download): 1.4K

totaltx(上传):180

totaltx(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);
    }

}

这篇关于Sigar网速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆