1
2
3
4
5
6
7
8
9
10
11
12
13
14 import os
15 import copy
16 import re
17 import sre_constants
18 import logging
19 log = logging.getLogger("zen.Events")
20
21 from Globals import DTMLFile
22 from Globals import InitializeClass
23 from Acquisition import aq_base
24 from AccessControl import ClassSecurityInfo
25 from AccessControl import Permissions
26
27 from Products.ZenRelations.RelSchema import *
28 from Products.ZenModel.ZenModelRM import ZenModelRM
29 from Products.ZenModel.EventView import EventView
30 from Products.ZenModel.ZenPackable import ZenPackable
31
38
39
40 addEventClassInst = DTMLFile('dtml/addEventClassInst',globals())
41
42
44
45 transform = ''
46
47 _properties = (
48 {'id':'transform', 'type':'text', 'mode':'w'},
49 )
50
52 """Modify event with values taken from dict Inst.
53 Any non-None property values are applied to the event.
54 """
55 evt._clearClasses = copy.copy(getattr(self, "zEventClearClasses", []))
56 evt._action = getattr(self, "zEventAction", "status")
57 sev = getattr(self, "zEventSeverity", -1)
58 if sev >= 0:
59 if evt.severity > 0:
60 evt.severity = sev
61 updates = {}
62 for name in 'resolution', 'explanation':
63 value = getattr(self, name, None)
64 if value is not None and value != '':
65 updates[name] = value
66 evt.updateFromDict(updates)
67 return evt
68
77
78
87
88
89
90
91 -class EventClassInst(EventClassPropertyMixin, ZenModelRM, EventView,
92 ZenPackable):
93 """
94 EventClassInst.
95 """
96
97 event_key = meta_type = "EventClassInst"
98
99 default_catalog = "eventClassSearch"
100
101 actions = ("status", "history", "heartbeat", "drop")
102
103 _properties = EventClassPropertyMixin._properties + (
104 {'id':'eventClassKey', 'type':'string', 'mode':'w'},
105 {'id':'sequence', 'type':'int', 'mode':'w'},
106 {'id':'rule', 'type':'string', 'mode':'w'},
107 {'id':'regex', 'type':'string', 'mode':'w'},
108 {'id':'example', 'type':'string', 'mode':'w'},
109 {'id':'explanation', 'type':'text', 'mode':'w'},
110 {'id':'resolution', 'type':'text', 'mode':'w'},
111 )
112
113
114 _relations = ZenPackable._relations + (
115 ("eventClass", ToOne(ToManyCont,"Products.ZenEvents.EventClass","instances")),
116 )
117
118
119
120 factory_type_information = (
121 {
122 'id' : 'EventClassInst',
123 'meta_type' : 'EventClassInst',
124 'description' : """Base class for all devices""",
125 'icon' : 'EventClassInst.gif',
126 'product' : 'ZenEvents',
127 'factory' : 'manage_addEventClassInst',
128 'immediate_view' : 'eventClassInstStatus',
129 'actions' :
130 (
131 { 'id' : 'status'
132 , 'name' : 'Status'
133 , 'action' : 'eventClassInstStatus'
134 , 'permissions' : (Permissions.view, )
135 },
136 { 'id' : 'edit'
137 , 'name' : 'Edit'
138 , 'action' : 'eventClassInstEdit'
139 , 'permissions' : ("Manage DMD", )
140 },
141 { 'id' : 'sequence'
142 , 'name' : 'Sequence'
143 , 'action' : 'eventClassInstSequence'
144 , 'permissions' : (Permissions.view,)
145 },
146 { 'id' : 'config'
147 , 'name' : 'zProperties'
148 , 'action' : 'zPropertyEdit'
149 , 'permissions' : ("Manage DMD",)
150 },
151 { 'id' : 'events'
152 , 'name' : 'Events'
153 , 'action' : 'viewEvents'
154 , 'permissions' : (Permissions.view, )
155 },
156 { 'id' : 'historyEvents'
157 , 'name' : 'History'
158 , 'action' : 'viewHistoryEvents'
159 , 'permissions' : (Permissions.view, )
160 },
161 { 'id' : 'viewHistory'
162 , 'name' : 'Modifications'
163 , 'action' : 'viewHistory'
164 , 'permissions' : (Permissions.view, )
165 },
166 )
167 },
168 )
169
170 security = ClassSecurityInfo()
171
181
182
190
191
193 """Return the full EventClass of this EventClassInst."""
194 return self.getOrganizerName()
195
200
201
203 """Return the dmd key of this mapping ie: /App/Start/zentinel
204 """
205 return self.getOrganizerName() + "/" + self.id
206
207
209 """
210 Apply the event dict regex to extract additional values from the event.
211 """
212 if self.regex:
213 m = re.search(self.regex, evt.message)
214 if m: evt.updateFromDict(m.groupdict())
215 return evt
216
217
225
235
236
237 - def match(self, evt, device):
238 """Match an event message against our regex.
239 """
240 value = False
241 log.debug("match on:%s", self.getPrimaryDmdId())
242 if self.rule:
243 try:
244 log.debug("eval rule:%s", self.rule)
245 value = eval(self.rule, {'evt':evt, 'device': device})
246 except Exception, e:
247 logging.warn("EventClassInst: %s rule failure: %s",
248 self.getDmdKey(), e)
249 else:
250 try:
251 log.debug("regex='%s' message='%s'", self.regex, evt.message)
252 value = re.search(self.regex, evt.message, re.I)
253 except sre_constants.error: pass
254 return value
255
256
266
267
269 """Test our rule by compiling it.
270 """
271 try:
272 if self.rule:
273 compile(self.rule, "<string>", "eval")
274 except:
275 return "color:#FF0000;"
276
277
279 """Return a list of all mappings with the same eventClassKey.
280 """
281 return self.eventClass().find(self.eventClassKey)
282
283
290
291
296
297
299 """
300 Device only propagates beforeDelete if we are being deleted or copied.
301 Moving and renaming don't propagate.
302 """
303 ZenModelRM.manage_beforeDelete(self, item, container)
304 self.unindex_object()
305
306
307 security.declareProtected('Manage DMD', 'manage_resequence')
319
320
321 security.declareProtected('Manage DMD', 'manage_editEventClassInst')
322 - def manage_editEventClassInst(self, name="", eventClassKey='',
323 regex='', rule='', example='',
324 transform='',
325 explanation='', resolution='', REQUEST=None):
344
345
346 InitializeClass(EventClassInst)
347