1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""
15
16 Event classifier classifies events based on their summary text. It
17 uses a positional index to determin the class. So for each word in the
18 summary it looks for the same word or wild card to determine which classes
19 that word in that position could be part.
20
21 positionIndex is an array of dictionaries. Each positoin of the array
22 represents the position of the word in the summary string. The value
23 is a dictionary with the words as keys and a tuple of classids as values.
24 """
25
26 import copy
27 from os import path
28 from sets import Set
29 from threading import Lock
30
31 from Products.ZenUtils.Exceptions import ZentinelException
32
34
36
38
40 self.positionIndex = []
41 self.templatesource = templatesource
42 self.indexlock = Lock()
43
44
45
46
48 words = event.summary.split()
49 if hasattr(event, "process"):
50 words.insert(0,event.process)
51 if len(words) > len(self.positionIndex):
52 raise EventClassNotFound, "event summary longer than position index"
53 classids = None
54 classid = -1
55 for i in range(len(words)):
56 word = words[i]
57 index = self.positionIndex[i]
58 if index.has_key('*'):
59 if not classids:
60 classids = copy.copy(index['*'])
61 else:
62 classids = classids.union(index['*'])
63 if index.has_key(word):
64 if not classids:
65 classids = copy.copy(index[word])
66 else:
67 classids = classids.intersection(index[word])
68 if len(classids) == 1:
69 classid = classids.pop()
70 break
71 elif len(classids) == 0:
72 raise EventClassNotFound, \
73 "no class found for words: " + " ".join(words)
74 if classid == -1:
75 raise EventClassNotUnique, \
76 "could not find unique classid possabilites are: ", classids
77
78
79 return classid
80
81
83 """build event classes based on log data"""
84 pass
85
86
88 """get a list of summary templates
89 and build our positionIndex from it"""
90 self.indexlock.acquire()
91 templates = self.readTemplates()
92 for process, summary, classid in templates:
93 words = summary.split()
94 if process: words.insert(0, process)
95 lendiff = len(words) - len(self.positionIndex)
96 if lendiff > 0:
97 for i in range(lendiff):
98 self.positionIndex.append({})
99 for i in range(len(words)):
100 word = words[i]
101 if not self.positionIndex[i].has_key(word):
102 self.positionIndex[i][word] = Set()
103 self.positionIndex[i][word].add(classid)
104
105
106 self.indexlock.release()
107
108
110 templates = []
111 if "http" in self.templatesource:
112
113 pass
114 elif path.exists(self.templatesource):
115 file = open(self.templatesource, "r")
116 for line in file.readlines():
117 if line.find("#") == 0: continue
118 process, summary, classid = line.split('||')
119 templates.append((process,summary,int(classid)))
120 file.close()
121 return templates
122