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

Source Code for Module 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  import socket 
 16   
 17  from ZenEventClasses import * 
 18  from Exceptions import * 
 19   
 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" % field) 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(object):
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 stateChange, 54 firstTime, 55 lastTime, 56 count, 57 prodState, 58 manager, 59 agent, 60 DeviceClass, 61 Location, 62 Systems, 63 DeviceGroups, 64 """ 65
66 - def __init__(self, rcvtime=None, **kwargs):
67 # not sure we need sub second time stamps 68 # if we do uncomment and change event backend to use 69 # double presicion values for these two fields. 70 if not rcvtime: 71 self.firstTime = self.lastTime = time.time() 72 else: 73 self.firstTime = self.lastTime = rcvtime 74 self._clearClasses = [] 75 self._action = "status" 76 self._fields = kwargs.get('fields',[]) 77 self.eventKey = '' 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 - def getEventData(self):
87 """return an list of event data""" 88 return [ getattr(self, x) for x in self._fields]
89 90
91 - def updateFromFields(self, fields, data):
92 """ 93 Update event from list of fields and list of data values. 94 They must have the same length. To be used when pulling data 95 from the backend db. 96 """ 97 self._fields = fields 98 for i in range(len(fields)): 99 setattr(self, fields[i], data[i])
100 101
102 - def updateFromDict(self, data):
103 """Update event from dict. Keys that don't match attributes are 104 put into the detail list of the event. 105 """ 106 for key, value in data.items(): 107 setattr(self, key, value)
108 109
110 - def clearClasses(self):
111 """Return a list of classes that this event clears. 112 if we have specified clearClasses always return them 113 if we ave a 0 severity return ourself as well. 114 """ 115 clearcls = self._clearClasses 116 evclass = getattr(self, "eventClass", None) 117 sev = getattr(self, 'severity', None) 118 if evclass and sev == 0: clearcls.append(self.eventClass) 119 return clearcls
120 121
122 - def getDataList(self, fields):
123 """return a list of data elements that map to the fields parameter. 124 """ 125 return map(lambda x: getattr(self, x), fields)
126 127
128 - def getDedupFields(self, default):
129 """Return list of dedupid fields. 130 """ 131 return default
132 133
134 -class EventHeartbeat(Event):
135 136 eventClass = Heartbeat 137
138 - def __init__(self, device, component, timeout=120):
139 self._fields = ("device", "component", "timeout") 140 super(EventHeartbeat, self).__init__( 141 device=device, component=component,timeout=timeout)
142