Compare commits
1 commit
master
...
geoip-upda
Author | SHA1 | Date | |
---|---|---|---|
![]() |
3c2268be94 |
2 changed files with 9 additions and 4 deletions
plugins/geoip
|
@ -11,7 +11,8 @@ from alerta.plugins import PluginBase
|
|||
|
||||
LOG = logging.getLogger('alerta.plugins.geoip')
|
||||
|
||||
GEOIP_URL = os.environ.get('GEOIP_URL') or app.config.get('GEOIP_URL', 'http://freegeoip.net/json')
|
||||
GEOIP_URL = os.environ.get('GEOIP_URL') or app.config.get('GEOIP_URL', 'http://api.ipstack.com')
|
||||
GEOIP_ACCESS_KEY = os.environ.get('GEOIP_ACCESS_KEY') or app.config.get('GEOIP_ACCESS_KEY', None)
|
||||
|
||||
|
||||
class GeoLocation(PluginBase):
|
||||
|
@ -22,14 +23,18 @@ class GeoLocation(PluginBase):
|
|||
LOG.debug("GeoIP lookup for IP: %s", ip_addr)
|
||||
|
||||
if 'ip' in alert.attributes:
|
||||
url = '%s/%s' % (GEOIP_URL, ip_addr)
|
||||
url = '%s/%s?access_key=%s' % (GEOIP_URL, ip_addr, GEOIP_ACCESS_KEY)
|
||||
else:
|
||||
LOG.warning("IP address must be included as an alert attribute.")
|
||||
raise RuntimeWarning("IP address must be included as an alert attribute.")
|
||||
|
||||
r = requests.get(url, headers={'Content-type': 'application/json'}, timeout=2)
|
||||
try:
|
||||
alert.attributes['geoip'] = r.json()
|
||||
geoip_lookup = r.json()
|
||||
alert.attributes = {
|
||||
'geoip': geoip_lookup,
|
||||
'country': geoip_lookup['location'].get('country_flag_emoji')
|
||||
}
|
||||
except Exception as e:
|
||||
LOG.error("GeoIP lookup failed: %s" % str(e))
|
||||
raise RuntimeError("GeoIP lookup failed: %s" % str(e))
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
version = '5.3.2'
|
||||
version = '5.4.0'
|
||||
|
||||
setup(
|
||||
name="alerta-geoip",
|
||||
|
|
Loading…
Add table
Reference in a new issue