aboutsummaryrefslogtreecommitdiffstats
path: root/LogParser/src/main/java/org/openecomp/xacml/parser/ParseLog.java
blob: 5303bacc2fa6fb86c098af4bae9fb00c31684d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*-
 * ============LICENSE_START=======================================================
 * LogParser
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.xacml.parser;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.openecomp.policy.common.im.AdministrativeStateException;
import org.openecomp.policy.common.im.IntegrityMonitor;
import org.openecomp.policy.common.im.StandbyStatusException;

import org.apache.log4j.Logger;
import org.openecomp.xacml.parser.LogEntryObject.LOGTYPE;

/**
 * Parse log files and store the information in a H2 database.
 * 
 *
 */
public class ParseLog {
	
	private static final Logger logger = Logger.getLogger(ParseLog.class.getName());

	private static String system;
	private static int lastNumberRead = 0;
	private static String type;
	private static long startFileSize;
	private static String systemLogFile;
	private static String logFile;
	
	private static String JDBC_URL;
	private static String JDBC_USER;
	private static String JDBC_PASSWORD = "";
	private static String JDBC_DRIVER;
	private static int maxLength = 255;   //Max length that is allowed in the DB table
	private static String resourceName;
	private static long sleepTimer = 50000;
	static IntegrityMonitor im;
	
	public static void main(String[] args) throws Exception {

		Properties logProperties = getPropertiesValue("parserlog.properties");
		Path filePath = Paths.get(logFile);
		File file = new File(logFile);
		File fileLog = new File(systemLogFile);
		startFileSize = file.length();
			
		im = IntegrityMonitor.getInstance(resourceName,logProperties );
		
		logger.info("System: " + system );  
		logger.info("System type: " + type );  
		logger.info("Logging File: " + systemLogFile );
		logger.info("log file: " + logFile);
		logger.info("JDBC_URL: " + JDBC_URL);
		logger.info("JDBC_DRIVER: " + JDBC_DRIVER);
		
		String filesRead = PullLastLineRead(fileLog);
		if (filesRead!= null){			
			filesRead = filesRead.replaceAll("(\\r\\n|\\n)", "<br />");
			lastNumberRead= Integer.parseInt(filesRead.trim());
		}else{
			lastNumberRead = 0;
		}
		startFileSize =  countLines(logFile);
		logger.info("File Line Count: " + startFileSize + " value read in: " + lastNumberRead);
		if (startFileSize < lastNumberRead ){
			logger.error("Filed Rolled: set Last number read to 0");
			lastNumberRead = 0;
		}
		Runnable  runnable = new Runnable (){
		public void run(){
			while (true){		
	             
				if (file.isFile()){
					try (Stream<String> lines = Files.lines(filePath, Charset.defaultCharset()).onClose(() -> logger.info("Last line Read: " + lastNumberRead)).skip(lastNumberRead)) {
						
						lines.forEachOrdered(line -> process(line, type));
		
					} catch (IOException e) {
						logger.error("Error processing line in log file: " + e);
					}	
				}
				try {
					Thread.sleep(sleepTimer);
					startFileSize =  countLines(logFile);
				} catch (InterruptedException | IOException e) {
					logger.error("Error: " + e);
				}
				
				logger.info("File Line Count: " + startFileSize + " value read in: " + lastNumberRead);
				if (startFileSize < lastNumberRead ){
					logger.info("Failed Rolled: set Last number read to 0");
					lastNumberRead = 0;
				}
			}	
		}
		};
		
		Thread thread = new Thread(runnable);
		thread.start();

	}			

	public static int countLines(String filename) throws IOException {
	    LineNumberReader reader  = new LineNumberReader(new FileReader(filename));
	    int cnt = 0;
	    String lineRead = "";
	    while ((lineRead = reader.readLine()) != null) {}

	    cnt = reader.getLineNumber(); 
	    reader.close();
	    return cnt;
	}	
	
	public static String PullLastLineRead(File file) throws IOException {
		if(!file.exists()){
			file.createNewFile();
			return null;
		}
		RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        int lines = 0;
        StringBuilder builder = new StringBuilder();
        long length = file.length();
        length--;
        randomAccessFile.seek(length);
        for(long seek = length; seek >= 0; --seek){
            randomAccessFile.seek(seek);
            char c = (char)randomAccessFile.read();
            builder.append(c);
            if(c == '\n'){
                builder = builder.reverse();
                if (builder.toString().contains("Last line Read:")){
            		String[] parseString = builder.toString().split("Last line Read:");
            		String returnValue = parseString[1].replace("\r", "");
            		return returnValue.trim();
            		//return parseString[2].replace("\r", "");
            	}
                lines++;
                builder = null;
                builder = new StringBuilder();
             }

        }
		return null;
	}

	public static LogEntryObject pullOutLogValues(String line, String type){
		Date date;
		LogEntryObject logEntry = new LogEntryObject();
		logEntry.setSystemType(type);
		String description = null;
		
		logEntry.setSystem(system);
		
		//Values for PDP/PAP log file
		if(line.contains("||INFO||") || line.contains("||ERROR||")){
			String[] splitString = line.split("[||]");
			String dateString = splitString[0].substring(0, 19);
			logEntry.setDescription(splitString[splitString.length-1]);	

			//parse out date
			date = parseDate(dateString.replace("T", " "), "yyyy-MM-dd HH:mm:ss", false);
			logEntry.setDate(date);
			
			logEntry.setRemote(parseRemoteSystem(line));
			if (line.contains("||INFO||")){
				logEntry.setLogType(LOGTYPE.INFO);
			}else{
				logEntry.setLogType(LOGTYPE.ERROR);
			}		
		}else if (line.contains("INFO") && line.contains(")-")){
			//parse out description
			logEntry.setDescription(line.substring(line.indexOf(")-")+3));

			date = parseDate(line, "yy_MM_dd_HH_mm_ss", true);
			logEntry.setDate(date);
	
			logEntry.setRemote(parseRemoteSystem(line));
			logEntry.setLogType(LOGTYPE.INFO);
		} else if (line.contains("INFO") && line.contains("--- [")){
			//parse out description
			String temp = line.substring(line.indexOf("---")+1);
			String[] split = temp.split(":");

			logEntry.setDescription(split[1]);

			//parse out date
			date = parseDate(line, "yyyy-MM-dd HH:mm:ss", false);
			logEntry.setDate(date);
			
			//remote system
			logEntry.setRemote(parseRemoteSystem(line));
			logEntry.setLogType(LOGTYPE.INFO);
		}else if (line.contains("SEVERE") && line.contains("[main]")){			
			String[] splitString = line.split(" ");
			
			for (int i = 5; i < splitString.length; i++){
				description = description +  " " + splitString[i];
			}

			logEntry.setDescription(description);
			//parse out date
			date = parseDate(line, "dd-MMM-yyyy HH:mm:ss", false);
			logEntry.setDate(date);
			logEntry.setLogType(LOGTYPE.SEVERE);
		} else if (line.contains("WARN") && line.contains(")-")){
			//parse out description

			logEntry.setDescription(line.substring(line.indexOf(")-")+3));

			//parse out date
			date = parseDate(line, "yy_MM_dd_HH_mm_ss", true);
			logEntry.setDate(date);
			
			//remote system
			logEntry.setRemote(parseRemoteSystem(line));
			logEntry.setLogType(LOGTYPE.WARN);
		}else if (line.contains("WARNING") && type =="PyPDP"){
			String[] splitString = line.split(" ");
			for (int i = 5; i < splitString.length; i++){
				description = description +  " " + splitString[i];
			}

			//parse out date
			date = parseDate(line, "dd-MMM-yyyy HH:mm:ss", false);
			logEntry.setDate(date);
			logEntry.setLogType(LOGTYPE.WARN);
		}else if (line.contains("ERROR") && line.contains(")-")){
			//parse out description
			description = line.substring(line.indexOf(")-")+3);

			//parse out date
			date = parseDate(line, "yy_MM_dd_HH_mm_ss", true);
			logEntry.setDate(date);
			//remote system
			logEntry.setRemote(parseRemoteSystem(line));
			logEntry.setLogType(LOGTYPE.ERROR);
		}else {
			return null;
		}
		

		return logEntry;
	}

	private static void DBClose(Connection conn) {
		try {
			conn.close();
		} catch (SQLException e) {
			logger.error("Error closing DB Connection: " + e);
			
		}
	}

	public static void process(String line, String type)  {
		LogEntryObject returnLogValue = null;
		if (im!=null){
			try {
				im.startTransaction();
			} catch (AdministrativeStateException e) {
				logger.error("Error received" + e);
				
			} catch (StandbyStatusException e) {
				logger.error("Error received" + e);
			}
		}
		returnLogValue = pullOutLogValues(line, type);
		lastNumberRead++;
		if (returnLogValue!=null){
			writeDB(returnLogValue);
		}
		if (im!=null){
			im.endTransaction();
		}
	}
	
	private static void writeDB(LogEntryObject returnLogValue) {
		Connection conn = DBConnection(JDBC_DRIVER, JDBC_URL, JDBC_USER,JDBC_PASSWORD);
		DBAccesss(conn, returnLogValue.getSystem(), returnLogValue.getDescription(),  
						returnLogValue.getDate(), returnLogValue.getRemote(), 
						returnLogValue.getSystemType(), returnLogValue.getLogType().toString());
		DBClose(conn);	
	}

	private static Connection DBConnection(String driver, String jdbc, String user, String pass){
        
        try {
        	Class.forName(driver);
			Connection conn = DriverManager.getConnection(jdbc, user, pass);
			return conn;
		} catch ( Exception e) {
			logger.error("Error connecting to DB: " + e);
		}
		return null;
	}
	private static void DBAccesss(Connection conn, String system, String description, Date date, String remote, String type, String logType)  {
		
		String sdate = null;
		
		if (date!=null){
			Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			sdate = formatter.format(date);		
		}
		
		//ensure the length of description is less than the maximumm db char length
		if (description.length() > maxLength) {
			description = description.substring(0, maxLength);
		}
		
		try {
			PreparedStatement prep = conn.prepareStatement("insert into SYSTEMLOGDB values (NULL, ?, ?, ?,  ?,  ?, ?);");
			prep.setString(1, system);
			prep.setString(2, description);
			prep.setString(3, remote);
			prep.setString(4, type);
			prep.setString(5, sdate);
			prep.setString(6, logType);

			prep.executeUpdate();
			prep.close();

		} catch (SQLException e1) {
			logger.error("Error trying to excute SQL Statment: " + e1);
		}
	}

	public static Date parseDate(String dateline, String pattern, boolean singleSplit)  {
		
		Date returnDate;
		String[] splitString = dateline.split(" ");
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);	
		if (singleSplit){
			try {
				returnDate = formatter.parse(splitString[0]);
			} catch (ParseException e) {
				logger.error("Unable to parse date for line: " + dateline);
				returnDate = null;
			}
		}else{
			String tmpString = splitString[0] + " " + splitString[1];
			try {
				returnDate = formatter.parse(tmpString);
			} catch (ParseException e) {
				logger.error("Unable to parse date for line: " + dateline);
				returnDate = null;
			}
		}
			
		return returnDate; 
	}

	
	public static String parseRemoteSystem(String line) {
		
		if (line.contains("http") && !(line.contains("www.w3.org"))){
	
			Pattern pattern = Pattern.compile("://(.+?)/");
			Matcher remote = pattern.matcher(line);
			if (remote.find())
			{
				return remote.group(1);
			} 
		}
		return null;
	}
	
	public static Properties getPropertiesValue(String fileName) {
		Properties config = new Properties();
		Path file = Paths.get(fileName);
		if (Files.notExists(file)) {
			logger.info("File doesn't exist in the specified Path "	+ file.toString());
		}else{ 
			if (file.toString().endsWith(".properties")) {
				InputStream in;
				try {
					in = new FileInputStream(file.toFile());
					config.load(in);
							
					resourceName = config.getProperty("RESOURCE_NAME");
					system = config.getProperty("SERVER");
					type = config.getProperty("LOGTYPE");
					systemLogFile = config.getProperty("PARSERLOGPATH");
					logFile = config.getProperty("LOGPATH");
					JDBC_URL = config.getProperty("JDBC_URL").replace("'", "");
					JDBC_USER = config.getProperty("JDBC_USER");
					JDBC_DRIVER =  config.getProperty("JDBC_DRIVER");
					JDBC_PASSWORD = config.getProperty("JDBC_PASSWORD");
					return config;

				} catch (IOException e) {					
					logger.info("Error porcessing Cofnig file will be unable to create Health Check");
				}
				
			}
		}
		return null;
	}	
}