0
0
Fork 0
mirror of https://github.com/alerta/alerta-contrib.git synced 2025-03-18 06:12:51 +00:00
alerta_alerta-contrib/plugins/geoip/alerta_geoip.py

41 lines
1.1 KiB
Python
Raw Normal View History

2015-01-30 15:22:43 +00:00
import os
2015-01-30 15:22:43 +00:00
import requests
2016-09-15 09:16:11 +00:00
import logging
2015-01-30 15:22:43 +00:00
from alerta.app import app
from alerta.plugins import PluginBase
2016-09-15 09:16:11 +00:00
LOG = logging.getLogger('alerta.plugins.geoip')
2015-01-30 15:22:43 +00:00
GEOIP_URL = os.environ.get('GEOIP_URL') or app.config.get('GEOIP_URL', 'http://freegeoip.net/json')
2015-01-30 15:22:43 +00:00
class GeoLocation(PluginBase):
def pre_receive(self, alert):
2016-11-20 21:59:18 +00:00
ip_addr = alert.attributes['ip'].split(', ')[0]
LOG.debug("GeoIP lookup for IP: %s", ip_addr)
2015-01-30 15:52:19 +00:00
if 'ip' in alert.attributes:
2016-11-20 21:59:18 +00:00
url = '%s/%s' % (GEOIP_URL, ip_addr)
2015-01-30 15:52:19 +00:00
else:
2016-09-15 09:16:11 +00:00
LOG.warning("IP address must be included as an alert attribute.")
2015-01-30 15:52:19 +00:00
raise RuntimeWarning("IP address must be included as an alert attribute.")
2015-01-30 15:22:43 +00:00
r = requests.get(url, headers={'Content-type': 'application/json'}, timeout=2)
try:
2016-11-20 21:59:18 +00:00
alert.attributes['geoip'] = r.json()
2015-01-30 15:22:43 +00:00
except Exception as e:
2016-09-15 09:16:11 +00:00
LOG.error("GeoIP lookup failed: %s" % str(e))
2015-01-30 15:22:43 +00:00
raise RuntimeError("GeoIP lookup failed: %s" % str(e))
return alert
def post_receive(self, alert):
return
2015-01-30 15:22:43 +00:00
def status_change(self, alert, status, text):
return