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 as published by
      8  # 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 -    def getEventData(self):
     90          """return an list of event data"""
     91          return [ getattr(self, x) for x in self._fields]
     92
     93
     94 -    def updateFromFields(self, fields, data):
     95          """
     96          Update event from list of fields and list of data values.
     97          They must have the same length.  To be used when pulling data
     98          from the backend db.
     99          """
    100          self._fields = fields
    101          for i in range(len(fields)):
    102              if data[i] is None: data[i] = ''
    103              setattr(self, fields[i], data[i])
    104
    105
    106 -    def updateFromDict(self, data):
    107          """Update event from dict.  Keys that don't match attributes are
    108          put into the detail list of the event.
    109          """
    110          for key, value in data.items():
    111              setattr(self, key, value)
    112
    113
    114 -    def clearClasses(self):
    115          """Return a list of classes that this event clears.
    116          if we have specified clearClasses always return them
    117          if we ave a 0 severity return ourself as well.
    118          """
    119          clearcls = self._clearClasses
    120          evclass = getattr(self, "eventClass", None)
    121          sev = getattr(self, 'severity', None)
    122          if evclass and sev == 0: clearcls.append(self.eventClass)
    123          return clearcls
    124
    125
    126 -    def getDataList(self, fields):
    127          """return a list of data elements that map to the fields parameter.
    128          """
    129          return map(lambda x: getattr(self, x), fields)
    130
    131
    132 -    def getDedupFields(self, default):
    133          """Return list of dedupid fields.
    134          """
    135          return default
    136  pb.setUnjellyableForClass(Event, Event)
    137
    138
    139
    140 -class EventHeartbeat(Event):
    141
    142      eventClass = Heartbeat
    143
    144 -    def __init__(self, device, component, timeout=120):
    145          self._fields = ("device", "component", "timeout")
    146          Event.__init__(self, device=device, component=component,timeout=timeout)
    147  pb.setUnjellyableForClass(EventHeartbeat, EventHeartbeat)
    148