Browse Source

logfileparser v1.0

Christoph Haas 7 years ago
parent
commit
a94fd4eed0
1 changed files with 196 additions and 0 deletions
  1. 196
    0
      src/at/sprinternet/apache/LogFileParser.java

+ 196
- 0
src/at/sprinternet/apache/LogFileParser.java View File

@@ -0,0 +1,196 @@
1
+package at.sprinternet.apache;
2
+/**
3
+ * @author Christoph Haas <christoph.h@sprinternet.at>
4
+ * 
5
+ * Parse an Apache log file with Regular Expressions
6
+ * 
7
+ * output is configurable
8
+ * 
9
+ * @version 1.0
10
+ */
11
+
12
+import java.io.BufferedReader;
13
+import java.io.DataInputStream;
14
+import java.io.File;
15
+import java.io.FileInputStream;
16
+import java.io.FileOutputStream;
17
+import java.io.IOException;
18
+import java.io.InputStreamReader;
19
+import java.util.regex.*;
20
+
21
+/**
22
+ * Parse an Apache log file with Regular Expressions
23
+ */
24
+public class LogFileParser{
25
+	private static final int NUM_FIELDS = 9;
26
+	private static final String FIELD_SPACE = "# #";
27
+	private static final int[] DEFAULT_FIELDS = new int[] {1,3,4,5,6,7,8,9};
28
+	private static final String logEntryPattern = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\d+) \"([^\"]+)\" \"([^\"]+)\"";
29
+	private static boolean showDebugOutput = false;
30
+	private static String logFilePath = "";
31
+	private static String parsedFilePath = "";
32
+	private static String filter = "";
33
+	private static int[] filterFields = DEFAULT_FIELDS;
34
+	private static File logFile;
35
+	private static Pattern pattern = Pattern.compile(logEntryPattern);
36
+	
37
+	private static String[] parseLine(String line) {
38
+		String[] returnString = new String[filterFields.length];
39
+		Matcher matcher = pattern.matcher(line);
40
+		if (!matcher.matches() || NUM_FIELDS != matcher.groupCount()) {
41
+			System.err.println("Bad log entry (or problem with RE?):");
42
+			System.err.println(line);
43
+		} else {
44
+			for(int i = 0; i < filterFields.length; i++) {
45
+				returnString[i] = matcher.group(filterFields[i]);
46
+			}
47
+			if(showDebugOutput) {
48
+				System.out.println("IP Address: " + matcher.group(1));
49
+				System.out.println("User: " + matcher.group(3));
50
+				System.out.println("Date&Time: " + matcher.group(4));
51
+				System.out.println("Request: " + matcher.group(5));
52
+				System.out.println("Response: " + matcher.group(6));
53
+				System.out.println("Bytes Sent: " + matcher.group(7));
54
+				if (!matcher.group(8).equals("-"))
55
+					System.out.println("Referer: " + matcher.group(8));
56
+				System.out.println("Browser: " + matcher.group(9));
57
+			}
58
+		}
59
+		
60
+		return returnString;
61
+	}
62
+	
63
+	private static void parseFilter() {
64
+		if(filter != null && filter != "" && filter != " ") {
65
+			String[] split = filter.split(" ");
66
+			filterFields = new int[split.length];
67
+			for(int i = 0; i < split.length; i++) {
68
+				int id = 2;		// default id... should alwasy be -
69
+				if(split[i].equals("ip"))
70
+					id = 1;
71
+				else if(split[i].equals("user"))
72
+					id = 3;
73
+				else if(split[i].equals("datetime"))
74
+					id = 4;
75
+				else if(split[i].equals("request"))
76
+					id = 5;
77
+				else if(split[i].equals("response"))
78
+					id = 6;
79
+				else if(split[i].equals("size"))
80
+					id = 7;
81
+				else if(split[i].equals("referer"))
82
+					id = 8;
83
+				else if(split[i].equals("browser"))
84
+					id = 9;
85
+					
86
+				filterFields[i] = id;
87
+			}
88
+		} 
89
+	}
90
+	
91
+	public static void main(String argv[]) throws IOException {
92
+		
93
+		if(argv.length < 1) {
94
+			System.err.println("Invalid argument count!");
95
+			System.err.println("Usage: ");
96
+			System.err.println("java LogFileParser <pathtologfile> [options]");
97
+			System.err.println("Options: ");
98
+			System.err.println("-o <parsedoutputfile>");
99
+			System.err.println("-f <filter>");
100
+			System.err.println("Filter: (seperated by space)");
101
+			System.err.println("ip, user, request, response, datetime, size, referer, browser");
102
+			System.exit(1);
103
+		} else {
104
+			logFilePath = argv[0];
105
+			logFile = new File(logFilePath);
106
+			if(logFile.canRead()) {
107
+				if(showDebugOutput) {
108
+					System.out.println("Using Logfile: " + logFilePath);
109
+				}
110
+			} else {
111
+				System.err.println("Cannot read logfile: " + logFilePath);
112
+				System.exit(1);
113
+			}
114
+			
115
+			// we have options =)
116
+			if(argv.length > 1) {
117
+				int options = argv.length -1;
118
+				String option = "";
119
+				boolean isOption = false;
120
+				
121
+				for(int i=1; i <= options; i++) {	
122
+					// we have a optionstartpoint...
123
+					if(argv[i].contains("-") && !isOption) {
124
+						isOption = true;
125
+						option = argv[i].substring(1);
126
+					} else if(isOption) {
127
+						if(option.equals("o")) {
128
+							if(argv[i].startsWith("-")) {
129
+								System.err.println("Option -o needs more arguments!");
130
+								System.exit(1);
131
+							} else {
132
+								parsedFilePath = argv[i];
133
+								isOption = false;
134
+							}
135
+						} else if(option.equals("f")) {
136
+							if(argv[i].startsWith("-")) {
137
+								isOption = false;
138
+								i--; // reuse this option!								
139
+							} else {
140
+								filter = filter + argv[i] + " ";	
141
+								
142
+							}
143
+						} else {
144
+							System.err.println("Incorrect option " + option);
145
+							System.exit(1);
146
+						}
147
+					}
148
+					parseFilter();
149
+				}
150
+			}
151
+		}
152
+		
153
+		// Read the logfile line by line
154
+		FileInputStream fileStream = new FileInputStream(logFile);
155
+		DataInputStream in = new DataInputStream(fileStream);
156
+		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
157
+		
158
+		String line;		
159
+		if(parsedFilePath != null && parsedFilePath != "") {
160
+			String parsedLine = "";
161
+			File parsedFile = new File(parsedFilePath);
162
+			if(!parsedFile.canWrite() && !parsedFile.createNewFile()) {
163
+				System.err.println("Cannot write output file: " + parsedFilePath + "!!!");
164
+				System.exit(1);
165
+			}
166
+				
167
+			FileOutputStream fileOut = new FileOutputStream(parsedFile);
168
+			while((line = bufferedReader.readLine()) != null){	
169
+				String[] pLine = parseLine(line);
170
+				for(int i = 0; i < filterFields.length; i++) {
171
+					if(i != 0) {
172
+						parsedLine += FIELD_SPACE;
173
+					}
174
+					parsedLine += pLine[i];
175
+				}
176
+				parsedLine +=  "\n";
177
+				fileOut.write(parsedLine.getBytes());
178
+				parsedLine = "";
179
+			}	
180
+			System.out.println("Output written to file: " + parsedFilePath);
181
+		} else {
182
+			String parsedLine = "";
183
+			while((line = bufferedReader.readLine()) != null){	
184
+				String[] pLine = parseLine(line);
185
+				for(int i = 0; i < filterFields.length; i++) {
186
+					if(i != 0) {
187
+						parsedLine += FIELD_SPACE;
188
+					}
189
+					parsedLine += pLine[i];
190
+				}
191
+				System.out.println(parsedLine);
192
+				parsedLine = "";
193
+			}			
194
+		}		
195
+	}
196
+}

Loading…
Cancel
Save