diff --git a/README.md b/README.md index aec5d88e..23edd6eb 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Plugins * [Forward](plugins/forward) * [GeoIP](plugins/geoip) * [InfluxDB](plugins/influxdb) + * [Ip2Location.io](plugins/ip2locationio) * [Logstash](plugins/logstash) * [Matrix](plugins/matrix) * [Mattermost](plugins/mattermost) diff --git a/plugins/ip2locationio/README.md b/plugins/ip2locationio/README.md new file mode 100644 index 00000000..61ac072a --- /dev/null +++ b/plugins/ip2locationio/README.md @@ -0,0 +1,65 @@ +IP2Locationio Plugin +==================== + +Query geolocation information for the origin IP address from IP2Location.io API. + +For help, join [![Slack chat](https://img.shields.io/badge/chat-on%20slack-blue?logo=slack)](https://slack.alerta.dev) + +Prerequisite +------------ +This plugin requires an API key to function. You may sign up for a free API key at https://www.ip2location.io/pricing. + +Installation +------------ + +Clone the GitHub repo and run: + + $ python setup.py install + +Or, to install remotely from GitHub run: + + $ pip install git+https://github.com/alerta/alerta-contrib.git#subdirectory=plugins/ip2locationio + +Note: If Alerta is installed in a python virtual environment then plugins +need to be installed into the same environment for Alerta to dynamically +discover them. + +Configuration +------------- + +Add `ip2locationio` to the list of enabled `PLUGINS` in `alertad.conf` server +configuration file and set plugin-specific variables either in the +server configuration file or as environment variables. For example: + +```python +PLUGINS = ['reject', 'remote_ip', 'geoip'] +IP2LOCATIONIO_ACCESS_KEY = 'YOUR_IP2LOCATIONIO_API_KEY' +``` + +**Sample output** + + + +```json +{ + "alert": { + "attributes": { + "as":"Google LLC" + "asn":"15169", + "city": "Mountain View", + "country": "US", + "ip": "8.8.8.8", + "latitude":37.38605, + "longitude":-122.08385, + "state": "California", + "time_zone":"-08:00", + "zip_code":"94035" + }, + ... + } + } +``` +License +------- + +Copyright (c) 2024 IP2Location.io. Available under the MIT License. \ No newline at end of file diff --git a/plugins/ip2locationio/alerta_ip2locationio.py b/plugins/ip2locationio/alerta_ip2locationio.py new file mode 100644 index 00000000..9e46eab5 --- /dev/null +++ b/plugins/ip2locationio/alerta_ip2locationio.py @@ -0,0 +1,53 @@ +import logging +import os + +import requests +from alerta.plugins import PluginBase + +try: + from alerta.plugins import app # alerta >= 5.0 +except ImportError: + from alerta.app import app # alerta < 5.0 + +LOG = logging.getLogger('alerta.plugins.ip2locationpy') + +IP2LOCATIONIO_URL = 'https://api.ip2location.io' +IP2LOCATIONIO_ACCESS_KEY = os.environ.get( + 'IP2LOCATIONIO_ACCESS_KEY') or app.config.get('IP2LOCATIONIO_ACCESS_KEY', None) + + +class IP2Locationio(PluginBase): + + def pre_receive(self, alert): + + if 'ip' in alert.attributes: + ip_addr = alert.attributes['ip'].split(', ')[0] + LOG.debug('IP2Location.io lookup for IP: %s', ip_addr) + url = '{}/?key={}&ip={}'.format(IP2LOCATIONIO_URL, IP2LOCATIONIO_ACCESS_KEY, + ip_addr) + 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) + LOG.debug('Result: %s', r.text) + try: + ip2locationio_result = r.json() + if 'country' in ip2locationio_result and 'language' in ip2locationio_result['country']: + ip2locationio_result['country']['official_language'] = ip2locationio_result['country'].pop('language') + alert.attributes = { + 'ip2locationio_result': ip2locationio_result + } + except Exception as e: + LOG.error('IP2Location.io lookup failed: %s' % str(e)) + raise RuntimeError('IP2Location.io lookup failed: %s' % str(e)) + + return alert + + def post_receive(self, alert): + return + + def status_change(self, alert, status, text): + return diff --git a/plugins/ip2locationio/setup.py b/plugins/ip2locationio/setup.py new file mode 100644 index 00000000..08b954ac --- /dev/null +++ b/plugins/ip2locationio/setup.py @@ -0,0 +1,25 @@ +from setuptools import setup, find_packages + +version = '1.0.0' + +setup( + name="alerta-ip2locationio", + version=version, + description='Alerta plugin to query geolocation information from IP2Location.io API.', + url='https://github.com/alerta/alerta-contrib', + license='MIT', + author='IP2Location', + author_email='support@ip2location.com', + py_modules=['alerta_ip2locationio'], + packages=find_packages(), + install_requires=[ + 'requests' + ], + include_package_data=True, + zip_safe=True, + entry_points={ + 'alerta.plugins': [ + 'ip2locationio = alerta_ip2locationio:IP2Locationio' + ] + } +)