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

# Source Code for Module Products.ZenEvents.EventClassifier

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      7  #
      8  ##############################################################################
      9
     10
     11  __doc__="""
     12
     13  Event classifier classifies events based on their summary text.  It
     14  uses a positional index to determin the class.  So for each word in the
     15  summary it looks for the same word or wild card to determine which classes
     16  that word in that position could be part.
     17
     18  positionIndex is an array of dictionaries.  Each positoin of the array
     19  represents the position of the word in the summary string.  The value
     20  is a dictionary with the words as keys and a tuple of classids as values.
     21  """
     22
     23  import copy
     24  from os import path
     25  from threading import Lock
     26
     27  from Products.ZenUtils.Exceptions import ZentinelException
     28
     29 -class EventClassNotFound(ZentinelException): pass
     30
     31 -class EventClassNotUnique(ZentinelException): pass
     32
     33 -class EventClassifier(object):
     34
     35 -    def __init__(self, templatesource):
     36          self.positionIndex = []
     37          self.templatesource = templatesource
     38          self.indexlock = Lock()
     39          #log = logging.getLogger("EventClassifier")
     40          #log.setLevel(logging.DEBUG)
     41
     42
     43 -    def classify(self, event):
     44          words = event.summary.split()
     45          if hasattr(event, "process"):
     46              words.insert(0,event.process)
     47          if len(words) > len(self.positionIndex):
     48              raise EventClassNotFound, "event summary longer than position index"
     49          classids = None
     50          classid = -1
     51          for index, word in zip(self.positionIndex, words):
     52              if '*' in index:
     53                  if not classids:
     54                      classids = copy.copy(index['*'])
     55                  else:
     56                      classids = classids.union(index['*'])
     57              if word in index:
     58                  if not classids:
     59                      classids = copy.copy(index[word])
     60                  else:
     61                      classids = classids.intersection(index[word])
     62              if len(classids) == 1:
     63                  classid = classids.pop()
     64                  break
     65              elif len(classids) == 0:
     66                  raise EventClassNotFound, \
     67                      "no class found for words: " + " ".join(words)
     68          if classid == -1:
     69              raise EventClassNotUnique, \
     70                  "could not find unique classid possabilites are: ", classids
     71          #logging.debug("found classid %s for words: %s" %
     72          #                (classid, " ".join(words)))
     73          return classid
     74
     75
     76 -    def learn(self):
     77          """build event classes based on log data"""
     78          pass
     79
     80
     81 -    def buildIndex(self):
     82          """get a list of summary templates
     83          and build our positionIndex from it"""
     84          with self.indexlock:
     85              templates = self.readTemplates()
     86              for process, summary, classid in templates:
     87                  words = summary.split()
     88                  if process: words.insert(0, process)
     89                  # add more position index entries if words list is longer than any seen before
     90                  self.positionIndex.extend(dict() for i in range(len(self.positionIndex), len(words)))
     91                  for posnIndex, word in zip(self.positionIndex, words):
     92                      if not word in posnIndex:
     93                          posnIndex[word] = set()
     94                      posnIndex[word].add(classid)
     95              # need to do a second pass to clear out unneeded positions
     96              # and to determin if a class can't be identified uniquely
     97
     98
     99 -    def readTemplates(self):
    100          templates = []
    101          if "http" in self.templatesource:
    102              # load over xmlrpc
    103              pass
    104          elif path.exists(self.templatesource):
    105              file = open(self.templatesource, "r")
    106              for line in file.readlines():
    107                  if line.find("#") == 0: continue
    108                  process, summary, classid = line.split('||')
    109                  templates.append((process,summary,int(classid)))
    110              file.close()
    111          return templates
    112