[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [metrics-utils/master] Allow writing out the server log with Tor user requests.



Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Mon, 20 Sep 2010 11:57:52 +0200
Subject: Allow writing out the server log with Tor user requests.
Commit: 4a2ae8ec8f2c427ab1bd37b019db646ce51264f6

---
 visitor/HOWTO        |   11 ++++++++---
 visitor/VisiTor.java |   24 ++++++++++++++++++------
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/visitor/HOWTO b/visitor/HOWTO
index bf24613..49b5060 100644
--- a/visitor/HOWTO
+++ b/visitor/HOWTO
@@ -5,11 +5,13 @@ VisiTor
 
 Change log:
 
-Changes in version 0.0.2 - 2010-09-20
+Changes in version 0.0.2 - 2010-09-2?
   - Don't break if we're given zero exit lists.
   - If we saw zero requests on a day, write "0", not "NA". Only write "NA"
     if we're missing a whole day of logs.
   - Warn if we're missing exit lists and skip that part of the server log.
+  - Add fourth parameter to write out the part of the server log with Tor
+    user requests.
 
 Changes in version 0.0.1 - 2010-09-19
   - Initial release
@@ -67,13 +69,16 @@ for Linux and Mac OS X; commands for Windows may vary):
 
   $ javac VisiTor.java
 
-- Run the Java application, providing it with the parameters it needs:
+- Run the Java application, providing it with the parameters it needs.
+  Note that the fourth parameter that writes out the server log part with
+  Tor user requests is optional:
 
   java VisiTor <web server log> <exit list directory> <output file>
+       [<server log part with Tor user requests>]
 
   A sample invocation might be:
 
-  $ java VisiTor access_log exitlists/ out.csv
+  $ java VisiTor access_log exitlists/ out.csv tor_access_log
 
 - Find the results in /home/you/visitor/out.csv in a format that can be
   imported by any spreadsheet application like OpenOffice.org Calc or
diff --git a/visitor/VisiTor.java b/visitor/VisiTor.java
index 7f58164..dd004cd 100644
--- a/visitor/VisiTor.java
+++ b/visitor/VisiTor.java
@@ -11,15 +11,17 @@ public final class VisiTor {
   public static void main(String[] args) {
 
     /* Check parameters. */
-    if (args.length != 3) {
+    if (args.length < 3 || args.length > 4) {
       System.out.println("\nUsage: java "
           + VisiTor.class.getSimpleName()
-          + " <web server log> <exit list directory> <output file>\n");
+          + " <web server log> <exit list directory> <output file> "
+          + "[<server log part with Tor user requests>]\n");
       return;
     }
     String webServerLog = args[0];
     String exitListDirectory = args[1];
     String outputFile = args[2];
+    String serverLogPartTorUsers = args.length == 4 ? args[3] : null;
 
     /* Read the first line of the web server log to let the user know
      * early if we think we can't parse it. */
@@ -96,7 +98,7 @@ public final class VisiTor {
           }
           exitListFiles.add(dirOrFile);
         } catch (ParseException e) {
-          /* Must be an unrelated file. Ingore it. */
+          /* Must be an unrelated file. Ignore it. */
         }
       }
     }
@@ -151,6 +153,8 @@ public final class VisiTor {
     try {
       BufferedReader br = new BufferedReader(new FileReader(
           webServerLog));
+      BufferedWriter bw = serverLogPartTorUsers == null ? null
+          : new BufferedWriter(new FileWriter(serverLogPartTorUsers));
       String line = null;
       boolean haveWarnedAboutMissingExitListsStart = false,
           haveWarnedAboutMissingExitListsEnd = false;
@@ -170,7 +174,7 @@ public final class VisiTor {
           if (!haveWarnedAboutMissingExitListsStart) {
             System.out.print("WARN\nWe are missing exit lists before "
                 + isoDateTimeFormat.format(firstExitList) + " that we "
-                + "need  to parse the beginning of the server log file!\n"
+                + "need to parse the beginning of the server log file!\n"
                 + "Skipping... ");
             haveWarnedAboutMissingExitListsStart = true;
           }
@@ -214,6 +218,9 @@ public final class VisiTor {
           }
         }
         if (isTorUser) {
+          if (bw != null) {
+            bw.write(line + "\n");
+          }
           int requestsSoFar = torRequests.containsKey(currentDate)
               ? torRequests.get(currentDate) : 0;
           torRequests.put(currentDate, requestsSoFar + 1);
@@ -224,10 +231,15 @@ public final class VisiTor {
         }
       }
       br.close();
+      if (bw != null) {
+        bw.close();
+      }
       System.out.println("OK");
     } catch (IOException e) {
-      System.out.println("FAILED\nCould not read web server log file! "
-          + "Trying to write results anyway.");
+      System.out.println("FAILED\nCould not read web server log file"
+          + (serverLogPartTorUsers == null ? "" : " or write the server "
+          + "log file part with Tor user requests") + "! Trying to write "
+          + "results anyway.");
       e.printStackTrace();
     }
 
-- 
1.7.1