|
Added
Link Here
|
| 1 |
/********************************************************************** |
| 2 |
* Copyright (c) 2006 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* $Id: WindowsLogParser.java,v 1.1 2006/02/14 07:15:40 roshetty Exp $ |
| 8 |
* |
| 9 |
* Contributors: |
| 10 |
* IBM - Initial API and implementation |
| 11 |
**********************************************************************/ |
| 12 |
package org.eclipse.tptp.monitoring.auto.tests.utils; |
| 13 |
//org.eclipse.tptp.monitoring.auto.tests.utils; |
| 14 |
|
| 15 |
import java.text.ParsePosition; |
| 16 |
import java.text.SimpleDateFormat; |
| 17 |
import java.util.Calendar; |
| 18 |
import java.util.Hashtable; |
| 19 |
|
| 20 |
import org.eclipse.hyades.logging.core.Guid; |
| 21 |
import org.eclipse.hyades.logging.events.cbe.CommonBaseEvent; |
| 22 |
import org.eclipse.hyades.logging.events.cbe.ComponentIdentification; |
| 23 |
import org.eclipse.hyades.logging.events.cbe.ReportSituation; |
| 24 |
import org.eclipse.hyades.logging.events.cbe.Situation; |
| 25 |
import org.eclipse.hyades.logging.parsers.LogParserException; |
| 26 |
import org.eclipse.hyades.logging.parsers.MonitoringParser; |
| 27 |
import org.eclipse.hyades.logging.parsers.ParserConstants; |
| 28 |
|
| 29 |
public class WindowsLogParser extends MonitoringParser |
| 30 |
{ |
| 31 |
//Variable to hold the system timezone value |
| 32 |
private String timezone; |
| 33 |
|
| 34 |
public static final String WINDOWS_DATE_FORMAT_1 = "dd/MM/yyyy,hh:mm:ss aa"; |
| 35 |
public static final String WINDOWS_DATE_FORMAT_2 = "dd/MM/yyyy,HH:mm:ss"; |
| 36 |
|
| 37 |
// protected static Locale parseLocale = new Locale("en", "US"); |
| 38 |
// protected static SimpleDateFormat dateFormat = new SimpleDateFormat(LogParserConstants.WAS_INSTALL_LOG_DATE_FORMAT1,parseLocale); |
| 39 |
//protected static SimpleDateFormat cbeDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",parseLocale); |
| 40 |
|
| 41 |
protected static SimpleDateFormat dateFormat_1 = new SimpleDateFormat(WINDOWS_DATE_FORMAT_1); |
| 42 |
protected static SimpleDateFormat dateFormat_2 = new SimpleDateFormat(WINDOWS_DATE_FORMAT_2); |
| 43 |
protected static SimpleDateFormat cbeDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); |
| 44 |
|
| 45 |
// Default Constructor, also used to initialize all member variables |
| 46 |
public WindowsLogParser() { |
| 47 |
//intialize the other variables |
| 48 |
timezone = ""; |
| 49 |
inittimezone(); |
| 50 |
} |
| 51 |
|
| 52 |
/* |
| 53 |
* this is an renamed function of setUserInput, (non-Javadoc) |
| 54 |
* @see org.eclipse.hyades.logging.parsers.IParser#setConfiguration(java.util.Hashtable) |
| 55 |
*/ |
| 56 |
public void setConfiguration(Hashtable table) throws LogParserException { |
| 57 |
super.setConfiguration(table); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Determine the situation based on the situation code value |
| 63 |
*/ |
| 64 |
private Situation createSituation() |
| 65 |
{ |
| 66 |
Situation cbeSituation = eventFactory.createSituation(); |
| 67 |
ReportSituation cbeReportSituation = eventFactory.createReportSituation(); |
| 68 |
cbeReportSituation.setReasoningScope(ParserConstants.INTERNAL_REASONING_SCOPE); |
| 69 |
cbeReportSituation.setReportCategory(ParserConstants.LOG_REPORT_CATEGORY); |
| 70 |
cbeSituation.setCategoryName(ParserConstants.REPORT_SITUATION_CATEGORY_NAME); |
| 71 |
cbeSituation.setSituationType(cbeReportSituation); |
| 72 |
return cbeSituation; |
| 73 |
} |
| 74 |
|
| 75 |
private void parseRecord(String line,CommonBaseEvent cbe) throws LogParserException |
| 76 |
{ |
| 77 |
// Initialize the CBE |
| 78 |
cbe.init(); |
| 79 |
|
| 80 |
// Set the event's globalInstanceId property with a new GUID |
| 81 |
cbe.setGlobalInstanceId(Guid.generate()); |
| 82 |
|
| 83 |
// Set the ExtensionName |
| 84 |
cbe.setExtensionName("CommonBaseEvent"); |
| 85 |
|
| 86 |
//Set the Situation value |
| 87 |
cbe.setSituation(createSituation()); |
| 88 |
|
| 89 |
for(int i=0;i<=7;i++) |
| 90 |
{ |
| 91 |
int idx = line.indexOf(','); |
| 92 |
String snippet = (idx >-1 && i < 7)?line.substring(0,idx):line; |
| 93 |
line = line.substring(idx+1); |
| 94 |
switch(i) |
| 95 |
{ |
| 96 |
case 0: |
| 97 |
//parse time |
| 98 |
idx = line.indexOf(','); |
| 99 |
snippet = snippet + "," + line.substring(0,idx); |
| 100 |
line = line.substring(idx+1); |
| 101 |
String cbetime = getCBEDate(snippet,(snippet.endsWith("M")?dateFormat_1:dateFormat_2)); |
| 102 |
cbetime = cbetime.concat(".000"); |
| 103 |
cbetime = cbetime.concat(timezone); |
| 104 |
cbe.setCreationTime(cbetime); |
| 105 |
break; |
| 106 |
case 1: |
| 107 |
//Create source component id and set the values |
| 108 |
ComponentIdentification srcComponent = eventFactory.createComponentIdentification() ; |
| 109 |
srcComponent.init(); |
| 110 |
srcComponent.setComponent("Windows"); |
| 111 |
srcComponent.setComponentIdType("ProductName"); |
| 112 |
srcComponent.setSubComponent(snippet); |
| 113 |
srcComponent.setComponentType("WindowsOperatingSystem"); |
| 114 |
cbe.setSourceComponentId(srcComponent); |
| 115 |
break; |
| 116 |
case 2: |
| 117 |
//Set the severity value |
| 118 |
cbe.setSeverity(getSeverity(snippet)); |
| 119 |
break; |
| 120 |
case 3: |
| 121 |
break; |
| 122 |
case 4: |
| 123 |
// code to conver to Hexadecimal as the eventReader.exe displays this is hex |
| 124 |
// Remove the below try-catch if the exe is change to output decimal |
| 125 |
/*try |
| 126 |
{ |
| 127 |
snippet = Long.toHexString(Long.parseLong(snippet)); |
| 128 |
snippet = snippet.toUpperCase(); |
| 129 |
|
| 130 |
for(int len = snippet.length();len < 8; len ++) |
| 131 |
{ |
| 132 |
snippet = 0 + snippet; |
| 133 |
} |
| 134 |
} |
| 135 |
catch(Exception e){}*/ |
| 136 |
//Uncomment the above code and comment the line below once bugzilla 176476 has been fixed. |
| 137 |
snippet = (recordCount + 1) + ""; |
| 138 |
cbe.setMsgDataElement(eventFactory.createMsgDataElement()); |
| 139 |
cbe.getMsgDataElement().init(); |
| 140 |
cbe.getMsgDataElement().setMsgId(snippet); |
| 141 |
cbe.getMsgDataElement().setMsgIdType("UNKNOWN"); |
| 142 |
break; |
| 143 |
case 5: |
| 144 |
if(!(snippet.trim().equals(""))) |
| 145 |
{ |
| 146 |
cbe.addExtendedDataElement(createStringEDE("Username",snippet.substring(snippet.indexOf('\\')+1))); |
| 147 |
} |
| 148 |
break; |
| 149 |
case 6: |
| 150 |
// Set the ReporterComponent values |
| 151 |
ComponentIdentification reporterComponent = eventFactory.createComponentIdentification() ; |
| 152 |
reporterComponent.init(); |
| 153 |
reporterComponent.setLocationType("Hostname"); |
| 154 |
reporterComponent.setComponent("ACAdapter"); |
| 155 |
reporterComponent.setComponentType("WindowsOperatingSystem"); |
| 156 |
reporterComponent.setComponentIdType("APPLICATION"); |
| 157 |
reporterComponent.setSubComponent("NTEventLogSensor"); |
| 158 |
cbe.setReporterComponentId(reporterComponent); |
| 159 |
|
| 160 |
if(!(snippet.trim().equals(""))) |
| 161 |
{ |
| 162 |
cbe.getReporterComponentId().setLocation(snippet); |
| 163 |
cbe.getReporterComponentId().setLocationType("Hostname"); |
| 164 |
cbe.getSourceComponentId().setLocation(snippet); |
| 165 |
cbe.getSourceComponentId().setLocationType("Hostname"); |
| 166 |
} |
| 167 |
else |
| 168 |
{ |
| 169 |
cbe.getReporterComponentId().setLocation(localHostId); |
| 170 |
cbe.getReporterComponentId().setLocationType(localHostIdFormat); |
| 171 |
cbe.getSourceComponentId().setLocation(localHostId); |
| 172 |
cbe.getSourceComponentId().setLocationType(localHostIdFormat); |
| 173 |
} |
| 174 |
break; |
| 175 |
case 7: |
| 176 |
////System.out.println("*****messageString****** "+snippet); |
| 177 |
String messageString = undoCVSFormating(snippet); |
| 178 |
////System.out.println("*****messageString****** "+messageString); |
| 179 |
if(messageString.equals("")) break; |
| 180 |
|
| 181 |
if (messageString.length() > 1024) |
| 182 |
{ |
| 183 |
cbe.setMsg(messageString.substring(0, 1024)); |
| 184 |
cbe.addExtendedDataElement(createStringEDE(ParserConstants.MESSAGE_EXTENDED_DATA_ELEMENT,messageString.toString())); |
| 185 |
} |
| 186 |
else |
| 187 |
{ |
| 188 |
cbe.setMsg(messageString.toString()); |
| 189 |
} |
| 190 |
break; |
| 191 |
default: |
| 192 |
break; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
private String undoCVSFormating(String msgString) |
| 198 |
{ |
| 199 |
msgString = msgString.replaceAll("\"\"", "\""); |
| 200 |
msgString = msgString.replaceAll("\n", " "); |
| 201 |
msgString = msgString.replaceAll("\r", " "); |
| 202 |
|
| 203 |
if(msgString.startsWith("\"") && msgString.endsWith("\"")) |
| 204 |
{ |
| 205 |
msgString = msgString.substring(1,msgString.length()-1); |
| 206 |
} |
| 207 |
if(msgString.matches("The description for Event ID .* in Source .* cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the .* flag to retrieve this description; see Help and Support for details. The following information is part of the event:.*")) |
| 208 |
{ |
| 209 |
msgString = msgString.replaceAll("The description for Event ID .* in Source .* cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event:", ""); |
| 210 |
msgString = msgString.trim(); |
| 211 |
msgString = msgString.substring(0, msgString.length()-1); |
| 212 |
} |
| 213 |
if(msgString.equals("The event log file is corrupt.")) |
| 214 |
{ |
| 215 |
msgString = ""; |
| 216 |
} |
| 217 |
return msgString.trim(); |
| 218 |
} |
| 219 |
|
| 220 |
private String nextLine = null; |
| 221 |
|
| 222 |
private String getLogRecord() throws LogParserException |
| 223 |
{ |
| 224 |
String record = nextLine; |
| 225 |
nextLine = null; |
| 226 |
String line=null; |
| 227 |
|
| 228 |
while ((line=readALine()) != null) |
| 229 |
{ |
| 230 |
if(line.length() == 0) line = " "; |
| 231 |
|
| 232 |
try |
| 233 |
{ |
| 234 |
if(line.matches("\\d{2}/\\d{2}/\\d{4},\\d{2}:\\d{2}:\\d{2}[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,.*")) |
| 235 |
{ |
| 236 |
if(record == null) |
| 237 |
{ |
| 238 |
record = line; |
| 239 |
} |
| 240 |
else |
| 241 |
{ |
| 242 |
nextLine = line; |
| 243 |
break; |
| 244 |
} |
| 245 |
} |
| 246 |
else if(record != null) |
| 247 |
{ |
| 248 |
record = record + ParserConstants.LINE_SEPARATOR + line; |
| 249 |
} |
| 250 |
} |
| 251 |
catch(Exception e) |
| 252 |
{ |
| 253 |
if(record != null) |
| 254 |
{ |
| 255 |
record = record + ParserConstants.LINE_SEPARATOR + line; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
return record; |
| 261 |
} |
| 262 |
/** |
| 263 |
* to parse Severity of the record |
| 264 |
* @param message id |
| 265 |
* @return |
| 266 |
*/ |
| 267 |
private short getSeverity(String severString) |
| 268 |
{ |
| 269 |
if(severString.endsWith("Success Audit")) |
| 270 |
return 15; |
| 271 |
else if(severString.endsWith("Failure Audit")) |
| 272 |
return 16; |
| 273 |
else if(severString.endsWith("Information")) |
| 274 |
return 20; |
| 275 |
else if(severString.endsWith("Warning")) |
| 276 |
return 30; |
| 277 |
else if(severString.endsWith("Error")) |
| 278 |
return 40; |
| 279 |
else |
| 280 |
return 17; |
| 281 |
} |
| 282 |
|
| 283 |
/* |
| 284 |
* Intialize the timezone value to the system default timezone value |
| 285 |
*/ |
| 286 |
private void inittimezone() |
| 287 |
{ |
| 288 |
//intialize the timezone value |
| 289 |
Calendar localCalendar = Calendar.getInstance(); |
| 290 |
int utcOffset = ((localCalendar.get(Calendar.ZONE_OFFSET) + localCalendar.get(Calendar.DST_OFFSET)) / 60000); |
| 291 |
if (utcOffset < 0) { |
| 292 |
timezone = timezone.concat("-"); |
| 293 |
} |
| 294 |
else { |
| 295 |
timezone = timezone.concat("+"); |
| 296 |
} |
| 297 |
utcOffset = Math.abs(utcOffset); |
| 298 |
// Compute the Time Zone value (+/-hh:mm) |
| 299 |
String numberHours = String.valueOf(utcOffset / 60); |
| 300 |
if (numberHours.length() == 1) { |
| 301 |
timezone = timezone.concat("0"); |
| 302 |
} |
| 303 |
timezone = timezone.concat(numberHours); |
| 304 |
timezone = timezone.concat(":"); |
| 305 |
String numberMinutes = String.valueOf(utcOffset % 60); |
| 306 |
if (numberMinutes.length() == 1) { |
| 307 |
timezone = timezone.concat("0"); |
| 308 |
} |
| 309 |
timezone = timezone.concat(numberMinutes); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* Parses the log file, generates a CBE for each parsed log record and |
| 315 |
* returns the array of CBE's. |
| 316 |
* @return CommonBaseEvent[] The array of generated CBE objects. |
| 317 |
* @exception LogParserException thrown if an exception is encountered during parsing |
| 318 |
*/ |
| 319 |
public CommonBaseEvent[] parseNext() throws LogParserException |
| 320 |
{ |
| 321 |
try |
| 322 |
{ |
| 323 |
// counter for the number of CBE events converted |
| 324 |
arrayIndex = 0; |
| 325 |
String currentLine = null; |
| 326 |
while((currentLine = getLogRecord()) != null) |
| 327 |
{ |
| 328 |
currentLine = currentLine.trim(); |
| 329 |
//System.out.println("*****currentLine**** "+currentLine); |
| 330 |
if(messages[arrayIndex] == null) |
| 331 |
{ |
| 332 |
messages[arrayIndex] = eventFactory.createCommonBaseEvent(); |
| 333 |
} |
| 334 |
|
| 335 |
if(currentLine.length() == 0) { |
| 336 |
continue; |
| 337 |
} |
| 338 |
if (currentLine.length()> 0) |
| 339 |
{ |
| 340 |
parseRecord(currentLine,messages[arrayIndex]); |
| 341 |
|
| 342 |
arrayIndex++; |
| 343 |
recordCount++; |
| 344 |
} //if condition block |
| 345 |
if(arrayIndex == MessageArraySize) |
| 346 |
{ |
| 347 |
arrayIndex = 0; |
| 348 |
return messages; |
| 349 |
} |
| 350 |
} |
| 351 |
if (arrayIndex == 0) { |
| 352 |
setEndOfFile(); |
| 353 |
return null; |
| 354 |
} |
| 355 |
else { |
| 356 |
for (int index = arrayIndex; index < MessageArraySize; index++) |
| 357 |
{ |
| 358 |
messages[index] = null; |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
catch(Exception e) { |
| 363 |
throw new LogParserException("Internal Error occurred in Windows CSV parser - " + e.getMessage()); |
| 364 |
} |
| 365 |
if(recordCount == 0) { |
| 366 |
throw new LogParserException("The Windows CSV log does not have any records"); |
| 367 |
} |
| 368 |
return messages; |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* This method converts the given date into CBE Date format |
| 373 |
* @param dateString |
| 374 |
* @param dateFormat |
| 375 |
* @return |
| 376 |
*/ |
| 377 |
private String getCBEDate(String dateString,SimpleDateFormat dateFormat) |
| 378 |
{ |
| 379 |
try |
| 380 |
{ |
| 381 |
//Date creationDateObj = dateFormat.parse(dateString); |
| 382 |
ParsePosition parseLoc = new ParsePosition(0); |
| 383 |
java.util.Date creationDateObj = dateFormat.parse(dateString, parseLoc); |
| 384 |
if(creationDateObj != null) |
| 385 |
{ |
| 386 |
StringBuffer cbeCreationDate = new StringBuffer(cbeDateFormat.format(creationDateObj)); |
| 387 |
return cbeCreationDate.toString(); |
| 388 |
} |
| 389 |
} |
| 390 |
catch(Exception pe) |
| 391 |
{ |
| 392 |
//pe.printStackTrace(); |
| 393 |
// do nothing |
| 394 |
} |
| 395 |
return null; |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Initialize this parser. |
| 400 |
*/ |
| 401 |
public void preParse() throws LogParserException { |
| 402 |
super.preParse(); |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Returns the name of the parser (non-Javadoc) |
| 407 |
* @see org.eclipse.hyades.logging.parsers.Parser#getName() |
| 408 |
*/ |
| 409 |
public String getName() { |
| 410 |
return "Windows application log"; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Returns current Version |
| 415 |
*/ |
| 416 |
public String getVersion() { |
| 417 |
return "windows"; |
| 418 |
} |
| 419 |
|
| 420 |
} |