Geoloc: fast geolocation from the command line
I wrote geoloc to help me query my access logs, fast.
geoloc is a command line tool for bulk geolocation queries written in C++. Once its binary database has been built, geoloc
performs geolocation queries offline.
Examples:
Bulk lookup from apache access.log:
Query some IPs:
geoloc
is designed to run fast and load fast:
Installation
The program is designed as a portable application, to run out of ~/bin
, with the database stored in ~/var/db/geoloc/geodata.bin
.
To install:
The configure script will check for these dependencies:
- iconv
- unzip
- wget
- make
- c++
During installation, data will be downloaded from MaxMind to create the database.
An update script will be installed into ~/bin/_geoloc_update.sh
. Run this script when you would like to update your geolocation database. MaxMind updates their source data once a month.
I have tested on OSX 10.9.5 and Ubuntu 14.04. Other unices are likely to work with minimal or no changes. It is unlikely to work on windows, due to the use of mmap.
Design and Implementation
I plan to do a longer write-up on the design and implementation of the tool, to share some C++ tips and tricks.
The short version is that the code operates in two phases, packing and query. The packing phase is all about converting the data into a machine optimal format, namely relocatable sorted vectors. The query phase simply mmaps that data, and performs a std::upper_bound binary search on it to find the IPs.
There is an outline of the code, roughly in topological order here, that contains a summary of each module.
Attribution
This software includes GeoLite data created by MaxMind available from http://www.maxmind.com
Like the article? Please follow me on twitter and check out my bio.