Search in sources :

Example 1 with NetworkParams

use of oshi.software.os.NetworkParams 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

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