All pastes #1736857 Raw Edit

access_log Java

public java v1 · immutable
#1736857 ·published 2010-01-04 00:36 UTC
rendered paste body
package accesslog;// based on http://www.wellho.net/resources/ex.php4?item=j714/Access.javaimport java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.HashSet;import java.util.NoSuchElementException;import java.util.StringTokenizer;/** * Access Log file (NCSA format) analysis */public class Access{  String host;  String time;  String request;  int status;  int size;  String referer;  String userAgent;  // 200.70.150.125 - - [26/Oct/2009:01:42:33 +0100] "GET /favicon.ico HTTP/1.1" 404 183 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.0; pt-BR; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3"  public Access(String data)  {    StringTokenizer splitter = new StringTokenizer(data, " \t");    String skip;    host = splitter.nextToken();    skip = splitter.nextToken();    skip = splitter.nextToken("[");    time = splitter.nextToken(" \t");    skip = splitter.nextToken("\"");    request = splitter.nextToken();    skip = splitter.nextToken(" \t");    status = Integer.parseInt(splitter.nextToken(" \t"));    try    {      size = Integer.parseInt(splitter.nextToken(" \t"));    }    catch (Exception e)    {      size = 0;    }    skip = splitter.nextToken("\"");    referer = splitter.nextToken();    if (referer != null && !referer.equals("") && !referer.equals(" "))       // "-" means "no referer", but "" would result in the userAgent ending up in the skip    {      skip = splitter.nextToken();    }    try    {      userAgent = splitter.nextToken();    }    catch (NoSuchElementException ex)    {      System.err.println("problem");    }  }  public static void main(String[] args) throws IOException  {    BufferedReader source = new BufferedReader(        new FileReader("/home/monika/Homepage/other/access_log/test/access_log_2009_w44-0"));    int total = 0;    int msie = 0;    int firefox = 0;    int opera = 0; // some Opera also contain "MSIE"    int chrome = 0;    int safari = 0; // Chrome also contains "Safari"    int konqueror = 0;    int k_meleon = 0;    int seamonkey = 0;    int bot = 0;    int other = 0;    boolean found = false;    int windows = 0, linux = 0, apple = 0, otherOs = 0;    int mnenhy = 0;    HashSet<String> hosts = new HashSet<String>();    String line = "";    String agent = "";    while ((line = source.readLine()) != null)    {      Access access = new Access(line);      if (hosts.contains(access.host))      {        continue;      }      hosts.add(access.host);      total++;      // System.out.println(access.userAgent);      found = false;      agent = access.userAgent.toLowerCase();      if (agent.contains("firefox") || agent.contains("iceweasel") || agent.contains("shiretoko") || agent.contains("minefield"))      {        firefox++;        found = true;      }      if (agent.contains("konqueror"))      {        if (found)        {          System.err.println("duplicate: " + access.userAgent);        }        konqueror++;        found = true;      }      if (agent.contains("seamonkey"))      {        if (found)        {          System.err.println("duplicate: " + access.userAgent);        }        seamonkey++;        found = true;      }      if (agent.contains("k-meleon"))      {        if (found)        {          System.err.println("duplicate: " + access.userAgent);        }        k_meleon++;        found = true;      }      if (agent.contains("opera"))      {        if (found)        {          System.err.println("duplicate: " + access.userAgent);        }        opera++;        found = true;      }      // Opera 8 claims to be also MSIE      else if (agent.contains("msie"))      {        if (found)        {          System.err.println("duplicate: " + access.userAgent);        }        msie++;        found = true;      }      if (agent.contains("chrome"))      {        if (found)        {          System.err.println("duplicate: " + access.userAgent);        }        chrome++;        found = true;      }      else if (agent.contains("safari"))      {        if (found)        {          System.err.println("duplicate: " + access.userAgent);        }        safari++;        found = true;      }            if ((agent.contains("bot") && !agent.contains("surveybot")) || agent.contains("yandex") || agent.contains("catalog") || agent.contains("about.ask.com")          || agent.contains("slurp") || agent.contains("trend micro") || agent.contains("yahoocache") || agent.contains("archive") || agent.contains("spider")          || agent.contains("crawler"))      {        if (found)        {          System.err.println("duplicate: " + access.userAgent);        }        bot++;        found = true;      }      else      {        if (agent.contains("windows"))        {          windows++;        }        else if (agent.contains("linux"))        {          linux++;        }        else if (agent.contains("mac") )         {          apple++;        }        else        {          otherOs++;        }               }      if (!found)      {        other++;        // System.out.println("unknown: " + access.userAgent);      }            if (agent.contains("mnenhy"))      {        mnenhy++;      }    } // end of loop over the access_log file content    System.out.println("total: " + total);    System.out.println("firefox: " + firefox);    System.out.println("msie: " + msie);    System.out.println("opera: " + opera);    System.out.println("chrome: " + chrome);    System.out.println("safari: " + safari);    System.out.println("konqueror: " + konqueror);    System.out.println("k_meleon: " + k_meleon);    System.out.println("seamonkey: " + seamonkey);    System.out.println("bot: " + bot);    System.out.println("other: " + other);        System.out.println("---");        System.out.println("windows: "+windows);    System.out.println("linux: "+linux);    System.out.println("apple: "+apple);    System.out.println("other OS: "+otherOs);        System.out.println("---");        System.out.println("mnenhy: "+mnenhy);  }}