Search in sources :

Example 1 with NetworkIF

use of oshi.hardware.NetworkIF in project flink by apache.

the class SystemResourcesCounter method calculateNetworkUsage.

private void calculateNetworkUsage(NetworkIF[] networkIFs) {
    checkState(networkIFs.length == receiveRatePerInterface.length());
    for (int i = 0; i < networkIFs.length; i++) {
        NetworkIF networkIF = networkIFs[i];
        networkIF.updateNetworkStats();
        receiveRatePerInterface.set(i, (networkIF.getBytesRecv() - bytesReceivedPerInterface[i]) * 1000 / probeIntervalMs);
        sendRatePerInterface.set(i, (networkIF.getBytesSent() - bytesSentPerInterface[i]) * 1000 / probeIntervalMs);
        bytesReceivedPerInterface[i] = networkIF.getBytesRecv();
        bytesSentPerInterface[i] = networkIF.getBytesSent();
    }
}
Also used : NetworkIF(oshi.hardware.NetworkIF)

Example 2 with NetworkIF

use of oshi.hardware.NetworkIF in project ats-framework by Axway.

the class OshiSystemInformation method listNetworkInterface.

@Override
public String[] listNetworkInterface() {
    List<NetworkIF> networkInterfaces = this.hal.getNetworkIFs();
    List<String> ifNames = new ArrayList<String>();
    for (NetworkIF networkInterface : networkInterfaces) {
        ifNames.add(networkInterface.getName());
    // or ifNames.add(networkInterface.getDisplayName());
    }
    return ifNames.toArray(new String[ifNames.size()]);
}
Also used : ArrayList(java.util.ArrayList) NetworkIF(oshi.hardware.NetworkIF)

Example 3 with NetworkIF

use of oshi.hardware.NetworkIF in project graylog2-server by Graylog2.

the class OshiNetworkProbe method networkStats.

@Override
public NetworkStats networkStats() {
    String primaryInterface = "";
    final NetworkParams networkParams = service.getOs().getNetworkParams();
    final String defaultGateway = networkParams.getIpv4DefaultGateway();
    Map<String, NetworkStats.Interface> ifaces = new HashMap<>();
    for (NetworkIF it : service.getHal().getNetworkIFs()) {
        if (Strings.isNotBlank(defaultGateway)) {
            for (InterfaceAddress that : it.queryNetworkInterface().getInterfaceAddresses()) {
                try {
                    final IpSubnet ipSubnet = new IpSubnet(that.getAddress().getHostAddress() + "/" + that.getNetworkPrefixLength());
                    if (ipSubnet.contains(defaultGateway)) {
                        primaryInterface = it.getName();
                    }
                } catch (UnknownHostException e) {
                    LOG.warn("Couldn't find primary interface", e);
                }
            }
        }
        NetworkStats.Interface anInterface = NetworkStats.Interface.create(it.getName(), it.queryNetworkInterface().getInterfaceAddresses().stream().map(address -> address.getAddress().getHostAddress()).collect(Collectors.toSet()), it.getMacaddr(), it.getMTU(), NetworkStats.InterfaceStats.create(it.getPacketsRecv(), it.getInErrors(), it.getInDrops(), -1, -1, it.getPacketsSent(), it.getOutErrors(), -1, -1, -1, it.getCollisions(), it.getBytesRecv(), it.getBytesSent()));
        ifaces.putIfAbsent(anInterface.name(), anInterface);
    }
    final InternetProtocolStats stats = service.getOs().getInternetProtocolStats();
    final InternetProtocolStats.TcpStats ipv4Stats = stats.getTCPv4Stats();
    final InternetProtocolStats.TcpStats ipv6Stats = stats.getTCPv6Stats();
    final NetworkStats.TcpStats tcpStats = NetworkStats.TcpStats.create(ipv4Stats.getConnectionsActive() + ipv6Stats.getConnectionsActive(), ipv4Stats.getConnectionsPassive() + ipv6Stats.getConnectionsPassive(), ipv4Stats.getConnectionFailures() + ipv6Stats.getConnectionFailures(), ipv4Stats.getConnectionsReset() + ipv6Stats.getConnectionsReset(), ipv4Stats.getConnectionsEstablished() + ipv6Stats.getConnectionsEstablished(), ipv4Stats.getSegmentsReceived() + ipv6Stats.getSegmentsReceived(), ipv4Stats.getSegmentsSent() + ipv6Stats.getSegmentsSent(), ipv4Stats.getSegmentsRetransmitted() + ipv6Stats.getSegmentsRetransmitted(), ipv4Stats.getInErrors() + ipv6Stats.getInErrors(), ipv4Stats.getOutResets() + ipv6Stats.getOutResets());
    return NetworkStats.create(primaryInterface, ifaces, tcpStats);
}
Also used : UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) InterfaceAddress(java.net.InterfaceAddress) NetworkIF(oshi.hardware.NetworkIF) IpSubnet(org.graylog2.utilities.IpSubnet) InternetProtocolStats(oshi.software.os.InternetProtocolStats) NetworkParams(oshi.software.os.NetworkParams)

Aggregations

NetworkIF (oshi.hardware.NetworkIF)3 InterfaceAddress (java.net.InterfaceAddress)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IpSubnet (org.graylog2.utilities.IpSubnet)1 InternetProtocolStats (oshi.software.os.InternetProtocolStats)1 NetworkParams (oshi.software.os.NetworkParams)1