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

# Source Code for Module Products.ZenEvents.Event

      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  import time
     15
     16  from Products.ZenEvents.ZenEventClasses import *
     17  from Products.ZenEvents.Exceptions import *
     18
     19  from twisted.spread import pb
     20
     21 -def buildEventFromDict(evdict):
     22      """Build an event object from a dictionary.
     23      """
     24      evclass = evdict.get("eventClass", Unknown)
     25      if evclass == Heartbeat:
     26          for field in ("device", "component", "timeout"):
     27              if field not in evdict:
     28                  raise ZenEventError("Required event field %s not found: %s" % (field, evdict))
     29          evt = EventHeartbeat(evdict['device'], evdict['component'],
     30                               evdict['timeout'])
     31      else:
     32          evt = Event(**evdict)
     33      return evt
     34
     35
     36
     37 -class Event(pb.Copyable, pb.RemoteCopy):
     38      """
     39      Event that lives independant of zope context.  As interface that allows
     40      it to be persisted to/from the event backend.
     41      dedupid,
     42      evid,
     43      device,
     44      ipAddress,
     45      component,
     46      eventClass,
     47      eventGroup,
     48      eventKey,
     49      facility,
     50      severity,
     51      priority,
     52      summary,
     53      message,
     54      stateChange,
     55      firstTime,
     56      lastTime,
     57      count,
     58      prodState,
     59      DevicePriority,
     60      manager,
     61      agent,
     62      DeviceClass,
     63      Location,
     64      Systems,
     65      DeviceGroups,
     66      """
     67
     68 -    def __init__(self, rcvtime=None, **kwargs):
     69          # not sure we need sub second time stamps
     70          # if we do uncomment and change event backend to use
     71          # double presicion values for these two fields.
     72          if not rcvtime:
     73              self.firstTime = self.lastTime = time.time()
     74          else:
     75              self.firstTime = self.lastTime = rcvtime
     76          self._clearClasses = []
     77          self._action = "status"
     78          self._fields = kwargs.get('fields',[])
     79          self.eventKey = ''
     80          self.component = ''
     81          if kwargs: self.updateFromDict(kwargs)
     82
     83
     84 -    def getEventFields(self):
     85          """return an array of event fields tuples (field,value)"""
     86          return [(x, getattr(self, x)) for x in self._fields]
     87
     88
     89      # DEPRECATE THIS METHOD - not used anywhere
     90      #def getEventData(self):
     91      #    """return an list of event data"""
     92      #    return [ getattr(self, x) for x in self._fields]
     93
     94
     95 -    def updateFromFields(self, fields, data):
     96          """
     97          Update event from list of fields and list of data values.
     98          They must have the same length.  To be used when pulling data
     99          from the backend db.
    100          """
    101          self._fields = fields
    102          data = [d if d is not None else '' for d in data]
    103          for field,val in zip(fields, data):
    104              setattr(self, field, val)
    105
    106
    107 -    def updateFromDict(self, data):
    108          """Update event from dict.  Keys that don't match attributes are
    109          put into the detail list of the event.
    110          """
    111          for key, value in data.items():
    112              setattr(self, key, value)
    113
    114 -    def clone(self):
    115          ret = self.__class__(**self.__dict__)
    116          # make copies of lists, instead of just duplicating refs to them
    117          ret._fields = self._fields[:]
    118          ret._clearClasses = self._clearClasses[:]
    119          return ret
    120
    121 -    def clearClasses(self):
    122          """Return a list of classes that this event clears.
    123          if we have specified clearClasses always return them
    124          if we ave a 0 severity return ourself as well.
    125          """
    126          clearcls = self._clearClasses
    127          evclass = getattr(self, "eventClass", None)
    128          sev = getattr(self, 'severity', None)
    129          if evclass and sev == 0:
    130              clearcls.append(self.eventClass)
    131
    132          # collapse out duplicates
    133          clearcls = list(set(clearcls))
    134          return clearcls
    135
    136
    137      # DEPRECATE THIS METHOD - not used anywhere
    138      #def getDataList(self, fields):
    139      #    """return a list of data elements that map to the fields parameter.
    140      #    """
    141      #    return map(lambda x: getattr(self, x), fields)
    142
    143
    144 -    def getDedupFields(self, default):
    145          """Return list of dedupid fields.
    146          """
    147          return default
    148  pb.setUnjellyableForClass(Event, Event)
    149
    150
    151
    152 -class EventHeartbeat(Event):
    153
    154      eventClass = Heartbeat
    155
    156 -    def __init__(self, device, component, timeout=120):
    157          self._fields = ("device", "component", "timeout")
    158          Event.__init__(self, device=device, component=component,timeout=timeout)
    159  pb.setUnjellyableForClass(EventHeartbeat, EventHeartbeat)
    160