Package ZenEvents :: Module EventClassInst'
[hide private]
[frames] | no frames]

Source Code for Module ZenEvents.EventClassInst'

  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 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   
32 -def manage_addEventClassInst(context, id, REQUEST = None):
33 """make a device class""" 34 dc = EventClassInst(id) 35 context._setObject(id, dc) 36 if REQUEST is not None: 37 REQUEST['RESPONSE'].redirect(context.absolute_url() + '/manage_main')
38 39 40 addEventClassInst = DTMLFile('dtml/addEventClassInst',globals()) 41 42
43 -class EventClassPropertyMixin(object):
44 45 transform = '' 46 47 _properties = ( 48 {'id':'transform', 'type':'text', 'mode':'w'}, 49 ) 50
51 - def applyValues(self, evt):
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
69 - def applyTransform(self, evt, device):
70 if not self.transform: return evt 71 try: 72 variables = {'evt':evt, 'device':device} 73 exec(self.transform, variables) 74 except Exception, ex: 75 log.error("Error transforming EventClassInst %s (%s)", self.id, ex) 76 return variables['evt']
77 78
79 - def testTransformStyle(self):
80 """Test our transform by compiling it. 81 """ 82 try: 83 if self.transform: 84 compile(self.transform, "<string>", "exec") 85 except: 86 return "color:#FF0000;"
87 88 89 # Why is this a subclass of EventView? 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 # Screen action bindings (and tab definitions) 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
172 - def __init__(self, id):
173 ZenModelRM.__init__(self, id) 174 self.eventClassKey = id 175 self.sequence = None 176 self.rule = "" 177 self.regex = "" 178 self.example = "" 179 self.explanation = "" 180 self.resolution = ""
181 182
183 - def getStatus(self, **kwargs):
184 """Return the status number for this device of class statClass. 185 """ 186 statkey = self.getEventClass 187 return self.getEventManager().getStatusME(self, 188 statusclass=self.getEventClass(), 189 **kwargs)
190 191
192 - def getEventClass(self):
193 """Return the full EventClass of this EventClassInst.""" 194 return self.getOrganizerName()
195
196 - def getEventClassHref(self):
197 """Return href of our class. 198 """ 199 return self.eventClass().getPrimaryUrlPath()
200 201
202 - def getDmdKey(self):
203 """Return the dmd key of this mapping ie: /App/Start/zentinel 204 """ 205 return self.getOrganizerName() + "/" + self.id
206 207
208 - def applyExtraction(self, evt):
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
218 - def applyValues(self, evt):
219 """Modify event with values taken from dict Inst. 220 Any non-None property values are applied to the event. 221 """ 222 evt.eventClass = self.getEventClass() 223 evt.eventClassMapping = '%s/%s' % (self.getEventClass(), self.id) 224 return EventClassPropertyMixin.applyValues(self, evt)
225
226 - def ruleOrRegex(self, limit=None):
227 """Return the rule if it exists else return the regex. 228 limit limits the number of characters returned. 229 """ 230 value = self.rule and self.rule or self.regex 231 if not value and self.example: 232 value = self.example 233 if limit: value = value[:limit] 234 return value
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
257 - def testRegexStyle(self):
258 """Test our regex using the example event string. 259 """ 260 if self.example: 261 try: 262 value = re.search(self.regex, self.example, re.I) 263 if not value: return "color:#FF0000;" 264 except sre_constants.error: 265 return "color:#FF0000;"
266 267
268 - def testRuleStyle(self):
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
278 - def sameKey(self):
279 """Return a list of all mappings with the same eventClassKey. 280 """ 281 return self.eventClass().find(self.eventClassKey)
282 283
284 - def manage_afterAdd(self, item, container):
285 """ 286 Device only propagates afterAdd if it is the added object. 287 """ 288 self.index_object() 289 ZenModelRM.manage_afterAdd(self, item, container)
290 291
292 - def manage_afterClone(self, item):
293 """Not really sure when this is called.""" 294 ZenModelRM.manage_afterClone(self, item) 295 self.index_object()
296 297
298 - def manage_beforeDelete(self, item, container):
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')
308 - def manage_resequence(self, seqmap, REQUEST=None):
309 """Reorder the sequence of eventClassMappings with the same key. 310 """ 311 # first pass set new sequence 312 for i, map in enumerate(self.sameKey()): 313 map.sequence = seqmap[i] 314 # second pass take out any holes 315 for i, map in enumerate(self.sameKey()): 316 map.sequence = i 317 if REQUEST: 318 return self.callZenScreen(REQUEST)
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):
326 """Edit a EventClassInst from a web page. 327 """ 328 redirect = self.rename(name) 329 if self.eventClassKey != eventClassKey: 330 self.unindex_object() 331 self.sequence = self.eventClass().nextSequenceNumber(eventClassKey) 332 self.eventClassKey = eventClassKey 333 self.index_object() 334 self.regex = regex 335 self.rule = rule 336 self.example = example 337 self.transform = transform 338 self.explanation = explanation 339 self.resolution = resolution 340 if REQUEST: 341 from Products.ZenUtils.Time import SaveMessage 342 REQUEST['message'] = SaveMessage() 343 return self.callZenScreen(REQUEST, redirect)
344 345 346 InitializeClass(EventClassInst) 347