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 import logging
31
32 from Products.ZenUtils.Exceptions import ZentinelException
33
35
37
39
41 self.positionIndex = []
42 self.templatesource = templatesource
43 self.indexlock = Lock()
44
45
46
47
49 words = event.summary.split()
50 if hasattr(event, "process"):
51 words.insert(0,event.process)
52 if len(words) > len(self.positionIndex):
53 raise EventClassNotFound, "event summary longer than position index"
54 classids = None
55 classid = -1
56 for i in range(len(words)):
57 word = words[i]
58 index = self.positionIndex[i]
59 if index.has_key('*'):
60 if not classids:
61 classids = copy.copy(index['*'])
62 else:
63 classids = classids.union(index['*'])
64 if index.has_key(word):
65 if not classids:
66 classids = copy.copy(index[word])
67 else:
68 classids = classids.intersection(index[word])
69 if len(classids) == 1:
70 classid = classids.pop()
71 break
72 elif len(classids) == 0:
73 raise EventClassNotFound, \
74 "no class found for words: " + " ".join(words)
75 if classid == -1:
76 raise EventClassNotUnique, \
77 "could not find unique classid possabilites are: ", classids
78
79
80 return classid
81
82
84 """build event classes based on log data"""
85 pass
86
87
89 """get a list of summary templates
90 and build our positionIndex from it"""
91 self.indexlock.acquire()
92 templates = self.readTemplates()
93 for process, summary, classid in templates:
94 words = summary.split()
95 if process: words.insert(0, process)
96 lendiff = len(words) - len(self.positionIndex)
97 if lendiff > 0:
98 for i in range(lendiff):
99 self.positionIndex.append({})
100 for i in range(len(words)):
101 word = words[i]
102 if not self.positionIndex[i].has_key(word):
103 self.positionIndex[i][word] = Set()
104 self.positionIndex[i][word].add(classid)
105
106
107 self.indexlock.release()
108
109
111 templates = []
112 if "http" in self.templatesource:
113
114 pass
115 elif path.exists(self.templatesource):
116 file = open(self.templatesource, "r")
117 for line in file.readlines():
118 if line.find("#") == 0: continue
119 process, summary, classid = line.split('||')
120 templates.append((process,summary,int(classid)))
121 file.close()
122 return templates
123