Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IP2Location.io plugin #401

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
65 changes: 65 additions & 0 deletions plugins/ip2locationio/README.md
Original file line number Diff line number Diff line change
@@ -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.
53 changes: 53 additions & 0 deletions plugins/ip2locationio/alerta_ip2locationio.py
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions plugins/ip2locationio/setup.py
Original file line number Diff line number Diff line change
@@ -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'
]
}
)