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 index, word in zip(self.positionIndex, words):
56 if '*' in index:
57 if not classids:
58 classids = copy.copy(index['*'])
59 else:
60 classids = classids.union(index['*'])
61 if word in index:
62 if not classids:
63 classids = copy.copy(index[word])
64 else:
65 classids = classids.intersection(index[word])
66 if len(classids) == 1:
67 classid = classids.pop()
68 break
69 elif len(classids) == 0:
70 raise EventClassNotFound, \
71 "no class found for words: " + " ".join(words)
72 if classid == -1:
73 raise EventClassNotUnique, \
74 "could not find unique classid possabilites are: ", classids
75
76
77 return classid
78
79
81 """build event classes based on log data"""
82 pass
83
84
86 """get a list of summary templates
87 and build our positionIndex from it"""
88 with self.indexlock:
89 templates = self.readTemplates()
90 for process, summary, classid in templates:
91 words = summary.split()
92 if process: words.insert(0, process)
93
94 self.positionIndex.extend(dict() for i in range(len(self.positionIndex), len(words)))
95 for posnIndex, word in zip(self.positionIndex, words):
96 if not word in posnIndex:
97 posnIndex[word] = set()
98 posnIndex[word].add(classid)
99
100
101
102
104 templates = []
105 if "http" in self.templatesource:
106
107 pass
108 elif path.exists(self.templatesource):
109 file = open(self.templatesource, "r")
110 for line in file.readlines():
111 if line.find("#") == 0: continue
112 process, summary, classid = line.split('||')
113 templates.append((process,summary,int(classid)))
114 file.close()
115 return templates
116