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

# Source Code for Module Products.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 or (at your
      8  # option) any later version as published by 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 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          #logging.debug("found classid %s for words: %s" %
     76          #                (classid, " ".join(words)))
     77          return classid
     78
     79
     80 -    def learn(self):
     81          """build event classes based on log data"""
     82          pass
     83
     84
     85 -    def buildIndex(self):
     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                  # add more position index entries if words list is longer than any seen before
     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              # need to do a second pass to clear out unneeded positions
    100              # and to determin if a class can't be identified uniquely
    101
    102
    103 -    def readTemplates(self):
    104          templates = []
    105          if "http" in self.templatesource:
    106              # load over xmlrpc
    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