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

Source Code for Module ZenEvents.EventClass

  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  __doc__="""EventClass.py 
 14   
 15  Event class objects 
 16  """ 
 17   
 18  import types 
 19  import logging 
 20  log = logging.getLogger("zen.Events") 
 21   
 22  import transaction 
 23  from Globals import InitializeClass 
 24  from Globals import DTMLFile 
 25  from AccessControl import ClassSecurityInfo 
 26  from AccessControl import Permissions 
 27  from Products.ZenModel.ManagedEntity import ManagedEntity 
 28  from Products.ZenModel.ZenossSecurity import * 
 29  from Acquisition import aq_base 
 30   
 31  from Products.ZenRelations.RelSchema import * 
 32  from EventClassInst import EventClassInst, EventClassPropertyMixin 
 33  from Products.ZenEvents.ZenEventClasses import Unknown 
 34   
 35  from Products.ZenModel.Organizer import Organizer 
 36  from Products.ZenModel.ZenPackable import ZenPackable 
 37   
 38  from Products.ZenUtils.Utils import prepId as globalPrepId 
 39   
 40  __pychecker__='no-argsused' 
 41   
42 -def manage_addEventClass(context, id="Events", REQUEST=None):
43 """make a event class""" 44 ed = EventClass(id) 45 context._setObject(id, ed) 46 if id == "Events": 47 ed = context._getOb(id) 48 ed.createCatalog() 49 ed.buildZProperties() 50 if REQUEST is not None: 51 REQUEST['RESPONSE'].redirect(context.absolute_url() + '/manage_main')
52 53 54 addEventClass = DTMLFile('dtml/addEventClass',globals()) 55
56 -class EventClass(EventClassPropertyMixin, Organizer, ManagedEntity, ZenPackable):
57 """ 58 EventClass organizer 59 """ 60 61 isInTree = True 62 63 transform = '' 64 65 meta_type = "EventClass" #FIXME - this is wrong just temp perserving data 66 event_key = "EventClass" 67 68 dmdRootName = "Events" 69 70 default_catalog = "eventClassSearch" 71 72 _relations = ZenPackable._relations + ( 73 ("instances", ToManyCont(ToOne,"Products.ZenEvents.EventClassInst","eventClass")), 74 ) 75 76 77 _properties = Organizer._properties + \ 78 EventClassPropertyMixin._properties + \ 79 ({'id':'transform', 'type':'text', 'mode':'w'},) 80 81 82 # Screen action bindings (and tab definitions) 83 factory_type_information = ( 84 { 85 'id' : 'EventClass', 86 'meta_type' : 'EventClass', 87 'description' : """Base class for all event classes""", 88 'icon' : 'EventClass.gif', 89 'product' : 'ZenEvents', 90 'factory' : 'manage_addEventClass', 91 'immediate_view' : 'eventClassStatus', 92 'actions' : 93 ( 94 { 'id' : 'classes' 95 , 'name' : 'Classes' 96 , 'action' : 'eventClassStatus' 97 , 'permissions' : ( 98 Permissions.view, ) 99 }, 100 { 'id' : 'eventList' 101 , 'name' : 'Mappings' 102 , 'action' : 'eventMappingList' 103 , 'permissions' : ( 104 Permissions.view, ) 105 }, 106 { 'id' : 'events' 107 , 'name' : 'Events' 108 , 'action' : 'viewEvents' 109 , 'permissions' : ( 110 Permissions.view, ) 111 }, 112 { 'id' : 'config' 113 , 'name' : 'zProperties' 114 , 'action' : 'zPropertyEdit' 115 , 'permissions' : ("Change Device",) 116 }, 117 ) 118 }, 119 ) 120 121 security = ClassSecurityInfo() 122 123 severityConversions = ( 124 ('Critical', 5), 125 ('Error', 4), 126 ('Warning', 3), 127 ('Info', 2), 128 ('Debug', 1), 129 ('Clear', 0), 130 ('Original', -1), 131 ) 132 severities = dict([(b, a) for a, b in severityConversions]) 133
134 - def getSubEventClasses(self):
135 """ 136 Return all EventClass objects below this one. 137 138 @return: list of event classes 139 @rtype: list of EventClass 140 """ 141 evts = self.children() 142 for subgroup in self.children(): 143 evts.extend(subgroup.getSubEventClasses()) 144 return evts
145 146 147 security.declareProtected(ZEN_COMMON, "getOrganizerNames")
148 - def getOrganizerNames(self, addblank=False, checkPerm=False):
149 """ 150 Returns a list of all organizer names under this organizer. Overridden 151 here so that restricted users can get a list of event classes. 152 153 @param addblank: If True, add a blank item in the list. 154 @type addblank: boolean 155 @return: The DMD paths of all Organizers below this instance. 156 @rtype: list 157 @permission: ZEN_COMMON 158 """ 159 return Organizer.getOrganizerNames( 160 self, addblank=addblank, checkPerm=checkPerm)
161 162
163 - def find(self, evClassKey):
164 """ 165 Look for the eventClassKey mapping in an event class, 166 and return them in sequence number oder, lowest-to-highest. 167 168 @parameter evClassKey: event class key 169 @type evClassKey: string 170 @return: list of event class mappings that match evClassKey, sorted 171 @rtype: list of EventClassInst 172 """ 173 cat = self._getCatalog() 174 matches = cat({'eventClassKey': evClassKey}) 175 insts = [ self.getObjByPath(b.getPrimaryId) for b in matches ] 176 insts.sort(lambda x,y: cmp(x.sequence, y.sequence)) 177 if evClassKey != "defaultmapping": 178 insts.extend(self.find("defaultmapping")) 179 return insts
180 181
182 - def lookup(self, evt, device):
183 """ 184 Given an event, return an event class organizer object 185 186 @parameter evt: an event 187 @type evt: dictionary 188 @parameter device: device object 189 @type device: DMD device 190 @return: an event class that matches the mapping 191 @rtype: EventClassInst 192 """ 193 evtcls = [] 194 if getattr(evt, "eventClass", False): 195 try: 196 return self.getDmdRoot("Events").getOrganizer(evt.eventClass) 197 except KeyError: 198 log.debug("Unable to find '%s' organizer" % evt.eventClass) 199 200 # Use defaultmapping if no eventClassKey is set, or if it blank. 201 eventClassKey = getattr(evt, 'eventClassKey', 'defaultmapping') \ 202 or 'defaultmapping' 203 204 log.debug("lookup eventClassKey:%s", eventClassKey) 205 evtcls = self.find(eventClassKey) 206 207 for evtcl in evtcls: 208 m = evtcl.match(evt, device) 209 if m: 210 log.debug("EventClass:%s matched", evtcl.getOrganizerName()) 211 break 212 else: 213 log.debug("No EventClass matched") 214 try: 215 return self.getDmdRoot("Events").getOrganizer(Unknown) 216 except KeyError: 217 evtcl = None 218 log.debug("Unable to find 'Unknown' organizer") 219 return evtcl
220 221
222 - def applyExtraction(self, evt):
223 """Don't have extraction on event class. 224 """ 225 return evt
226 227
228 - def getInstances(self):
229 """Return all EventClassInstances from this node down. 230 """ 231 insts = self.instances() 232 for subclass in self.children(): 233 insts.extend(subclass.getInstances()) 234 return insts
235 236
237 - def nextSequenceNumber(self, key):
238 """Get next sequence number for instance. 239 """ 240 idx = 0 241 insts = self.find(key) 242 if len(insts) > 0: 243 idx = insts[-1].sequence + 1 244 return idx
245
246 - def prepId(self, id, subchar='_'):
247 return globalPrepId(id, subchar)
248
249 - def createInstance(self, id=None, REQUEST=None):
250 """Add an EventClassInst to this EventClass. 251 """ 252 if id: 253 id = self.prepId(id) 254 c=0 255 while self.instances._getOb(id,False): 256 c+=1 257 id = "%s_%02d" % (id, c) 258 ecr = EventClassInst(id) 259 ecr.sequence = self.nextSequenceNumber(ecr.eventClassKey) 260 self.instances._setObject(id, ecr) 261 if REQUEST: return self() 262 return self.instances._getOb(id)
263 264
265 - def removeInstances(self, ids=None, REQUEST=None):
266 """Remove Instances from an EventClass. 267 """ 268 if not ids: return self() 269 if type(ids) == types.StringType: ids = (ids,) 270 for id in ids: 271 self.instances._delObject(id) 272 if REQUEST: return self()
273 274
275 - def moveInstances(self, moveTarget, ids=None, REQUEST=None):
276 """Move instances from this EventClass to moveTarget. 277 """ 278 if not moveTarget or not ids: return self() 279 if type(ids) == types.StringType: ids = (ids,) 280 target = self.getChildMoveTarget(moveTarget) 281 for id in ids: 282 rec = self.instances._getOb(id, None) 283 if rec is None: continue 284 rec._operation = 1 # moving object state 285 self.instances._delObject(id) 286 target.instances._setObject(id, rec) 287 if REQUEST: 288 REQUEST['RESPONSE'].redirect(target.getPrimaryUrlPath())
289 290
291 - def countInstances(self):
292 """count all instances with in an event dict""" 293 count = self.instances.countObjects() 294 for group in self.children(): 295 count += group.countInstances() 296 return count
297 298
299 - def buildZProperties(self):
300 edict = self.getDmdRoot("Events") 301 if getattr(aq_base(edict), "zEventAction", False): return 302 edict._setProperty("zEventClearClasses", [], type="lines") 303 edict._setProperty("zEventAction", "status") 304 edict._setProperty("zEventSeverity", -1, type="int")
305
306 - def testTransformStyle(self):
307 """Test our transform by compiling it. 308 """ 309 try: 310 if self.transform: 311 compile(self.transform, "<string>", "exec") 312 except: 313 return "color:#FF0000;"
314
315 - def manage_editEventClassTransform(self, transform = '', REQUEST=None):
316 "Save the transform" 317 self.transform = transform 318 if REQUEST: return self.callZenScreen(REQUEST)
319 320
321 - def getEventSeverities(self):
322 """Return a list of tuples of severities [('Warning', 3), ...] 323 """ 324 return self.severityConversions
325
326 - def getEventSeverityString(self, severity):
327 """Return a list of tuples of severities [('Warning', 3), ...] 328 """ 329 try: 330 return self.severities[severity] 331 except IndexError: 332 return "Unknown"
333
334 - def reIndex(self):
335 """Go through all ips in this tree and reindex them.""" 336 log.debug("reindexing EventClass:%s", self.getOrganizerName()) 337 zcat = self._getCatalog() 338 zcat.manage_catalogClear() 339 transaction.savepoint() 340 for evtclass in self.getSubEventClasses(): 341 for ip in evtclass.instances(): 342 ip.index_object() 343 transaction.savepoint()
344 345
346 - def createCatalog(self):
347 """Create a catalog for EventClassRecord searching""" 348 from Products.ZCatalog.ZCatalog import manage_addZCatalog 349 manage_addZCatalog(self, self.default_catalog, 350 self.default_catalog) 351 zcat = self._getOb(self.default_catalog) 352 zcat.addIndex('eventClassKey', 'FieldIndex') 353 zcat.addColumn('getPrimaryId')
354 355 356 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'getOverriddenObjects')
357 - def getOverriddenObjects(self, propname, showDevices=False):
358 """ 359 Get the objects that override a property somewhere below in the tree 360 This method overrides ZenPropertyManager 361 """ 362 objects = [] 363 for inst in self.getSubInstances('instances'): 364 if inst.isLocal(propname) and inst not in objects: 365 objects.append(inst) 366 for suborg in self.children(): 367 if suborg.isLocal(propname): 368 objects.append(suborg) 369 for inst in suborg.getOverriddenObjects(propname): 370 if inst not in objects: 371 objects.append(inst) 372 return objects
373 374 security.declareProtected(ZEN_VIEW, 'getIconPath')
375 - def getIconPath(self):
376 """ Override the zProperty icon path and return a folder 377 """ 378 return "/zport/dmd/img/icons/folder.png"
379 380 security.declareProtected(ZEN_VIEW, 'getPrettyLink')
396 397 InitializeClass(EventClass) 398