Package ZenEvents :: Module EventClassifier
[hide private]
[frames] | no frames]

Source Code for Module ZenEvents.EventClassifier

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 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   
33 -class EventClassNotFound(ZentinelException): pass
34
35 -class EventClassNotUnique(ZentinelException): pass
36
37 -class EventClassifier(object):
38
39 - def __init__(self, templatesource):
40 self.positionIndex = [] 41 self.templatesource = templatesource 42 self.indexlock = Lock()
43 #log = logging.getLogger("EventClassifier") 44 #log.setLevel(logging.DEBUG) 45 46
47 - def classify(self, event):
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 #logging.debug("found classid %s for words: %s" % 78 # (classid, " ".join(words))) 79 return classid
80 81
82 - def learn(self):
83 """build event classes based on log data""" 84 pass
85 86
87 - def buildIndex(self):
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 # need to do a second pass to clear out unneeded positions 105 # and to determin if a class can't be identified uniquely 106 self.indexlock.release()
107 108
109 - def readTemplates(self):
110 templates = [] 111 if "http" in self.templatesource: 112 # load over xmlrpc 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