1
2
3
4
5
6
7
8
9
10
11 __doc__="""EventClass.py
12
13 Event class objects
14 """
15
16 import logging
17 log = logging.getLogger("zen.Events")
18
19 import transaction
20 from zope.interface import implements
21 from Globals import InitializeClass
22 from AccessControl import ClassSecurityInfo
23 from AccessControl import Permissions
24 from Products.ZenModel.ManagedEntity import ManagedEntity
25 from Products.ZenModel.ZenossSecurity import *
26 from Acquisition import aq_base
27
28 from Products.ZenRelations.RelSchema import *
29 from EventClassInst import EventClassInst, EventClassPropertyMixin
30 from Products.ZenEvents.ZenEventClasses import Unknown
31
32 from Products.ZenModel.Organizer import Organizer
33 from Products.ZenModel.ZenPackable import ZenPackable
34 from Products.ZenUtils.guid.interfaces import IGloballyIdentifiable
35 from Products.ZenUtils.Utils import prepId as globalPrepId, getDisplayName
36 from Products.ZenMessaging.audit import audit
37
38 __pychecker__='no-argsused'
39
50
51 -class EventClass(EventClassPropertyMixin, Organizer, ManagedEntity, ZenPackable):
52 """
53 EventClass organizer
54 """
55
56 implements(IGloballyIdentifiable)
57
58 isInTree = True
59
60 transform = ''
61
62 meta_type = "EventClass"
63 event_key = "EventClass"
64
65 dmdRootName = "Events"
66
67 default_catalog = "eventClassSearch"
68
69 _relations = ZenPackable._relations + (
70 ("instances", ToManyCont(ToOne,"Products.ZenEvents.EventClassInst","eventClass")),
71 )
72
73
74 _properties = Organizer._properties + \
75 EventClassPropertyMixin._properties + \
76 ({'id':'transform', 'type':'text', 'mode':'w'},)
77
78
79
80 factory_type_information = (
81 {
82 'id' : 'EventClass',
83 'meta_type' : 'EventClass',
84 'description' : """Base class for all event classes""",
85 'icon' : 'EventClass.gif',
86 'product' : 'ZenEvents',
87 'factory' : 'manage_addEventClass',
88 'immediate_view' : 'eventClassStatus',
89 'actions' :
90 (
91 { 'id' : 'classes'
92 , 'name' : 'Classes'
93 , 'action' : 'eventClassStatus'
94 , 'permissions' : (
95 Permissions.view, )
96 },
97 { 'id' : 'eventList'
98 , 'name' : 'Mappings'
99 , 'action' : 'eventMappingList'
100 , 'permissions' : (
101 Permissions.view, )
102 },
103 { 'id' : 'events'
104 , 'name' : 'Events'
105 , 'action' : 'viewEvents'
106 , 'permissions' : (
107 Permissions.view, )
108 },
109 { 'id' : 'config'
110 , 'name' : 'Configuration Properties'
111 , 'action' : 'zPropertyEditNew'
112 , 'permissions' : ("Change Device",)
113 },
114 )
115 },
116 )
117
118 security = ClassSecurityInfo()
119
120 severityConversions = (
121 ('Critical', 5),
122 ('Error', 4),
123 ('Warning', 3),
124 ('Info', 2),
125 ('Debug', 1),
126 ('Clear', 0),
127 ('Original', -1),
128 )
129 severities = dict((b, a) for a, b in severityConversions)
130
132 """
133 Return all EventClass objects below this one.
134
135 @return: list of event classes
136 @rtype: list of EventClass
137 """
138 evts = self.children()
139 for subgroup in self.children():
140 evts.extend(subgroup.getSubEventClasses())
141 return evts
142
143
144 security.declareProtected(ZEN_COMMON, "getOrganizerNames")
146 """
147 Returns a list of all organizer names under this organizer. Overridden
148 here so that restricted users can get a list of event classes.
149
150 @param addblank: If True, add a blank item in the list.
151 @type addblank: boolean
152 @return: The DMD paths of all Organizers below this instance.
153 @rtype: list
154 @permission: ZEN_COMMON
155 """
156 return Organizer.getOrganizerNames(
157 self, addblank=addblank, checkPerm=checkPerm)
158
159
160 - def find(self, evClassKey):
161 """
162 Look for the eventClassKey mapping in an event class,
163 and return them in sequence number oder, lowest-to-highest.
164
165 @parameter evClassKey: event class key
166 @type evClassKey: string
167 @return: list of event class mappings that match evClassKey, sorted
168 @rtype: list of EventClassInst
169 """
170 cat = self._getCatalog()
171 matches = cat({'eventClassKey': evClassKey})
172 insts = sorted((self.getObjByPath(b.getPrimaryId) for b in matches),
173 key=lambda x: x.sequence)
174 if evClassKey != "defaultmapping":
175 insts.extend(self.find("defaultmapping"))
176 return insts
177
178
179 - def lookup(self, evt, device):
180 """
181 Given an event, return an event class organizer object
182
183 @parameter evt: an event
184 @type evt: dictionary
185 @parameter device: device object
186 @type device: DMD device
187 @return: an event class that matches the mapping
188 @rtype: EventClassInst
189 """
190 evtcls = []
191 if getattr(evt, "eventClass", False):
192 try:
193 log.debug("Looking for event class named in event: %s",
194 evt.eventClass)
195 path = evt.eventClass
196 if path.startswith("/"): path = path[1:]
197 return self.getDmdRoot('Events').findChild(path)
198 except (AttributeError, KeyError):
199 log.debug("Unable to find '%s' organizer" % evt.eventClass)
200
201
202 eventClassKey = getattr(evt, 'eventClassKey', 'defaultmapping') \
203 or 'defaultmapping'
204
205 log.debug("No event class specified, searching for eventClassKey %s",
206 eventClassKey)
207 evtcls = self.find(eventClassKey)
208 log.debug("Found the following event classes that matched key %s: %s",
209 eventClassKey, evtcls)
210
211 for evtcl in evtcls:
212 m = evtcl.match(evt, device)
213 if m:
214 log.debug("EventClass %s matched", evtcl.getOrganizerName())
215 break
216 else:
217 log.debug("No EventClass matched -- using /Unknown")
218 try:
219 return self.getDmdRoot("Events").getOrganizer(Unknown)
220 except KeyError:
221 evtcl = None
222 log.debug("Unable to find 'Unknown' organizer")
223 return evtcl
224
225
227 """Don't have extraction on event class.
228 """
229 return evt
230
231
233 """Return all EventClassInstances from this node down.
234 """
235 insts = self.instances()
236 for subclass in self.children():
237 insts.extend(subclass.getInstances())
238 return insts
239
240
242 """Get next sequence number for instance.
243 """
244 idx = 0
245 insts = self.find(key)
246 if len(insts) > 0:
247 idx = insts[-1].sequence + 1
248 return idx
249
250 - def prepId(self, id, subchar='_'):
251 return globalPrepId(id, subchar)
252
269
270
272 """Remove Instances from an EventClass.
273 """
274 if not ids: return self()
275 if isinstance(ids, basestring): ids = (ids,)
276 for id in ids:
277 self.instances._delObject(id)
278 if REQUEST:
279 audit('UI.EventClassMapping.Delete', id)
280 if REQUEST: return self()
281
282
299
300
307
308
315
324
325 security.declareProtected(ZEN_MANAGE_EVENTS, 'manage_editEventClassTransform')
333
335 """Return a list of tuples of severities [('Warning', 3), ...]
336 """
337 return self.severityConversions
338
340 """Return a list of tuples of severities [('Warning', 3), ...]
341 """
342 try:
343 return self.severities[severity]
344 except IndexError:
345 return "Unknown"
346
355
356
358 """Create a catalog for EventClassRecord searching"""
359 from Products.ZCatalog.ZCatalog import manage_addZCatalog
360 manage_addZCatalog(self, self.default_catalog,
361 self.default_catalog)
362 zcat = self._getOb(self.default_catalog)
363 zcat.addIndex('eventClassKey', 'FieldIndex')
364 zcat.addColumn('getPrimaryId')
365
366
367 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'getOverriddenObjects')
384
385 security.declareProtected(ZEN_VIEW, 'getIconPath')
387 """ Override the zProperty icon path and return a folder
388 """
389 return "/zport/dmd/img/icons/folder.png"
390
391 security.declareProtected(ZEN_VIEW, 'getPrettyLink')
393 """ Gets a link to this object, plus an icon """
394 href = self.getPrimaryUrlPath().replace('%','%%')
395 linktemplate = "<a href='"+href+"' class='prettylink'>%s</a>"
396 icon = ("<div class='device-icon-container'> "
397 "<img class='device-icon' src='%s'/> "
398 "</div>") % self.getIconPath()
399 name = self.getPrimaryDmdId()
400 if noicon: icon=''
401 if shortDesc: name = self.id
402 rendered = icon + name
403 if not self.checkRemotePerm("View", self):
404 return rendered
405 else:
406 return linktemplate % rendered
407
408 InitializeClass(EventClass)
409