Sunday, October 23, 2011

GoDaddy Site Analytics, User History, Log Parser, Hit Counter Example Tutorial


For $2.99 a month GoDaddy.com provides Site Analytics, which reads your Microsoft web server's IIS log files and summarizes your hit counter along with IP addresses for you similar to WebTrends

You can do this yourself with open source and free code if you have Admin rights to your server, i.e. you can configure request logging in IIS Manager.

Then use Log Parser  to read XML, tab and space separated files, active directory, Windows registry, and Netmon captures. Output as charts, text, or send to a SYSLOG server.

Because you don't have access to the log files at GoDaddy, some useful tools such as Microsoft's Log Parser and Apache Log4Net are of limited use.

Output - slowest pages

Log Parser does charts.
Output - most commonly used .aspx
		  pages

  Download the Apache Software Foundation port of Log4j for C# for ASP.Net at http://logging.apache.org/log4net/download.html


W3SVC Extended Log Format fields:
date
time 
s-sitename 
s-ip
cs-method 
cs-uri-stem 
cs-uri-query
s-port
cs-username 
c-ip
cs(User-Agent) 
sc-status 
sc-substatus 


sc-win32-status

Unfortunately, in the free, shared hosting plans you don't have access to the log files for a number of reasons. However, if you did, you could use code snippets from http://www.mikesdotnetting.com/Article/103/Build-your-own-Whois-Lookup-with-ASP.NET-and-jQuery :


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="IPLookup.aspx.cs" Inherits="IPLookup" %>

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Untitled Pagetitle>
    <style type="text/css">
        body { font-family:Verdana;font-size:76%; }
        pre { font-size:10pt; }
        span { cursor:pointer; }
        #dns { float:left; }
        #calendar{ float:left;width:350px; }
        #loading { left:300px;z-index:100;position:absolute; }
    style>
head> 
<body> 
     id="form1" runat="server">

    <div id="calendar">
      <asp:Calendar ID="Calendar1" runat="server"
        onselectionchanged="Calendar1_SelectionChanged" />
      <asp:Literal ID="ip_addresses" runat="server" />
    div>
    <div id="dns">div>
    <div id="loading">div>
    form>
body> 
html> 


protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    ip_addresses.Text = "";
    List<string> IPs = new List<string>();
    StringBuilder sb = new StringBuilder();
    DateTime date = Calendar1.SelectedDate;
    string filedate = string.Format("{0:yyMMdd}", date);
    string path = @"D:\Logs\ex" + filedate + ".log";

    if (File.Exists(path))
    {
        string content;
        using (StreamReader sr = new StreamReader(path))
        {
            content = sr.ReadToEnd();
        }

        Regex re = new Regex(@"\w\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}\w");
        MatchCollection mc = re.Matches(content);
        foreach (Match mt in mc)
        {
            if (mt.ToString() != "xxx.xxx.xxx.xxx")
                IPs.Add(mt.ToString());
        }

        var result = IPs.Select(i => i).Distinct().ToList();

        foreach (string ip in result)
        {
            sb.Append("" + ip + "\n");
        }

        ip_addresses.Text = "
" + sb.ToString() + "

";

    }
    else
    {
        ip_addresses.Text = "No logs available for that date";
    }
}
private static string GetHtmlPage(string url)
{
    String result;
    WebResponse response;
    WebRequest request = HttpWebRequest.Create(url);
    response = request.GetResponse();
    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
    {
        result = sr.ReadToEnd();
        sr.Close();
    }
    return result;
}
private static string PostHtmlPage(string url, string post)
{
    ASCIIEncoding enc = new ASCIIEncoding();
    byte[] data = enc.GetBytes(post);
    WebRequest request = HttpWebRequest.Create(url);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;
    Stream stream = request.GetRequestStream();
    stream.Write(data, 0, data.Length);
    stream.Close();
    WebResponse response = request.GetResponse();
    string result;
    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
    {
        result = sr.ReadToEnd();
        sr.Close();
    }
    return result;
}
[WebMethod]
public static string GetWhois(string ip)
{
    string response = "";
    string arin = "https://ws.arin.net/whois/?queryinput=" + ip;
    string ripe = "http://www.db.ripe.net/whois?form_type=simple&full_query_string=&searchtext=" + ip + "&do_search=Search";
    string apnic = "http://wq.apnic.net/apnic-bin/whois.pl";
    string lacnicFields = "query=" + ip;
    string apnicFields = ".cgifields=object_type&.cgifields=reverse_delegation_domains&do_search=Search&" +
                         "form_type=advancedfull_query_string=&inverse_attributes=None&object_type=All&searchtext=" + ip;
    response = GetHtmlPage(arin);
    Regex pre = new Regex(@"
[.\n\W\w]*</p>", RegexOptions.IgnoreCase);

    Match m = pre.Match(response);
    if (pre.IsMatch(response))
    {
        if (m.Value.IndexOf("OrgName:    RIPE Network Coordination Centre") > 0)
        {
            response = GetHtmlPage(ripe);
            m = pre.Match(response);
        }
        else if (m.Value.IndexOf("OrgName:    Asia Pacific Network Information Centre") > 0)
        {
            response = PostHtmlPage(apnic, apnicFields);
            m = pre.Match(response);
        }
        else if (m.Value.IndexOf("OrgName:    Latin American and Caribbean IP address Regional Registry") > 0)
        {
            response = PostHtmlPage(lacnic, lacnicFields);
            m = pre.Match(response);
        }
        return m.Value;
    }
    else
    {
        return "
No Data

";

    }
}
<script type="text/javascript" src="script/jquery-1.3.2.min.js">script> 
<script type="text/javascript"
  $(document).ready(function() {
    $("span").each(function() {
      $(this).click(function() {
        $("#loading").html("");
        $("#dns").empty();
        $.ajax({
          type: "POST",
          contentType: "application/json; charset=utf-8",
          data: "{ip: '" + $(this).html() + "'}",
          url: "IPLookup.aspx/GetWhois",
          dataType: "json",
          success: function(response) {
            $("#loading").empty();
            $("#dns").html(response.d);
          }
        });
      });
    });
  });
script> 

Young Roofers

Young Roofers
Men Making

Fly. Be Free.

Fly. Be Free.
Man Ready to Hang ... Glide

Burke

Burke
A Man in the Making - 12 years old

Blackwater

Blackwater
A Man in the Mud