-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter.py
More file actions
342 lines (282 loc) · 8.51 KB
/
Copy pathfilter.py
File metadata and controls
342 lines (282 loc) · 8.51 KB
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
#*********************************************************************************************#
#*********************************************************************************************#
#******************witten by Neil Hao(nbaoping@cisco.com), 2013/05/19*************************#
#*********************************************************************************************#
#*********************************************************************************************#
#*********************************************************************************************#
import re
import traceback
import sys
import logging
from base import *
from expression import *
__nameTypeMap = {
'clientIp' : 'string',
'seIp' : 'string',
'bytesSent' : 'value',
'bitrate' : 'string',
'lookupTime' : 'value',
'servTime' : 'value',
'encType' : 'string',
'storageUrl' : 'string',
'sourceUrl' : 'string',
'remoteHost' : 'string',
'protocol' : 'string',
'sessionId' : 'string',
'bytesRecvd' : 'string',
'trackMethod' : 'string',
'method' : 'string',
'mimeType' : 'string',
'bytesSentAll' : 'string',
'queryString' : 'string',
'firstLine' : 'string',
'description' : 'string',
'status' : 'value',
'sessionStatus' : 'string',
'standardTime' : 'string',
'servSeconds' : 'value',
'urlAll' : 'string',
'url' : 'string',
'hostHeader' : 'string',
'abrType' : 'string',
'connStatus' : 'string',
'recvdTime' : 'string',
'originLine' : 'string'
}
def register_filter( name, ftype ):
__nameTypeMap[name] = ftype
def get_name_type( name ):
if name in __nameTypeMap:
return __nameTypeMap[name]
return None
def check_name( name ):
return name in __nameTypeMap
class Filter( Expression ):
def __init__( self, fmtName ):
self.fmtName = fmtName
self.isCommonFmt = is_common_fmt_name( fmtName )
def is_true( self, logInfo ):
value = logInfo.get_member( self.fmtName )
if not self.isCommonFmt:
return self.filter( value )
else:
#for common fmtName, should handle exception since we don't know the value type
try:
return self.filter( value )
except:
logging.debug( '\n'+traceback.format_exc() )
return False
def filter( self, value ):
raise_virtual( func_name() )
class SingleFilter( Filter ):
def __init__( self, fmtName, exp ):
super(SingleFilter, self).__init__( fmtName )
self.exp = exp
def filter( self, value ):
if self.exp is None:
return True
return self.exp.is_true( value )
class ValueFilter( Filter ):
def __init__( self, fmtName, low, high ):
super(ValueFilter, self).__init__( fmtName )
self.low = low
self.high = high
def filter( self, value ):
value = float( value )
if self.low == 'min':
return value <= self.high
elif self.high == 'max':
return value >= self.low
else:
return value >= self.low and value <= self.high
class LowValueExp( Expression ):
def __init__( self, lowValue ):
super(LowValueExp, self).__init__()
self.lowValue = lowValue
def is_true( self, value ):
if self.lowValue == 'min':
return True
value = float( value )
return self.lowValue <= value
class HighValueExp( Expression ):
def __init__( self, highValue ):
super(HighValueExp, self).__init__()
self.highValue = highValue
def is_true( self, value ):
if self.highValue == 'max':
return True
value = float( value )
return self.highValue >= value
class EqualExp( Expression ):
def __init__( self, value ):
super(EqualExp, self).__init__()
self.value = value
def is_true( self, value ):
value = str( value )
return self.value == value
class LengthExp( Expression ):
def __init__( self, low, high ):
super(LengthExp, self).__init__()
self.low = low
self.high = high
def is_true( self, string ):
value = len(string)
if self.low == 'min':
return value <= self.high
elif self.high == 'max':
return value >= self.low
else:
return value >= self.low and value <= self.high
class KeywordExp( Expression ):
def __init__( self, kword ):
self.kword = kword
def is_true( self, string ):
return string.find( self.kword ) >= 0
class PrefixExp( Expression ):
def __init__( self, prefix ):
self.prefix = prefix
def is_true( self, string ):
return string.startswith( self.prefix )
class SuffixExp( Expression ):
def __init__( self, suffix ):
self.suffix = suffix
def is_true( self, string ):
return string.endswith( self.suffix )
class MatchExp( Expression ):
def __init__( self, pattern ):
self.regular = re.compile( pattern )
def is_true( self, string ):
string = str(string)
ret = self.regular.match( string )
return ret is not None
class SearchExp( Expression ):
def __init__( self, pattern ):
self.regular = re.compile( pattern )
def is_true( self, string ):
string = str(string)
ret = self.regular.search( string )
return ret is not None
class StringFilter( Filter ):
def __init__( self, fmtName, exp ):
super(StringFilter, self).__init__( fmtName )
self.exp = exp
def filter( self, string ):
if self.exp is not None:
return self.exp.is_true( string )
return True
class BaseFilter( object ):
def __init__( self ):
self.exp = None
def parse_xml( self, node ):
expList = parse_exp_from_xml( node, BaseFilter.__parse_normal, self )
if expList is not None and len(expList) > 0:
if len(expList) == 1:
self.exp = expList[0]
else:
andExp = AndExp( expList )
self.exp = andExp
logging.debug( 'parsing filter xml '+str(self.exp) )
return self.exp is not None
def filter( self, logInfo ):
if self.exp is None:
return True
return self.exp.is_true( logInfo )
def __parse_normal( self, node ):
name = node.nodeName
if name == 'filter':
return self.__parse_filter( node )
elif name == 'id':
idstr = get_nodevalue( node )
self.idstr = idstr
return None
def __parse_filter( self, node ):
fmtNodeList = get_xmlnode( node, 'fmtName' )
if fmtNodeList is None or len(fmtNodeList) == 0:
logging.warn( 'no fmtName in filter' )
return None
fmtName = get_nodevalue( fmtNodeList[0] )
fmtName = std_fmt_name( fmtName )
#if not check_name(fmtName) and not is_common_fmt_name(fmtName):
#print 'invalid filter name:', fmtName
#return None
typeNodeList = get_xmlnode( node, 'type' )
if typeNodeList is not None and len(typeNodeList) > 0:
ftype = get_nodevalue( typeNodeList[0] )
else:
ftype = get_name_type( fmtName )
if ftype is None:
#if not is_common_fmt_name(fmtName):
#print 'invalid fmtName:', fmtName
#return None
ftype = 'string'
expList = list()
slist = parse_exp_from_xml( node, BaseFilter.__parse_filter_args, self )
exp = None
if slist is not None and len(slist) > 0:
if len(slist) == 1:
exp = slist[0]
else:
exp = AndExp( slist )
logging.debug( 'expression:'+str(exp) )
filter = SingleFilter( fmtName, exp )
expList.append( filter )
logging.info( 'parsed one filter'+str(filter) )
return expList
def __parse_low_high( self, node ):
lowNodeList = get_xmlnode( node, 'low' )
highNodeList = get_xmlnode( node, 'high' )
low = 'min'
high = 'max'
if lowNodeList is not None and len(lowNodeList) > 0:
lowNode = lowNodeList[0]
low = float( get_nodevalue(lowNode) )
if highNodeList is not None and len(highNodeList) > 0:
highNode = highNodeList[0]
high = float( get_nodevalue(highNode) )
return (low, high)
def __parse_filter_args( self, node ):
expList = list()
name = node.nodeName
if name == 'keyword':
value = get_nodevalue( node )
kwordExp = KeywordExp( value )
expList.append( kwordExp )
elif name == 'prefix':
value = get_nodevalue( node )
prefixExp = PrefixExp( value )
expList.append( prefixExp )
elif name == 'suffix':
value = get_nodevalue( node )
suffixExp = SuffixExp( value )
expList.append( suffixExp )
elif name == 'match':
value = get_nodevalue( node )
reExp = MatchExp( value )
expList.append( reExp )
elif name == 'search':
value = get_nodevalue( node )
reExp = SearchExp( value )
expList.append( reExp )
elif name == 'length':
(low, high) = self.__parse_low_high( node )
lenExp = LengthExp( low, high )
expList.append( lenExp )
elif name == 'equal':
value = get_nodevalue( node )
equalExp = EqualExp( value )
expList.append( equalExp )
elif name == 'low':
value = get_nodevalue( node )
if value != 'min':
value = float( value )
lowExp = LowValueExp( value )
expList.append( lowExp )
elif name == 'high':
value = get_nodevalue( node )
if value != 'max':
value = float( value )
highExp = HighValueExp( value )
expList.append( highExp )
else:
return None
return expList