This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParserLog.py
396 lines (328 loc) · 13.4 KB
/
ParserLog.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
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
#
# License:
#
# Copyright (c) 2003-2006 ossim.net
# Copyright (c) 2007-2014 AlienVault
# All rights reserved.
#
# This package is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
# You may not use, modify or distribute this program under any other version
# of the GNU General Public License.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301 USA
#
#
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL-2'.
#
# Otherwise you can read it here: http://www.gnu.org/licenses/gpl-2.0.txt
#
#
# GLOBAL IMPORTS
#
import os, time, re
import pyinotify #deb package python-pyinotify
# python-pyinotify version 0.7.1-1
from threading import Lock
#
# LOCAL IMPORTS
#
from Detector import Detector
from Event import Event,EventIdm
from Logger import Logger, Lazyformat
from TailFollowBookmark import TailFollowBookmark
import glob
logger = Logger.logger
class RuleMatch:
NEWLINE = "\\n"
def __init__(self, name, rule, plugin):
logger.debug("Adding rule (%s).." % (name))
self.rule = rule
self.name = name
self.plugin = plugin
# store {precheck:, regexp: , pattern: , result: } hashes
self.lines = []
self.encode = plugin.get('config','encoding')
# precheck
# The precheck directive allows for a particular string
# to be searched as a prerequisite, before conducting
# an expensive regex.search()
#
try:
precheck = self.rule["precheck"]
except:
precheck = ""
regexp = self.rule["regexp"]
regex_flags = re.IGNORECASE | re.UNICODE
for r in regexp.split(RuleMatch.NEWLINE):
try:
self.lines.append({
"precheck": "",
"regexp": r,
"pattern": re.compile(r, regex_flags),
"result": None})
except Exception, e:
logger.error("Error reading rule [%s]: %s" % (self.name, e))
if len(self.lines)>0:#Do the precheck only on the first line (Thanks Alex Lisle)
self.lines[0]["precheck"] = precheck
self.nlines = regexp.count(RuleMatch.NEWLINE) + 1
self.line_count = 1
self.matched = False
self.log = ""
self.groups = {}
# in order to eliminate unnecessary calls to the expensive re.findall(),
# perform assessments on the _replace_* functions of the Conf class to
# determine which are necessary.
self._replace_assessment = {}
for key, value in self.rule.iteritems():
if key != "regexp":
self._replace_assessment[key] = self.plugin.replace_value_assess(value)
def resetRule(self):
self.line_count = 1
self.log=''
self.matched = False
for line in self.lines:
line['result'] = None
def feed(self, line):
self.matched = False
self.groups = {}
line_index = self.line_count - 1
if len(self.lines) > line_index:
if line.find(self.lines[line_index]["precheck"]) != -1:
self.lines[line_index]["result"] = self.lines[line_index]["pattern"].search(line)
# (logs for multiline rules)
# Fill the log attribute with all its lines,
# not only with the last one matched
if line_index == 0:
self.log = ""
self.log += line.rstrip() + " "
if self.line_count == self.nlines:
if self.lines[line_index]["result"] is not None: # matched!
self.matched = True
self.line_count = 1
else:
#Line not matched, reset rule.
self.log=''
self.matched=False
self.line_count = 1
else:
if self.lines[line_index]["result"] is not None: # matched!
self.line_count += 1
else:
self.line_count = 1
else:
logger.error("There was an error loading rule [%s]" % (self.name))
def match(self):
if self.matched:
self.group()
return self.matched
# convert the list of pattern objects to a dictionary
def group(self):
self.groups = {}
count = 1
if self.matched:
for line in self.lines:
# group by index ()
groups = line["result"].groups()
for group in groups:
if group is None:
group = '' # convert to '' better than 'None'
value = ''
value = str(group.encode('utf-8'))
self.groups.update({str(count): value})
count += 1
# group by name (?P<name-of-group>)
groups = line["result"].groupdict()
for key, group in groups.iteritems():
if group is None:
group = '' # convert to '' better than 'None'
value = ''
value = str(group.encode('utf-8'))
self.groups.update({str(key): value})
def generate_event(self):
if not self.rule.has_key('event_type'):
logger.error("Event has no type, check plugin configuration!")
return None
if self.rule['event_type'] == Event.EVENT_TYPE:
event = Event()
elif self.rule['event_type'] == EventIdm.EVENT_TYPE:
event = EventIdm()
else:
logger.error("Bad event_type (%s) in rule (%s)" % \
(self.rule["event_type"], self.name))
return None
for key, value in self.rule.iteritems():
if key not in ["regexp", "precheck"]:
event[key] = self.plugin.get_replace_value(value.encode('utf-8'), self.groups, self._replace_assessment[key])
if self.log and not event['log'] and "log" in event.EVENT_ATTRS:
event['log'] = self.log.encode('utf-8')
return event
class FileEventHandler(pyinotify.ProcessEvent):
def __init__(self,ptrTail):
self.__ptrTail = ptrTail
def process_IN_CREATE(self, event):
"""
Version 0.8.x:
logger.info("File: %s has been created!" % event.pathname)
self.__ptrTail(event.pathname)
"""
pathname = os.path.join(event.path, event.name)
logger.info("File: %s has been created!" % pathname)
self.__ptrTail(pathname)
class ParserLog(Detector):
def __init__(self, conf, plugin, conn):
self._conf = conf # config.cfg info
self._plugin = plugin # plugins/X.cfg info
self.rules = [] # list of RuleMatch objects
self.conn = conn
Detector.__init__(self, conf, plugin, conn)
self.__tailLock = Lock()
self.stop_processing = False
self.__locations = []
self.__watchdog = pyinotify.WatchManager()
self.__notifier = None #pyinotify.ThreadedNotifier(self.__watchdog, FileEventHandler())
self.__startNotifier = False
self.__tails = []
self.__monitorLocations = []
self.__bookmark_dir = ""
def check_file_path(self, location):
can_read = True
if self._plugin.has_option("config", "create_file"):
create_file = self._plugin.getboolean("config", "create_file")
else:
create_file = False
if not os.path.exists(location) and create_file:
if not os.path.exists(os.path.dirname(location)):
self.logwarn(Lazyformat(
"Creating the {} directory...",
os.path.dirname(location)
))
os.makedirs(os.path.dirname(location), 0755)
self.logwarn(Lazyformat("The {} file is missing. Creating a new one...", location))
fd = open(location, 'w')
fd.close()
# open file
fd = None
try:
#check if file exist.
if os.path.isfile(location):
fd = open(location, 'r')
else:
self.logwarn(Lazyformat("File: {} does not exist!", location))
can_read = False
except IOError, e:
self.logerror(Lazyformat("Failed to read the file {}: {}", location, e))
can_read = False
#sys.exit()
if fd is not None:
fd.close()
return can_read
def stop(self):
self.logdebug("Scheduling plugin stop")
self.stop_processing = True
if self.__startNotifier:
self.__notifier.stop()
try:
self.join()
except RuntimeError:
self.logwarn("Stopping thread that likely hasn't started.")
def addTail(self, newlocation):
self.__tailLock.acquire()
for location in self.__monitorLocations:
if location == newlocation:
self.__tails.append(TailFollowBookmark(location, 1, self.__bookmark_dir, self._plugin.get("config", "encoding")))
self.__monitorLocations.remove(location)
break
self.__tailLock.release()
def resetAllrules(self):
for rule in self.rules:
rule.resetRule()
def process(self):
self.__notifier= pyinotify.ThreadedNotifier(self.__watchdog, FileEventHandler(self.addTail))
try:
self.__bookmark_dir = self._plugin.get("config", "bookmark_dir")
except:
self.__bookmark_dir = ""
mask = pyinotify.IN_CREATE
#check if the plugin has rlocation
locations = []
rlocationvalue = self._plugin.get("config","rlocation")
if rlocationvalue != "":
files = glob.glob(rlocationvalue )
for f in files:
self.logdebug(Lazyformat("Adding location: {}", f))
locations.append(f)
else:
locations = self._plugin.get("config", "location")
locations = locations.split(',')
self.__notifier.start()
# first check if file exists
for location in locations:
if self.check_file_path(location):
self.__locations.append(location)
else:
self.__monitorLocations.append(location)
dir = os.path.dirname(location)
# if not self.__watchdog.watches.has_key(dir): #--version python-pyinotify 0.8.9
self.__watchdog.add_watch(dir, mask, rec=True)
# compile the list of regexp
unsorted_rules = self._plugin.rules()
keys = unsorted_rules.keys()
keys.sort()
for key in keys:
item = unsorted_rules[key]
self.rules.append(RuleMatch(key, item, self._plugin))
# Move to the end of file
# fd.seek(0, 2)
for location in self.__locations:
self.__tails.append(TailFollowBookmark(location, 1, self.__bookmark_dir, self._plugin.get('config','encoding')))
while not self.stop_processing:
# is plugin enabled?
if not self._plugin.getboolean("config", "enable"):
# wait until plugin is enabled
while not self._plugin.getboolean("config", "enable"):
time.sleep(1)
# plugin is now enabled, skip events generated on
# 'disable' state, so move to the end of file
self._thresholding()
for tail in self.__tails:
# stop processing tails if requested
if self.stop_processing:
break
for line in tail:
matches = 0
rules = 0
# stop processing lines if requested
if self.stop_processing:
break
rule_matched = False
for rule in self.rules:
rules += 1
rule.feed(line)
if rule.match() and not rule_matched:
matches += 1
self.logdebug(Lazyformat("Match rule: [{}] -> {}", rule.name, line))
event = rule.generate_event()
self.resetAllrules()
# send the event as appropriate
if event is not None:
self.send_message(event)
# one rule matched, no need to check more
rule_matched = True
break
#Added small sleep to avoid the excessive cpu usage
time.sleep(0.01)
for tail in self.__tails:
tail.close()
self.logdebug("Processing completed.")
# vim:ts=4 sts=4 tw=79 expandtab: