
import requests

from alerta.app import app
from alerta.plugins import PluginBase

LOG = app.logger

GEOIP_URL = 'http://freegeoip.net/json'


class GeoLocation(PluginBase):

    def pre_receive(self, alert):

        url = '%s/%s' % (GEOIP_URL, alert.attributes['ip'])

        r = requests.get(url, headers={'Content-type': 'application/json'}, timeout=2)
        try:
            alert.attributes.update(r.json())
        except Exception as e:
            raise RuntimeError("GeoIP lookup failed: %s" % str(e))

        return alert

    def post_receive(self, alert):
        pass


@@ -200,24 +200,65 @@
 f, alert):%0A%0A
+        if 'ip' in alert.attributes:%0A    
         url 
@@ -304,16 +304,117 @@
 s%5B'ip'%5D)
+%0A        else:%0A            raise RuntimeWarning(%22IP address must be included as an alert attribute.%22)
 %0A%0A      

print warning if ip address not set