Search in sources :

Example 6 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project LogHub by fbacchella.

the class Geoip2 method processMessage.

@Override
public boolean processMessage(Event event, String field, String destination) throws ProcessorException {
    Object addr = event.get(field);
    InetAddress ipInfo = null;
    if (addr instanceof InetAddress) {
        ipInfo = (InetAddress) addr;
    } else if (addr instanceof String) {
        try {
            ipInfo = Helpers.parseIpAddres((String) addr);
            if (ipInfo == null) {
                throw event.buildException("can't read ip address " + addr);
            }
        } catch (UnknownHostException e) {
            throw event.buildException("can't read ip address " + addr, e);
        }
    }
    Country country = null;
    Country registred_country = null;
    Country represented_country = null;
    City city = null;
    Continent continent = null;
    Location location = null;
    Postal postal = null;
    List<Subdivision> subdivision = null;
    Map<String, Object> informations = new HashMap<>();
    try {
        switch(reader.getMetadata().getDatabaseType()) {
            case "GeoIP2-City":
            case "GeoLite2-City":
                {
                    CityResponse response = reader.city(ipInfo);
                    if (response == null) {
                        throw event.buildException("City not found for " + ipInfo.toString());
                    }
                    country = response.getCountry();
                    city = response.getCity();
                    continent = response.getContinent();
                    location = response.getLocation();
                    postal = response.getPostal();
                    registred_country = response.getRegisteredCountry();
                    represented_country = response.getRepresentedCountry();
                    subdivision = response.getSubdivisions();
                    break;
                }
            case "GeoIP2-Country":
            case "GeoLite2-Country":
                {
                    CountryResponse response = reader.country(ipInfo);
                    if (response == null) {
                        throw event.buildException("Country not found for " + ipInfo.toString());
                    }
                    country = response.getCountry();
                    continent = response.getContinent();
                    registred_country = response.getRegisteredCountry();
                    represented_country = response.getRepresentedCountry();
                    break;
                }
        }
    } catch (AddressNotFoundException e) {
        // not an error, just return a failure
        return false;
    } catch (IOException | GeoIp2Exception e) {
        throw event.buildException("can't read geoip database", e);
    }
    for (LocationType type : types) {
        switch(type) {
            case COUNTRY:
                if (country != null) {
                    Map<String, Object> infos = new HashMap<>(2);
                    Helpers.putNotEmpty(infos, "code", country.getIsoCode());
                    Helpers.putNotEmpty(infos, "name", country.getNames().get(locale));
                    if (infos.size() > 0) {
                        informations.put("country", infos);
                    }
                }
                break;
            case REPRESENTEDCOUNTRY:
                if (represented_country != null) {
                    Map<String, Object> infos = new HashMap<>(2);
                    Helpers.putNotEmpty(infos, "code", represented_country.getIsoCode());
                    Helpers.putNotEmpty(infos, "name", represented_country.getNames().get(locale));
                    if (infos.size() > 0) {
                        informations.put("represented_country", infos);
                    }
                }
                break;
            case REGISTREDCOUNTRY:
                if (registred_country != null) {
                    Map<String, Object> infos = new HashMap<>(2);
                    Helpers.putNotEmpty(infos, "code", registred_country.getIsoCode());
                    Helpers.putNotEmpty(infos, "name", registred_country.getNames().get(locale));
                    if (infos.size() > 0) {
                        informations.put("registred_country", infos);
                    }
                }
                break;
            case CITY:
                {
                    if (city != null) {
                        Helpers.putNotEmpty(informations, "city", city.getNames().get(locale));
                    }
                    break;
                }
            case LOCATION:
                Map<String, Object> infos = new HashMap<>(10);
                if (location != null) {
                    Helpers.putNotEmpty(infos, "latitude", location.getLatitude());
                    Helpers.putNotEmpty(infos, "longitude", location.getLongitude());
                    Helpers.putNotEmpty(infos, "timezone", location.getTimeZone());
                    Helpers.putNotEmpty(infos, "accuray_radius", location.getAccuracyRadius());
                    Helpers.putNotEmpty(infos, "metro_code", location.getMetroCode());
                    Helpers.putNotEmpty(infos, "average_income", location.getAverageIncome());
                    Helpers.putNotEmpty(infos, "population_density", location.getPopulationDensity());
                    if (infos.size() > 0) {
                        informations.put("location", infos);
                    }
                }
            case CONTINENT:
                if (continent != null) {
                    Helpers.putNotEmpty(informations, "continent", continent.getNames().get(locale));
                }
                break;
            case POSTAL:
                if (postal != null) {
                    Helpers.putNotEmpty(informations, "postal", postal.getCode());
                }
                break;
            case SUBDIVISION:
                if (subdivision != null) {
                    List<Map<String, Object>> all = new ArrayList<>(subdivision.size());
                    for (Subdivision sub : subdivision) {
                        Map<String, Object> subdivisioninfo = new HashMap<>(2);
                        Helpers.putNotEmpty(subdivisioninfo, "code", sub.getIsoCode());
                        Helpers.putNotEmpty(subdivisioninfo, "name", sub.getNames().get(locale));
                        if (subdivisioninfo.size() > 0) {
                            all.add(subdivisioninfo);
                        }
                    }
                    if (all.size() > 0) {
                        informations.put("subdivisions", all);
                    }
                }
                break;
        }
    }
    event.put(destination, informations);
    return true;
}
Also used : UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) City(com.maxmind.geoip2.record.City) Subdivision(com.maxmind.geoip2.record.Subdivision) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) CityResponse(com.maxmind.geoip2.model.CityResponse) Continent(com.maxmind.geoip2.record.Continent) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) Country(com.maxmind.geoip2.record.Country) CountryResponse(com.maxmind.geoip2.model.CountryResponse) InetAddress(java.net.InetAddress) HashMap(java.util.HashMap) Map(java.util.Map) Postal(com.maxmind.geoip2.record.Postal) Location(com.maxmind.geoip2.record.Location)

Example 7 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project bender by Nextdoor.

the class GeoIpOperation method perform.

@Override
public InternalEvent perform(InternalEvent ievent) {
    String ipStr = null;
    /*
     * Get field containing an IP address
     */
    try {
        ipStr = ievent.getEventObj().getField(this.pathToIpAddress);
    } catch (NoSuchElementException e) {
        if (!this.required) {
            return ievent;
        }
        throw new OperationException("ip address field " + this.pathToIpAddress + " does not exist");
    }
    if (ipStr == null) {
        if (!this.required) {
            return ievent;
        }
        throw new OperationException("ip address field " + this.pathToIpAddress + " was null");
    }
    /*
     * Sometimes the field contains comma separated ip addresses (ie forwarded web requests). In
     * this case pick the first value in the list which is typically the user.
     */
    if (!ipStr.isEmpty() && ipStr.contains(",")) {
        ipStr = ipStr.split(",")[0];
    }
    InetAddress ipAddress = null;
    try {
        ipAddress = InetAddress.getByName(ipStr);
    } catch (UnknownHostException e) {
        if (!this.required) {
            return ievent;
        }
        throw new OperationException(e);
    }
    if (ipAddress == null) {
        if (!this.required) {
            return ievent;
        }
        throw new OperationException("ip address " + ipStr + " did not resolve");
    }
    CityResponse response = null;
    try {
        response = this.databaseReader.city(ipAddress);
    } catch (IOException | GeoIp2Exception e) {
        if (!this.required) {
            return ievent;
        }
        throw new OperationException(e);
    }
    HashMap<String, Object> geo = new HashMap<String, Object>(1);
    for (GeoProperty property : this.geoProperties) {
        switch(property) {
            case COUNTRY_NAME:
                if (response.getCountry() == null) {
                    if (!this.required) {
                        return ievent;
                    }
                    throw new OperationException("country returned null");
                }
                geo.put("country_name", response.getCountry().getName());
                break;
            case COUNTRY_ISO_CODE:
                if (response.getCountry() == null) {
                    if (!this.required) {
                        return ievent;
                    }
                    throw new OperationException("country returned null");
                }
                geo.put("country_iso_code", response.getCountry().getIsoCode());
                break;
            case SUBDIVISION_NAME:
                if (response.getMostSpecificSubdivision() == null) {
                    if (!this.required) {
                        return ievent;
                    }
                    throw new OperationException("MostSpecificSubdivision returned null");
                }
                geo.put("subdivision_name", response.getMostSpecificSubdivision().getName());
                break;
            case SUBDIVISION_ISO_CODE:
                if (response.getMostSpecificSubdivision() == null) {
                    if (!this.required) {
                        return ievent;
                    }
                    throw new OperationException("MostSpecificSubdivision returned null");
                }
                geo.put("subdivision_iso_code", response.getMostSpecificSubdivision().getIsoCode());
                break;
            case CITY_NAME:
                if (response.getCity() == null) {
                    if (!this.required) {
                        return ievent;
                    }
                    throw new OperationException("city returned null");
                }
                geo.put("city_name", response.getCity().getName());
                break;
            case POSTAL_CODE:
                if (response.getPostal() == null) {
                    if (!this.required) {
                        return ievent;
                    }
                    throw new OperationException("postal returned null");
                }
                geo.put("postal_code", response.getPostal().getCode());
                break;
            case LOCATION:
                if (response.getLocation() == null) {
                    if (!this.required) {
                        return ievent;
                    }
                    throw new OperationException("location returned null");
                }
                Double lat = response.getLocation().getLatitude();
                Double lon = response.getLocation().getLongitude();
                if (lat == null || lon == null) {
                    if (!this.required) {
                        return ievent;
                    }
                    throw new OperationException("error getting lat/lon");
                }
                HashMap<String, Object> location = new HashMap<String, Object>(2);
                location.put("lat", lat);
                location.put("lon", lon);
                geo.put("location", location);
                break;
        }
    }
    ievent.getEventObj().setField(this.destFieldName, geo);
    return ievent;
}
Also used : UnknownHostException(java.net.UnknownHostException) HashMap(java.util.HashMap) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) CityResponse(com.maxmind.geoip2.model.CityResponse) InetAddress(java.net.InetAddress) GeoProperty(com.nextdoor.bender.operations.geo.GeoIpOperationConfig.GeoProperty) NoSuchElementException(java.util.NoSuchElementException) OperationException(com.nextdoor.bender.operation.OperationException)

Example 8 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project graylog2-server by Graylog2.

the class MaxMindIpLocationResolver method doGetGeoIpData.

@Override
public Optional<GeoLocationInformation> doGetGeoIpData(InetAddress address) {
    GeoLocationInformation info;
    try (Timer.Context ignored = resolveTime.time()) {
        final CityResponse response = databaseReader.city(address);
        final Location location = response.getLocation();
        final Country country = response.getCountry();
        final City city = response.getCity();
        info = GeoLocationInformation.create(location.getLatitude(), location.getLongitude(), country.getGeoNameId() == null ? "N/A" : country.getIsoCode(), country.getGeoNameId() == null ? "N/A" : country.getName(), // calling to .getName() may throw a NPE
        city.getGeoNameId() == null ? "N/A" : city.getName(), "N/A", "N/A");
    } catch (IOException | GeoIp2Exception | UnsupportedOperationException e) {
        info = null;
        if (!(e instanceof AddressNotFoundException)) {
            LOG.debug("Could not get location from IP {}", address.getHostAddress(), e);
            lastError = e.getMessage();
        }
    }
    return Optional.ofNullable(info);
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) Timer(com.codahale.metrics.Timer) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) Country(com.maxmind.geoip2.record.Country) City(com.maxmind.geoip2.record.City) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) Location(com.maxmind.geoip2.record.Location)

Example 9 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project graylog2-server by Graylog2.

the class MaxMindIpAsnResolver method doGetGeoIpData.

@Override
protected Optional<GeoAsnInformation> doGetGeoIpData(InetAddress address) {
    GeoAsnInformation asn;
    try (Timer.Context ignored = resolveTime.time()) {
        AsnResponse response = databaseReader.asn(address);
        String number = response.getAutonomousSystemNumber() == null ? "N/A" : response.getAutonomousSystemNumber().toString();
        asn = GeoAsnInformation.create(response.getAutonomousSystemOrganization(), "N/A", number);
    } catch (GeoIp2Exception | IOException | UnsupportedOperationException e) {
        asn = null;
        if (!(e instanceof AddressNotFoundException)) {
            String error = String.format(Locale.US, "Error getting ASN for IP Address '%s'. %s", address, e.getMessage());
            LOG.warn(error, e);
            lastError = e.getMessage();
        }
    }
    return Optional.ofNullable(asn);
}
Also used : Timer(com.codahale.metrics.Timer) AsnResponse(com.maxmind.geoip2.model.AsnResponse) AddressNotFoundException(com.maxmind.geoip2.exception.AddressNotFoundException) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception)

Example 10 with GeoIp2Exception

use of com.maxmind.geoip2.exception.GeoIp2Exception in project druid by druid-io.

the class WikipediaIrcDecoder method decodeMessage.

@Override
public InputRow decodeMessage(final DateTime timestamp, String channel, String msg) {
    final Map<String, String> dimensions = Maps.newHashMap();
    final Map<String, Float> metrics = Maps.newHashMap();
    Matcher m = pattern.matcher(msg);
    if (!m.matches()) {
        throw new IllegalArgumentException("Invalid input format");
    }
    Matcher shortname = shortnamePattern.matcher(channel);
    if (shortname.matches()) {
        dimensions.put("language", shortname.group(1));
    }
    String page = m.group(1);
    String pageUrl = page.replaceAll("\\s", "_");
    dimensions.put("page", pageUrl);
    String user = m.group(4);
    Matcher ipMatch = ipPattern.matcher(user);
    boolean anonymous = ipMatch.matches();
    if (anonymous) {
        try {
            final InetAddress ip = InetAddress.getByName(ipMatch.group());
            final Omni lookup = geoLookup.omni(ip);
            dimensions.put("continent", lookup.getContinent().getName());
            dimensions.put("country", lookup.getCountry().getName());
            dimensions.put("region", lookup.getMostSpecificSubdivision().getName());
            dimensions.put("city", lookup.getCity().getName());
        } catch (UnknownHostException e) {
            log.error(e, "invalid ip [%s]", ipMatch.group());
        } catch (IOException e) {
            log.error(e, "error looking up geo ip");
        } catch (GeoIp2Exception e) {
            log.error(e, "error looking up geo ip");
        }
    }
    dimensions.put("user", user);
    final String flags = m.group(2);
    dimensions.put("unpatrolled", Boolean.toString(flags.contains("!")));
    dimensions.put("newPage", Boolean.toString(flags.contains("N")));
    dimensions.put("robot", Boolean.toString(flags.contains("B")));
    dimensions.put("anonymous", Boolean.toString(anonymous));
    String[] parts = page.split(":");
    if (parts.length > 1 && !parts[1].startsWith(" ")) {
        Map<String, String> channelNamespaces = namespaces.get(channel);
        if (channelNamespaces != null && channelNamespaces.containsKey(parts[0])) {
            dimensions.put("namespace", channelNamespaces.get(parts[0]));
        } else {
            dimensions.put("namespace", "wikipedia");
        }
    } else {
        dimensions.put("namespace", "article");
    }
    float delta = m.group(6) != null ? Float.parseFloat(m.group(6)) : 0;
    metrics.put("delta", delta);
    metrics.put("added", Math.max(delta, 0));
    metrics.put("deleted", Math.min(delta, 0));
    return new InputRow() {

        @Override
        public List<String> getDimensions() {
            return dimensionList;
        }

        @Override
        public long getTimestampFromEpoch() {
            return timestamp.getMillis();
        }

        @Override
        public DateTime getTimestamp() {
            return timestamp;
        }

        @Override
        public List<String> getDimension(String dimension) {
            final String value = dimensions.get(dimension);
            if (value != null) {
                return ImmutableList.of(value);
            } else {
                return ImmutableList.of();
            }
        }

        @Override
        public Object getRaw(String dimension) {
            return dimensions.get(dimension);
        }

        @Override
        public float getFloatMetric(String metric) {
            return metrics.get(metric);
        }

        @Override
        public long getLongMetric(String metric) {
            return new Float(metrics.get(metric)).longValue();
        }

        @Override
        public int compareTo(Row o) {
            return timestamp.compareTo(o.getTimestamp());
        }

        @Override
        public String toString() {
            return "WikipediaRow{" + "timestamp=" + timestamp + ", dimensions=" + dimensions + ", metrics=" + metrics + '}';
        }
    };
}
Also used : UnknownHostException(java.net.UnknownHostException) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) GeoIp2Exception(com.maxmind.geoip2.exception.GeoIp2Exception) InputRow(io.druid.data.input.InputRow) InputRow(io.druid.data.input.InputRow) Row(io.druid.data.input.Row) Omni(com.maxmind.geoip2.model.Omni) InetAddress(java.net.InetAddress)

Aggregations

CityResponse (com.maxmind.geoip2.model.CityResponse)10 InetAddress (java.net.InetAddress)10 GeoIp2Exception (com.maxmind.geoip2.exception.GeoIp2Exception)9 IOException (java.io.IOException)9 City (com.maxmind.geoip2.record.City)4 Country (com.maxmind.geoip2.record.Country)4 Location (com.maxmind.geoip2.record.Location)4 Subdivision (com.maxmind.geoip2.record.Subdivision)4 HashMap (java.util.HashMap)4 AddressNotFoundException (com.maxmind.geoip2.exception.AddressNotFoundException)3 CountryResponse (com.maxmind.geoip2.model.CountryResponse)3 Continent (com.maxmind.geoip2.record.Continent)3 Postal (com.maxmind.geoip2.record.Postal)3 UnknownHostException (java.net.UnknownHostException)3 Timer (com.codahale.metrics.Timer)2 User (com.earth2me.essentials.User)2 DatabaseReader (com.maxmind.geoip2.DatabaseReader)2 IspResponse (com.maxmind.geoip2.model.IspResponse)2 RepresentedCountry (com.maxmind.geoip2.record.RepresentedCountry)2 FlowFile (org.apache.nifi.flowfile.FlowFile)2