Search in sources :

Example 6 with IpSubnet

use of org.graylog2.utilities.IpSubnet in project graylog2-server by Graylog2.

the class RestToolsTest method getRemoteAddrFromRequestWorksWithIPv6IfSubnetsContainsOnlyIPv6.

@Test
public void getRemoteAddrFromRequestWorksWithIPv6IfSubnetsContainsOnlyIPv6() throws Exception {
    final Request request = mock(Request.class);
    when(request.getRemoteAddr()).thenReturn("2001:DB8::42");
    when(request.getHeader("X-Forwarded-For")).thenReturn("2001:DB8::1:2:3:4:5:6");
    final String s = RestTools.getRemoteAddrFromRequest(request, Collections.singleton(new IpSubnet("2001:DB8::/32")));
    assertThat(s).isEqualTo("2001:DB8::1:2:3:4:5:6");
}
Also used : IpSubnet(org.graylog2.utilities.IpSubnet) Request(org.glassfish.grizzly.http.server.Request) Test(org.junit.Test)

Example 7 with IpSubnet

use of org.graylog2.utilities.IpSubnet 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

IpSubnet (org.graylog2.utilities.IpSubnet)7 Request (org.glassfish.grizzly.http.server.Request)4 Test (org.junit.Test)4 UnknownHostException (java.net.UnknownHostException)2 InterfaceAddress (java.net.InterfaceAddress)1 HashMap (java.util.HashMap)1 NetworkIF (oshi.hardware.NetworkIF)1 InternetProtocolStats (oshi.software.os.InternetProtocolStats)1 NetworkParams (oshi.software.os.NetworkParams)1