org.hyperic.sigar.NetInterfaceStat Java Examples

The following examples show how to use org.hyperic.sigar.NetInterfaceStat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: sigar.java    From sigar-system_runtime with MIT License 6 votes vote down vote up
private static void net() throws Exception {
    Sigar sigar = new Sigar();
    String ifNames[] = sigar.getNetInterfaceList();
    for (int i = 0; i < ifNames.length; i++) {
        String name = ifNames[i];
        NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
        System.out.println("网络设备名:    " + name);// 网络设备名
        System.out.println("IP地址:    " + ifconfig.getAddress());// IP地址
        System.out.println("子网掩码:    " + ifconfig.getNetmask());// 子网掩码
        if ((ifconfig.getFlags() & 1L) <= 0L) {
            System.out.println("!IFF_UP...skipping getNetInterfaceStat");
            continue;
        }
        NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
        
        System.out.println(name + "接收的总包裹数:" + ifstat.getRxPackets());// 接收的总包裹数
        System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());// 发送的总包裹数
        System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());// 接收到的总字节数
        System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());// 发送的总字节数
        System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());// 接收到的错误包数
        System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());// 发送数据包时的错误数
        System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());// 接收时丢弃的包数
        System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());// 发送时丢弃的包数
    }
}
 
Example #2
Source File: NetworkUtilizationSampler.java    From kieker with Apache License 2.0 6 votes vote down vote up
private NetworkStatistic getCurrentNetworkStatistic(final ITimeSource timesource, final String interfaceName) throws SigarException {
	final long currentTimestamp = timesource.getTime();
	final NetInterfaceStat interfaceStat = this.sigar.getNetInterfaceStat(interfaceName);
	final long speed = interfaceStat.getSpeed();
	final long txBytes = interfaceStat.getTxBytes();
	final long txCarrier = interfaceStat.getTxCarrier();
	final long txCollisions = interfaceStat.getTxCollisions();
	final long txDropped = interfaceStat.getTxDropped();
	final long txErrors = interfaceStat.getTxErrors();
	final long txOverruns = interfaceStat.getTxOverruns();
	final long txPackets = interfaceStat.getTxPackets();
	final long rxBytes = interfaceStat.getRxBytes();
	final long rxDropped = interfaceStat.getRxDropped();
	final long rxErrors = interfaceStat.getRxErrors();
	final long rxFrame = interfaceStat.getRxFrame();
	final long rxOverruns = interfaceStat.getRxOverruns();
	final long rxPackets = interfaceStat.getRxPackets();

	return new NetworkStatistic(currentTimestamp, speed, txBytes, txCarrier, txCollisions, txDropped, txErrors, txOverruns, txPackets, rxBytes, rxDropped,
			rxErrors, rxFrame, rxOverruns, rxPackets);
}
 
Example #3
Source File: PerformanceService.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
/**
 * getNetSpeed.
 * 
 * @return
 */
public Map<String, Long> getNetSpeed()
        throws UnknownHostException, SigarException, InterruptedException {
    Map<String, Long> map = new HashMap<String, Long>();
    String ip = getIp();

    String[] ifNames = sigar.getNetInterfaceList();
    long rxbps = 0;
    long txbps = 0;
    for (int i = 0; i < ifNames.length; i++) {
        String name = ifNames[i];
        NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
        if (ifconfig.getAddress().equals(ip)) {
            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) / 1024 / 8;
            txbps = ((txBytesEnd - txBytesStart) * 8 / (end - start) * 1000) / 1024 / 8;
            break;
        }
    }
    map.put(RXBPS, rxbps);
    map.put(TXBPS, txbps);
    return map;
}
 
Example #4
Source File: SystemRuntime.java    From sigar-system_runtime with MIT License 5 votes vote down vote up
public NetInterfaceStat netBytes(String ip) throws Exception {
        String ifNames[] = sigar.getNetInterfaceList();
        NetInterfaceStat result = null;
        for (int i = 0; i < ifNames.length; i++) {
            String name = ifNames[i];
            NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
            if(ifconfig.getAddress().equals(ip)){
                result = sigar.getNetInterfaceStat(name);
                break;
            }
//            System.out.println("网络设备名:    " + name);// 网络设备名
//            System.out.println("IP地址:    " + ifconfig.getAddress());// IP地址
//            System.out.println("子网掩码:    " + ifconfig.getNetmask());// 子网掩码
//            if ((ifconfig.getFlags() & 1L) <= 0L) {
//                System.out.println("!IFF_UP...skipping getNetInterfaceStat");
//                continue;
//            }
//            NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
//            
//            System.out.println(name + "接收的总包裹数:" + ifstat.getRxPackets());// 接收的总包裹数
//            System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());// 发送的总包裹数
//            System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());// 接收到的总字节数
//            System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());// 发送的总字节数
//            System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());// 接收到的错误包数
//            System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());// 发送数据包时的错误数
//            System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());// 接收时丢弃的包数
//            System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());// 发送时丢弃的包数
        }
        return result;
    }
 
Example #5
Source File: NetInterfaceInfo.java    From maintain with MIT License 4 votes vote down vote up
public NetInterfaceInfo(String name, NetInterfaceConfig netInterfaceConfig, NetInterfaceStat netInterfaceStat) {
	this.name = name;
	this.netInterfaceConfig = netInterfaceConfig;
	this.netInterfaceStat = netInterfaceStat;
}
 
Example #6
Source File: NetInterfaceInfo.java    From maintain with MIT License 4 votes vote down vote up
public NetInterfaceStat getNetInterfaceStat() {
	return netInterfaceStat;
}
 
Example #7
Source File: NetInterfaceInfo.java    From maintain with MIT License 4 votes vote down vote up
public void setNetInterfaceStat(NetInterfaceStat netInterfaceStat) {
	this.netInterfaceStat = netInterfaceStat;
}
 
Example #8
Source File: NetworkIOMetric.java    From perfmon-agent with Apache License 2.0 4 votes vote down vote up
public void getValue(StringBuffer res) throws SigarException {
    double val = 0;
    double cur;
    int factor = 1;
    for (int n = 0; n < interfaces.length; n++) {
        NetInterfaceStat usage;
        try {
            usage = sigarProxy.getNetInterfaceStat(interfaces[n]);
        } catch (SigarException e) {
            log.error("Failed to get interface stat: " + interfaces[n], e);
            continue;
        }
        switch (type) {
            case RX_BYTES:
                val += usage.getRxBytes();
                factor = dividingFactor;
                break;
            case RX_DROPPED:
                val += usage.getRxDropped();
                break;
            case RX_ERRORS:
                val += usage.getRxErrors();
                break;
            case RX_FRAME:
                val += usage.getRxFrame();
                break;
            case RX_OVERRUNS:
                val += usage.getRxOverruns();
                break;
            case RX_PACKETS:
                val += usage.getRxPackets();
                break;
            case TX_BYTES:
                val += usage.getTxBytes();
                factor = dividingFactor;
                break;
            case TX_CARRIER:
                val += usage.getTxCarrier();
                break;
            case TX_COLLISIONS:
                val += usage.getTxCollisions();
                break;
            case TX_DROPPED:
                val += usage.getTxDropped();
                break;
            case TX_ERRORS:
                val += usage.getTxErrors();
                break;
            case TX_OVERRUNS:
                val += usage.getTxOverruns();
                break;
            case USED:
                val = usage.getTxPackets();
                break;
            case SPEED:
                val = usage.getSpeed();
                break;
            case TX_PACKETS:
                val += usage.getTxPackets();
                break;
            default:
                throw new SigarException("Unknown net io type " + type);
        }
    }

    // some post-processing
    switch (type) {
        case SPEED:
            break;
        case USED:
            break;
        default:
            cur = val;
            val = prev > 0 ? cur - prev : 0;
            prev = cur;
    }
    val = val / factor;
    res.append(Double.toString(val));
}