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  import logging 
 31   
 32  from Products.ZenUtils.Exceptions import ZentinelException 
 33   
34 -class EventClassNotFound(ZentinelException): pass
35
36 -class EventClassNotUnique(ZentinelException): pass
37
38 -class EventClassifier(object):
39
40 - def __init__(self, templatesource):
41 self.positionIndex = [] 42 self.templatesource = templatesource 43 self.indexlock = Lock()
44 #log = logging.getLogger("EventClassifier") 45 #log.setLevel(logging.DEBUG) 46 47
48 - def classify(self, event):
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 #logging.debug("found classid %s for words: %s" % 79 # (classid, " ".join(words))) 80 return classid
81 82
83 - def learn(self):
84 """build event classes based on log data""" 85 pass
86 87
88 - def buildIndex(self):
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 # need to do a second pass to clear out unneeded positions 106 # and to determin if a class can't be identified uniquely 107 self.indexlock.release()
108 109
110 - def readTemplates(self):
111 templates = [] 112 if "http" in self.templatesource: 113 # load over xmlrpc 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