Get geo-location information with GeoIP2 Analytics visitor IPs

Source: Internet
Author: User
Tags geoip gz file maxmind

Original link: http://blog.csdn.net/johnnycode/article/details/42028841

MaxMind GeoIP2 Services can identify the location and other characteristics of Internet users, including personalized content, fraud detection, advertising targeting, website traffic analysis, enforcement, geo-targeting, geo-fencing (geo-fencing), and digital rights management. Currently using GeoIP more is to cooperate with Nginx or Apache server for log analysis to obtain the geographical distribution of website traffic.

GeoIP is divided into commercial version and free version, the free version than the commercial version of the accuracy of a lot, after testing for the city positioning does have a gap, can accept your accuracy requirements!

First, the free version of the introduction:

1, GeoLite version, online spread more widely, the database type is dat format file, the library file is small without precision testing.

2, GeoLite2 version, currently the latest version, database files for mmdb format files, interested in understanding the Mmdb format points here.

Both database file size comparison, GeoLite2 feature click here

$ du-sh *32mgeolite2-city.mmdb2.3mgeolite2-country.mmdb18mgeolitecity.dat732kgeolitecountry.dat
The city file contains the urban information database, and the country file is the national information database.

Second, download GeoLite2 database
The download method is divided into two types, the first by downloading the GZ compressed package, the second by using the official download update program, the second type is recommended, the official name of the database in the first Tuesday month update, if you want to make a scheduled task monthly update recommendations Choose the second kind! GeoIP2 detailed update log point here.

Two ways to talk about here, this stage is just how to download the database, call the way you need to refer to the third stage API call section!

1, the first way, download the GZ file and unzip.

GeoLite2 only provide city database and country database download View details in the click, the database file is divided into binary and CVS two kinds, where binary files are used.

$ sudo mkdir-p/mnt/data/geolite2 && cd $_$ sudo wget http://geolite.maxmind.com/download/geoip/database/ geolite2-city.mmdb.gz$ sudo gzip-d geolite2-city.mmdb.gz$ sudo wget http://geolite.maxmind.com/download/geoip/ database/geolite2-country.mmdb.gz$ sudo gzip-d geolite2-country.mmdb.gz

2, the second way, need to install the official download update program geoipupdate.

A, to GitHub download Geoipupdate, the latest version for 2.1.0,github connection speed to have patience, certainly can download drip! Compiling files requires Libcurl-devel package support, which needs to be downloaded and installed in advance.

$ sudo yum install libcurl-devel-y$ sudo wget https://github.com/maxmind/geoipupdate/releases/download/v2.1.0/  geoipupdate-2.1.0.tar.gz$ sudo tar xzvf geoipupdate-2.1.0.tar.gz$ cd geoipupdate-2.1.0$ sudo./configure$ sudo make$ sudo Make install

Only two files to follow after compiling

Update execution File/usr/local/bin/geoipupdate

Account information File/usr/local/etc/geoip.conf

B, configure the account information geoip.conf, modify the configuration file as follows, this configuration file by default download Mmdb file, if you want to download dat file uncomment!

# The following UserId and LicenseKey is required Placeholders:userid 999999LicenseKey 000000000000# Include one or more of the following productids:# * Geolite2-city-geolite 2 city# * geolite2-country-geolite2 country# * 506-geolite Leg Acy country# * 517-geolite Legacy asn# * 533-geolite Legacy city# dat format database #productids geolite2-city geolite2-country 506 533# mmdb Format database productids geolite2-city geolite2-country 132 106

C. Perform the update

Check out the geoipupdate help file to see what parameters are available! The-d parameter downloads the file to the specified directory, and the-v parameter displays the download process details.

$/usr/local/bin/geoipupdate-husage:geoipupdate [-VHV] [-F license_file] [-D Custom Directory]-  d DIR   store Dow nloaded files in DIR  -F file Use  configuration found in file (see Geoip.conf (5) Mans page)-  h       Display this Help Text-  v use       verbose output-  v display the version and       exit

Execute update command, download speed look at network situation, this article download the file to the/mnt/data/geolite2/directory.

$ ll/mnt/data/geolite2/&& cd $_ total dosage 0$ sudo/usr/local/bin/geoipupdate-d/mnt/data/geolite2/-v$ ll Total dosage 34088-rw- R--r--. 1 root root 32553611 December 18:14 geolite2-city.mmdb-rw-r--r--. 1 root root  2349406 December 18:14 Geolite2-country.mmdb

How to configure Scheduled Tasks update GeoLite2 database on your own.

Third, install the GeoLite2 API Calling program

Officially available. NET (C #), C, Java, Perl, Python, Apache API calls. Other third-party interfaces are also available, but the official does not provide technical support, click here for details.

This article uses the C language API interface for call testing, other languages please refer to the official guide to solve their own! C Language API GitHub

$ sudo wget https://github.com/maxmind/libmaxminddb/releases/download/1.0.3/libmaxminddb-1.0.3.tar.gz$ sudo tar xzvf libmaxminddb-1.0.3.tar.gz$ cd libmaxminddb-1.0.3$ sudo./configure$ sudo make$ sudo make install

View Help documentation

$/usr/local/bin/mmdblookup--help mmdblookup--file/path/to/file.mmdb--ip 1.2.3.4 [path to lookup] this application a Ccepts the following options:--file (-F) The path to the MMDB file.      Required. --ip (-i) The IP address to look up.      Required. --verbose (-V) Turns on verbose output.      Specifically, this causes this application to output the database metadata.      --version Print The program's version number and exit.  --help (-H-?)  Show usage information.  If an IP's data entry resolves to a maps or array, you can provide a lookup path to the only show part of the that data. For example, given a JSON structure like this: {"names": {"en": "Germany", "de": "Deut Schland "}," Cities ": [" Berlin "," Frankfurt "]} You could look up just the 中文版 name by calling MMD Blookup with a lookup path of:mmdblookup--file ...--ip names en Or you could looks up the second city in theList With:mmdblookup--file--ip ... cities 1 Array numbering begins with zero (0). If you don't provide a path to lookup, all of the information for a given IP would be shown.
Iv. Testing

Test ip:112.225.35.70 Shandong Province Qingdao Unicom

1, to obtain the country information, the country information is correct drops, looked like garbled place is the display Russian!

$/usr/local/bin/mmdblookup--file/mnt/data/geolite2/geolite2-country.mmdb--ip 112.225.35.70 {"Continent": {           "Code": "as" <utf8_string> "geoname_id": 6255147 <uint32> "names": {"De": "Asien" <utf8_string> "en": "Asia" <utf8_string            > "es": "Asia" <utf8_string> "fr": "Asie" <utf8_string>            "Ja": "アジア" <utf8_string> "PT-BR": "Ásia" <utf8_string>       "Ru": "Азия" <utf8_string> "ZH-CN": "Asia" <utf8_string>} } "Country": {"geoname_id": 1814991 <uint32> "Iso_code": "CN" <               Utf8_string> "names": {"de": "China" <utf8_string> "en": "China" <Utf8_string> "es": "China" <utf8_string> "fr": "Chine" <utf8 _string> "ja": "Chinese" <utf8_string> "PT-BR": "China" <utf8_str Ing> "ru": "Китай" <utf8_string> "ZH-CN": "China" <utf8_string&          Gt           }} "Registered_country": {"geoname_id": 1814991 <uint32> "Iso_code":            "CN" <utf8_string> "names": {"de": "China" <utf8_string>            "EN": "China" <utf8_string> "es": "China" <utf8_string> "FR": "Chine" <utf8_string> "ja": "China" <utf8_string> "PT-BR"            : "China" <utf8_string> "ru": "Китай" <utf8_string> "ZH-CN":    "China" <utf8_string>}} 

2, access to city information, this data is a bit tangled, the province no problem, the city is a problem! The official presentation address is very precise, perhaps this is the difference between free and charge:)

$/usr/local/bin/mmdblookup--file/mnt/data/geolite2/geolite2-city.mmdb--ip 112.225.35.70 {"City": { "geoname_id": 1805753 <uint32> "names": {"de": "Jinan" <utf8 _string> "en": "Jinan" <utf8_string> "es": "Jinan" <utf8_str Ing> "fr": "Jinan" <utf8_string> "ja": "済 Seongnam" <utf8_string&gt            ;            "PT-BR": "Jinan" <utf8_string> "ru": "Цзинань" <utf8_string> "ZH-CN": "Jinan" <utf8_string>} "continent": {"code": "As" &               Lt;utf8_string> "geoname_id": 6255147 <uint32> "names": {"de":               "Asien" <utf8_string> "en": "Asia" <utf8_string> "es": "Asia" <utf8_string> "fr": "Asie" <utf8_string> "ja": "アジア" <ut F8_string> "PT-BR": "Ásia" <utf8_string> "ru": "Азия" <utf8_ String> "ZH-CN": "Asia" <utf8_string>} "country": {"Geon           ame_id ": 1814991 <uint32>" Iso_code ":" CN "<utf8_string>" names ":            {"De": "China" <utf8_string> "en": "China" <utf8_string>            "Es": "China" <utf8_string> "fr": "Chine" <utf8_string>            "ja": "Chinese" <utf8_string> "PT-BR": "China" <utf8_string>      "Ru": "Китай" <utf8_string> "ZH-CN": "China" <utf8_string>}      } "Location": {"Latitude": 36.668300 <double> "Longitude": 116.997200 <double> "t           Ime_zone ":" Asia/shanghai "<utf8_string>}" Registered_country ": {" geoname_id ":            1814991 <uint32> "Iso_code": "CN" <utf8_string> "names": { "De": "China" <utf8_string> "en": "China" <utf8_string> "es"               : "China" <utf8_string> "fr": "Chine" <utf8_string> "ja":               "Chinese" <utf8_string> "PT-BR": "China" <utf8_string> "ru": "Китай" <utf8_string> "ZH-CN": "China" <utf8_string>} "Subdi  Visions ": [{" geoname_id ": 1796328 <uint32>" Iso_code ":" 37 "    <utf8_string>      "Names": {"en": "Shandong Sheng" <utf8_string> "ZH-CN": "Shandong Province" <utf8_string>}]}

Because the data obtained is in Json format, the content can be formatted output, such as CITY->NAMES->ZH-CN content in the output city database, according to the Help document hint

$/usr/local/bin/mmdblookup--file/mnt/data/geolite2/geolite2-city.mmdb--ip 112.225.35.70 city names zh-cn  "Jinan" & Lt;utf8_string>

Although GeoIP2 in the city positioning analysis is not very ideal, but for me precision can be accepted, better than nothing!

Get geo-location information with GeoIP2 Analytics visitor IPs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.