-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogFileChecker.py
352 lines (275 loc) · 11.8 KB
/
LogFileChecker.py
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
import datetime
import logging
import getpass
import threading
import time
#configuration
pathToConfig = "Property.properties"
logger = logging.getLogger('LogFileChecker')
logger.setLevel(logging.INFO)
# create file handler which logs even debug messages
fh = logging.FileHandler('LogFileChecker.log')
fh.setLevel(logging.INFO)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
'''# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message') '''
class Property():
def __init__(self, checkname):
self.__checkname = str(checkname)
self.__type = None
self.__dateformate = '%b %d %Y'
self.__position = 0
self.__separator = ","
self.__searchPattern = "not defined"
self.__validation = "not defined"
self.__positionToCheck = self.__position+1
self.__scanPath = "/testfile"
#__result and __comparedValue will be set during the execution
self.__result = False
self.__comparedValue = ""
def addType(self, t):
self.__type = t
def addPostion(self, pos):
self.__position = pos
def addSeparator(self, sep):
self.__separater = sep
def addPattern(self, pattern):
self.__searchPattern = pattern
def addPTC(self, ptc):
self.__positionToCheck = ptc
def addScanPath(self, path):
self.__scanPath = path
def addValidation(self, valid):
self.__validation = valid
def setResult(self, result, comparedValue):
self.__result = result
self.__comparedValue = str(comparedValue)
def setPassIfnoMatch(self):
self.__result = True
def setDateFormat(self, vFormat):
self.__dateformate = vFormat
def getResult(self):
return self.__result
def getComparedValue(self):
return self.__comparedValue
def getName(self):
return self.__checkname
def getType(self):
return self.__type
def getPostion(self):
return int(self.__position)
def getSeparator(self):
return self.__separater
def getPattern(self):
return self.__searchPattern
def getPTC(self):
return int(self.__positionToCheck)
def getScanPath(self):
return self.__scanPath
def getValidation(self):
return self.__validation
def getDateFormat(self):
return self.__dateformate
def __str__(self):
return ("Class Property with values - getName: " + self.getName() + " getType: " + self.getType() + " getPostion: " + str(self.getPostion()) + " getSeparator: " + str(self.getSeparator()) + " getPattern: " + str(self.getPattern()) + " getValidation: " + self.getValidation() + " getPTC: " + str(self.getPTC()) + " getScanPath: " + self.getScanPath() + " Pass check if the pattern is not matching: " + str(self.__result))
def __repr__(self):
return self.__str__()
class LoadFile():
def __init__(self, pfad):
try:
self.__file = open(pfad, "r")
except Exception as ex:
# THis will catch any exception!
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
logger.info(message)
self.__filelist = []
for line in self.__file:
self.__filelist.append(line.strip())
logger.debug("loadFile() line -> " + line.strip())
def getFileAsList(self):
#Get file as list as it is.
return self.__filelist
def getFileAsListWOComments(self):
#Get File as a list without comment and empty rows
listWOComments = []
for line in self.__filelist:
if line.strip()[:1] == "#" or line.strip() == "" or not line:
logger.debug("Line with # or None -> " + str(line))
else:
listWOComments.append(line.strip())
logger.debug("Return list w/o Comments: " + str(listWOComments))
return listWOComments
class ResultOutput():
def __init__(self,filename):
self.__file = open(filename, "w")
self.writeHeader()
def writeHeader(self):
writeValue = "**** Scan Log " + str(datetime.datetime.now()) + " by " + getpass.getuser() + " ****\n\n"
self.__file.write(writeValue)
logger.info(writeValue)
def saveResult(self, prop):
writeValue = " " + str(prop.getName()) + "\t type: \t" + prop.getType() + " Compared Value: " + prop.getComparedValue()
if prop.getResult():
writeValue = writeValue + "\n> Passed\n"
else:
writeValue = writeValue + "\n!-> Failed!!!\n"
logger.info(writeValue)
self.__file.write(writeValue)
def writeEOF(self):
writeValue= "\n**** All Test executed: " + str(datetime.datetime.now()) + " ****"
self.__file.write(writeValue)
logger.info(writeValue)
class LoadProperties():
def __init__(self, propertiesRAWFile):
self.__properties = []
self.__importProperties(propertiesRAWFile)
def __importProperties(self, fileAsList):
prop = None
propstring = "No Properties"
for line in fileAsList:
logger.debug("RAW Line: "+ str(line))
if len(line.split("=")) < 1 and len(line.split("=")) > 1:
logger.debug("Invalid line ignored: " + line)
continue
else:
identifier = line.split("=")[0].strip().lower()
value = line.split("=")[1]
logger.debug("LINE: identifier=" + identifier + " Value="+value)
if prop == None and identifier == "name":
prop = Property(value.strip())
self.__properties.append(prop)
continue
elif prop != None and identifier == "name":
prop = Property(value.strip())
self.__properties.append(prop)
continue
elif identifier == "type":
prop.addType(value.strip())
continue
elif identifier == "position":
prop.addPostion(int(value.strip()))
continue
elif identifier == "separator":
prop.addSeparator(value)
continue
elif identifier == "searchpattern":
prop.addPattern(value.strip())
continue
elif identifier == "dateformat":
prop.setDateFormat(value.strip())
continue
elif identifier == "validation":
prop.addValidation(value.strip())
continue
elif identifier == "postiontocheck":
prop.addPTC(int(value.strip()))
continue
elif identifier == "scanpath":
prop.addScanPath(value.strip())
continue
elif identifier == "passifnomatch" and value.strip() == "True":
prop.setPassIfnoMatch()
logger.debug("Set PassIfnoMatch to true")
continue
logger.debug("Propertiies list: " + str(self.__properties))
def getPropertiesAsList(self):
return self.__properties
class Scan():
def __init__(self, propertiesList, resultOutput):
self.__propertiesList = propertiesList
self.__resultOutput = resultOutput
def startScan(self):
logger.debug("Start file scan")
for prop in self.__propertiesList:
try:
self.__check(prop)
except Exception as ex:
# THis will catch any exception!
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
logger.info(message)
prop.setResult(False, message)
self.__resultOutput.saveResult(prop)
def __check(self, prop):
file = LoadFile(prop.getScanPath()).getFileAsListWOComments()
dataFromFile = []
patternFound = False
cResult = True
for line in file:
sLine = line.split(prop.getSeparator())
patternYes = self.__line(sLine, prop)
if patternYes == "":
continue
else:
logger.debug("Data from File: " + (line[prop.getPTC()]))
dataFromFile.append(sLine[prop.getPTC()].strip())
logger.debug("Data to compare: " +str(dataFromFile))
for itmeToCheck in dataFromFile:
if cResult == True:
cResult = self.__compare(prop, itmeToCheck)
logger.debug("Scan Result: "+ str(cResult) + " Compared Value: " + itmeToCheck)
prop.setResult(cResult, itmeToCheck)
else:
logger.debug("Scan Result: "+ str(cResult) + " Compared Value: " + itmeToCheck)
cResult = False
continue
def __compare(self, prop, dataFromFile):
passedBol = False
if prop.getType().lower() == "date":
today = datetime.datetime.combine(datetime.datetime.today().date(), datetime.datetime.min.time())
dateToCompare = today - datetime.timedelta(days=int(prop.getValidation()))
logger.debug("Validation Date: " + str(dateToCompare))
logger.debug("Date from file Plain String: " + dataFromFile)
logger.debug("ValidateToCompare dation Date: " + str(datetime.datetime.strptime(dataFromFile, prop.getDateFormat())))
if dateToCompare <= datetime.datetime.strptime(dataFromFile, prop.getDateFormat()):
passedBol = True
else:
passedBol = False
elif prop.getType().lower() == "integer":
logger.debug("Validation int: " + str(prop.getValidation()))
logger.debug("Date from file Plain int: " + str(dataFromFile))
if int(dataFromFile) == int(prop.getValidation()):
passedBol = True
else:
passedBol = False
elif prop.getType().lower() == "string":
logger.debug("Validation String: " + str(prop.getValidation()))
logger.debug("Date from file Plain String: " + str(dataFromFile))
if str(dataFromFile) == str(prop.getValidation()):
passedBol = True
else:
passedBol = False
return passedBol
def __line(self, splitedLine, prop):
returnVar = ""
if len(splitedLine) >= prop.getPTC():
logger.debug("Line on position: " + splitedLine[prop.getPostion()].strip() )
if splitedLine[prop.getPostion()].strip() == prop.getPattern().strip():
logger.debug("Pattern found: " + str(splitedLine) + " Pattern: " + splitedLine[prop.getPostion()])
returnVar = splitedLine[prop.getPTC()]
else:
logger.debug("Pattern NOT found: " + str(prop.getPattern()) + " String: " + str(splitedLine))
returnVar = ""
else:
logger.debug("Pattern line is empty or out of range " + str(splitedLine))
returnVar = ""
return returnVar
#Start
properties = LoadProperties(LoadFile(pathToConfig).getFileAsListWOComments()).getPropertiesAsList()
output = ResultOutput("Scan Result " + datetime.datetime.now().strftime('%Y-%b-%d-%H%M') + "by " + getpass.getuser() +".log")
Scan(properties, output).startScan()
output.writeEOF()