(Lviv community of .NET developers)

IP geo-location - make it easy!

September 9, 2008 00:49 by alexk

Introduction

Today I want to open one more interesting part of making internet business - statistics collection. I hope you already knows popular service Google Analytics that helps many companies to track internet activity on web site (main issue that those analytics is not runtime, you have to wait 1 day for getting ready to use statistics). But do you want to know how to make own statistics tracking engine on IIS? I hope you do.

In this article I will describe how to collect statistics from IIS and how to do most common thing - resolve clients IP into geo-locations.

Theory

Let's make a quick look on web site hosting. What we can see? Let's count:

  1. IIS hosting server
  2. Web site (for example ASP.NET)
  3. Database (for example MS SQL Server)

Those 3 components are the most common for every web site based on Microsoft technologies.

Statistics tracking can be done only on first two components: IIS and ASP.NET... (but this is not exactly true, if take a look into depth then we can identify that in reality we have only one component IIS and it's filters model - ISAPI filters.)

Tracking of statistics on IIS layer is well described by Microsoft in own knowledge database.


Practice

First of all we don't want to re-invent bicycles. That is why we are taking a close look on systems available on WEB for free or commercial use (to avoid any advertising I miss here names of those systems). All those systems also known as “IIS Logs analyzer” utilities. So the first part of our effort will be to configure properly IIS for collecting our statistics.

To Do so we will use Microsoft published article: http://support.microsoft.com/kb/245243 -How to configure ODBC logging in IIS. Article contains all requered information about proper configuration of IIS for storing logs into Database (in my case this was latest SQL Server 2008). I hope Microsoft did a great job and everyone can repeat steps from article for proper IIS configuration.


Next step is the most interesting: How to get geo-location information about IP? Fast look into google and we found a lot of commercial services that for additional payment will open for us database of IPs geo-location... But THIS IS NOT OUR WAY! I search a little more and found really amazing service: http://software77.net/
and there open database IP-to-country: http://software77.net/cgi-bin/ip-country/geo-ip.pl

That looks very promissing. Software77.NET open database for free downloads. Delivery is done by ZIP archive with database CSV files in it. Wow! this is very very prommising.

3 hours later I publish small C# console application that automatically download database, unpack it and upload into our local database.

result database table is:

CREATE TABLE [dbo].[geoips]

(

    [ip_from] [bigint] NULL,

    [ip_to] [bigint] NULL,

    [rigistry] [nvarchar](20) NULL,

    [assigned] [bigint] NULL,

    [country_code] [nchar](2) NULL,

    [country_abbr] [nchar](5) NULL,

    [country_name] [nvarchar](100) NULL

) ON [PRIMARY]


and for proper user friendly representation of IPs I create special sql view:

create view vwGeoips

as

select ip_from,

    cast( (ip_from & 0xFF000000) / 16777216 as NVARCHAR(3)) + '.' +

    cast( (ip_from & 0xFF0000) / 65536 as NVARCHAR(3)) + '.' +

    cast( (ip_from & 0xFF00) / 256 as NVARCHAR(3)) + '.' +

    cast( ip_from & 0xFF as NVARCHAR(3)) as from_ip,

    ip_to,

    cast( (ip_to & 0xFF000000) / 16777216 as NVARCHAR(3)) + '.' +

    cast( (ip_to & 0xFF0000) / 65536 as NVARCHAR(3)) + '.' +

    cast( (ip_to & 0xFF00) / 256 as NVARCHAR(3)) + '.' +

    cast( ip_to & 0xFF as NVARCHAR(3)) as to_ip,

    rigistry,

    country_code,

    country_abbr,

    country_name

from geoips


And last moment is how to resolve client IP’s into geo locations:

create view [dbo].[vwInetLog]
as
select
    t.ClientHost,
    t.HttpRequests,
    t.LogTime,
    t.bytesrecvd,
    t.bytessent,
    g.country_abbr,
    g.country_name 
from (
    select
        ClientHost,
        dbo.IpToNumeric( ClientHost ) as clientIp,
        COUNT(*) as HttpRequests,
        MAX(LogTime) as LogTime,
        SUM(bytesrecvd) as bytesrecvd,
        SUM(bytessent) as bytessent
    from inetlog i
    where ClientHost not like '10.10.0.%'
    group by ClientHost
) t
left join geoips g on t.clientIp between g.ip_from and g.ip_to

 

NOTE: in view we remove all IP's that belongs to the local domain scope. In our case this "10.10.0.%" pattern, in yours case it can be different, for example: "192.168.1.%".


and small T-SQL function in use:

CREATE FUNCTION [dbo].[IpToNumeric]( @strIP varchar(15) )
    RETURNS bigint
AS
BEGIN
    DECLARE @intIPNum bigint
   
    -- check to ensure there are 3 dots
    IF( LEN(@strIP) - LEN( REPLACE( @strIP, '.', '' ) ) ) / LEN('.') = 3
    begin
        SET @intIPNum = (
            16777216 * CAST(PARSENAME(@strIP,4) as bigint) +
            65536 * PARSENAME(@strIP,3) +
            256 * PARSENAME(@strIP,2) +
            PARSENAME(@strIP,1)
        )
    end
   
    RETURN @intIPNum
END


As result we have a view that resolve for us IP to it's geo-location. shows last visit time from corresponding IP, show quantity of HTTP requests from one IP, shows accumulated IN/OUT traffic for corresponding IP...




I think this is very good first step for making own statistics analysis systems, IP resolving web services and etc.


Enjoy


Note: in source code change CONNECTION string according to your environment.

Source code of upload utility: geoipupdater_src.zip (29.03 kb)



Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

September 9. 2008 15:07

as I mention before in article I don't want to make advertising to any commercial solution, and THING NUMBER ONE: this web site with article - how to make something by own hands. If you understand internals you can make own code more effective and professional.

alex.kucherenko

September 19. 2008 08:50

Fantastic! It just happened that I was looking for some free GeoIP db and then got software77.net. Next was trying to understand the conversion (IP to Numeric) and here I got your post. Haven't tried your code yet, but I am sure this is what I was looking for. Thanks a lot dude.
Now what I am looking for next is to get a City/Region db. Which I think would be much difficult. My application needs city/region discovery, so please let me know if you come across any free / open source db for this.

Sumant

September 19. 2008 09:20

Tried! works like a charm! Neat work.

However, I got one build error in Program.cs (line 113) -
"bool bResult = s_event.WaitOne ( DownloadTimeout);"
it could not locate appropriate overload, so I set the second boolean parameter to true and it worked.

Sumant

December 16. 2008 14:12

That's a pretty UDF you got there!

Chrissy

January 11. 2010 16:18

You write very well. Kept me really engaged for some time :)

Niche Blueprint

January 21. 2010 11:03

well

friendly themes code structure

February 7. 2010 14:23

Thank nice post

craftsman sears

February 22. 2010 17:12

Interesting post and I really like your take on the issue. I now have a clear idea on what this matter is all about. Thank you so much.

araç sorgulama

February 25. 2010 04:58

Hello. Great job. I did not expect this on a Wednesday. This is a great story. Thanks!

mountain bike

February 26. 2010 19:44

However, I got one build error in Program.cs (line 113) - "bool bResult = s_event.WaitOne ( DownloadTimeout);" it could not locate appropriate overload, so I set the second boolean parameter to true and it worked.

colic calm

April 18. 2010 02:37

it's good to see this information in your post, i was looking the same but there was not any proper resource, thanx now i have the link which i was looking for my research.

Dissertation Writing

April 26. 2010 14:39

Great post, keeping me from working

Stanley

May 3. 2010 09:54

Subject matter such as this post is why I continue to come back to this blog of yours. Keep up the nice work!

city of roseville michigan

May 6. 2010 00:29

Thanks for this post. Cleared some issues up for me as well.

Raka

May 6. 2010 23:41

truly think the objective associated with the following writing has prompted me to start my own website. Thank you for the amazing work.

Debts Advice

May 9. 2010 09:33

My notes were too short for the report I needed until I stumbled on this information. I am sure this will make a good impression once I have done a few edits.
Thanks for making my job easier.

Marcus

May 11. 2010 00:24

Love your blog!

Shawn

May 27. 2010 17:41

I really like your blog about client loyalty :) This is my second visit to your site and I think I might just have to subscribe, thanks for posting about development . Peace.

customer satisfaction

June 14. 2010 01:36

You really know your development stuff. Great work, by the way.

Tracey

June 17. 2010 04:18

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

Annuities

June 20. 2010 23:50

I just stumbled upon your post. a gud view point. Thanks for the post on how to make own statistics tracking engine on IIS. keep it up

Annuities

July 6. 2010 19:50

Houses and cars are not very cheap and not everybody can buy it. But, <a href="http://bestfinance-blog.com/topics/business-loans";>business loans</a> are invented to aid different people in such situations.

PEARLWolfe34

July 7. 2010 09:40

Couldn't agree more.

Peeing

July 7. 2010 20:44

Ought to disagree with the comment above, get your details straight before writting such a comment.

pee-l

July 8. 2010 22:50

Trubloods.com is the web-site specialized in providing all the actual many advantages and also night from the struck demonstrate True Blood. True Blood is actually called as the particular artificial bloodstream the japanese have designed and now vampires of the underworld come out from the night to attempt because well since exist together along with humans. You follow Sookie Stackhouse as the actual lady deals even though using turmoil which is out there about the earth. There is love, humor, action, as well as several a whole lot more as you view vampires, humans and supernaturals clash within the city associated with Bon Temperatures, Louisiana. View free of charge of charge avenues from trubloods.com!

japanese

July 9. 2010 00:54

The Big 10 Football Nation Forum is often a web site in which it is possible to discuss just about something concerning the NCAA Big 10 Football Conference. You could also examine about many other points like other NCAA Football Conferences, other sporting activities, every day chit chat, and other topics. There is also a unique VIP Section in which you may possibly purchase, sell, trade, or have sporting activities bets with other members. http://www.big10footballnation.net/forums/forum.php

football forum

July 11. 2010 02:03

Great article, looking forward to reading more in the future.

James

July 13. 2010 07:39

hi, thank for this latest information. Therefore, I would like to ask for your permission to add some of this information in my blog. Of course, I will provide a link to your blog, as a source of my mentioned information.

live answering services

July 15. 2010 01:19

Great article, looking forward to reading more in the future.

vibram fivefingers

July 17. 2010 04:40

This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine. Did you acquired lots of links and I see lots of trackbacks??

dentist

July 23. 2010 16:43

I hope your next posts are as good as this one

russ

July 27. 2010 13:36

nice info and great way to show your talent

flat stomach exercises for women

Add comment


(Will show your Gravatar icon)