Handle AddressNotFoundError in nodemapper.py (#25)

pull/26/head
Max.-F. Helm 6 months ago committed by GitHub
parent e4c9eaf677
commit fd41033055
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -12,6 +12,7 @@ from os import environ as env
import requests import requests
import geoip2.database import geoip2.database
from geoip2.errors import AddressNotFoundError
from flask import Flask, make_response from flask import Flask, make_response
@ -24,8 +25,11 @@ NODE_PORT = env.get('NODE_PORT', 18083)
def get_geoip(ip): def get_geoip(ip):
"""Takes an IP address and determines GeoIP data""" """Takes an IP address and determines GeoIP data"""
with geoip2.database.Reader("./geoip.mmdb") as reader: try:
return reader.city(ip) with geoip2.database.Reader("./geoip.mmdb") as reader:
return reader.city(ip)
except AddressNotFoundError:
return None
@app.route("/metrics") @app.route("/metrics")
@ -35,6 +39,9 @@ def nodes():
peer_list = requests.get(f'http://{NODE_HOST}:{NODE_PORT}/get_peer_list').json() peer_list = requests.get(f'http://{NODE_HOST}:{NODE_PORT}/get_peer_list').json()
def add_peer(host, status): def add_peer(host, status):
geo = get_geoip(host) geo = get_geoip(host)
if geo is None:
return
geostr = 'geoip{{latitude="{lat}", longitude="{lon}", country_code="{country_code}", country_name="{country_name}", status="{status}"}} 1' geostr = 'geoip{{latitude="{lat}", longitude="{lon}", country_code="{country_code}", country_name="{country_name}", status="{status}"}} 1'
if geostr not in peers: if geostr not in peers:
if 'en' in geo.continent.names: if 'en' in geo.continent.names:
@ -54,4 +61,4 @@ def nodes():
data = '\n'.join(peers) data = '\n'.join(peers)
response = make_response(data, 200) response = make_response(data, 200)
response.mimetype = "text/plain" response.mimetype = "text/plain"
return response return response

Loading…
Cancel
Save