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

Source Code for Module Products.ZenEvents.Event

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