1
2
3
4
5
6
7
8
9
10
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
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"
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
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
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")
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 if getattr(evt, "eventClassKey", False):
201 log.debug("lookup eventClassKey:%s", evt.eventClassKey)
202 evtcls = self.find(evt.eventClassKey)
203
204 for evtcl in evtcls:
205 m = evtcl.match(evt, device)
206 if m:
207 log.debug("EventClass:%s matched", evtcl.getOrganizerName())
208 break
209 else:
210 log.debug("No EventClass matched")
211 try:
212 return self.getDmdRoot("Events").getOrganizer(Unknown)
213 except KeyError:
214 evtcl = None
215 log.debug("Unable to find 'Unknown' organizer")
216 return evtcl
217
218
220 """Don't have extraction on event class.
221 """
222 return evt
223
224
226 """Return all EventClassInstances from this node down.
227 """
228 insts = self.instances()
229 for subclass in self.children():
230 insts.extend(subclass.getInstances())
231 return insts
232
233
235 """Get next sequence number for instance.
236 """
237 idx = 0
238 insts = self.find(key)
239 if len(insts) > 0:
240 idx = insts[-1].sequence + 1
241 return idx
242
243 - def prepId(self, id, subchar='_'):
244 return globalPrepId(id, subchar)
245
260
261
263 """Remove Instances from an EventClass.
264 """
265 if not ids: return self()
266 if type(ids) == types.StringType: ids = (ids,)
267 for id in ids:
268 self.instances._delObject(id)
269 if REQUEST: return self()
270
271
286
287
294
295
302
311
316
317
319 """Return a list of tuples of severities [('Warning', 3), ...]
320 """
321 return self.severityConversions
322
324 """Return a list of tuples of severities [('Warning', 3), ...]
325 """
326 try:
327 return self.severities[severity]
328 except IndexError:
329 return "Unknown"
330
332 """Go through all ips in this tree and reindex them."""
333 log.debug("reindexing EventClass:%s", self.getOrganizerName())
334 zcat = self._getCatalog()
335 zcat.manage_catalogClear()
336 transaction.savepoint()
337 for evtclass in self.getSubEventClasses():
338 for ip in evtclass.instances():
339 ip.index_object()
340 transaction.savepoint()
341
342
344 """Create a catalog for EventClassRecord searching"""
345 from Products.ZCatalog.ZCatalog import manage_addZCatalog
346 manage_addZCatalog(self, self.default_catalog,
347 self.default_catalog)
348 zcat = self._getOb(self.default_catalog)
349 zcat.addIndex('eventClassKey', 'FieldIndex')
350 zcat.addColumn('getPrimaryId')
351
352
353 security.declareProtected(ZEN_ZPROPERTIES_VIEW, 'getOverriddenObjects')
370
371 security.declareProtected(ZEN_VIEW, 'getIconPath')
373 """ Override the zProperty icon path and return a folder
374 """
375 return "/zport/dmd/img/icons/folder.png"
376
377 security.declareProtected(ZEN_VIEW, 'getPrettyLink')
379 """ Gets a link to this object, plus an icon """
380 href = self.getPrimaryUrlPath().replace('%','%%')
381 linktemplate = "<a href='"+href+"' class='prettylink'>%s</a>"
382 icon = ("<div class='device-icon-container'> "
383 "<img class='device-icon' src='%s'/> "
384 "</div>") % self.getIconPath()
385 name = self.getPrimaryDmdId()
386 if noicon: icon=''
387 if shortDesc: name = self.id
388 rendered = icon + name
389 if not self.checkRemotePerm("View", self):
390 return rendered
391 else:
392 return linktemplate % rendered
393
394 InitializeClass(EventClass)
395