Wednesday, December 14, 2011

hostip grails plugin: from IP address to localisation

Getting a localisation from an IP address is somehow straight forwards with available web services. However, for massive report (such as analyzing web logs from the remoteIP perspective), it is not doable, even with caching.
The idea of the plugin is to use the free distributed hostip database, mirror it locally and give a Grails service to query it.
There is another post, comparing hostip with maxmind geoip. It should certainly be read before jumping to hostip.
Installation
grails install-plugin hostip
Configuration
The hostip database is to be mirrored (see below), so we must set in Config.groovy to access the local store.
 hostip.datasource.database="hostip"
 hostip.datasource.host="localhost"
 hostip.datasource.username="hostip"
 hostip.datasource.password="h0st1p"
Usage
A simple hostipService is proposed:
def hostipService
def loc = hostipService.getLocation('129.195.0.205')


assert loc.city == 'Geneva'
assert loc.country == 'SWITZERLAND'
assert loc.country_code == 'CH'
//null is returned if location cannot be defined

Mirroring the database
Create the local database
Log as root user on your mysql

CREATE USER 'hostip'@'localhost' IDENTIFIED BY  'h0st1p';
CREATE DATABASE hostip;
GRANT ALL ON hostip.* TO 'hostip'@'localhost';
Download and mirror
wget -O - --quiet \
http://db.hostip.info/mirror/hostip_current.sql.gz | \
mysql -u hostip -p -D hostip
Other mirrors are available. It should be straightforwards to include the mirroring in a crontab

3 comments:

  1. Hello Alexandre

    Thank you for this post. I like the idea of this plugin. Still I have a question regarding the additional database configuration. Why does your plugin need an additional dataSource? Don't you provide plugin specific domain classes and use the app database?

    Best regards
    Silvio

    ReplyDelete
  2. Hi Silvio
    Hostipinfo provide their data as a mysql dump. So one solution was to import the dump and run with it directly.
    As the question I wanted this plugin to answer was rather straightforwards, I just even make a direct SQL call to their database.
    Of course, if they had provided another kind of export, of if we had wanted the plugin to anser other questions, that would have not been a great deal to build our own domain classes and import their data.

    ReplyDelete
  3. I would add that when I realized the difference between hostip data and the commercial concurrent geoip (see the other post), I did limit a little bit the effort on that module.
    But I'd be happy to muscle it a little bit if either I was wrong in my benchmark or the data evolves
    alex

    ReplyDelete