Package Products :: Package ZenEvents :: Package browser :: Module EventConsole
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenEvents.browser.EventConsole

  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 urllib 
 15  from Products.Five.browser import BrowserView 
 16  from Products.ZenUtils.json import json 
 17  from Products.ZenUtils.Utils import unused, formreq 
 18   
 19   
20 -class EventConsole(BrowserView):
21 """ 22 A view for Devices, CustomEventViews and EventViews that provides JSON data 23 to populate the event console widget. 24 """
25 - def __call__(self, **kwargs):
26 return self._getEventsData(**kwargs)
27 28 @json 29 @formreq
30 - def _getEventsData(self, offset=0, count=50, getTotalCount=True, 31 startdate=None, enddate=None, filter='', severity=2, 32 state=1, orderby='', **kwargs):
33 """ 34 Data that populates the event console. 35 36 @return: A JSON representation of a tuple containing a list of lists of 37 event info, and the total number of matching events 38 @rtype: "([[a, b, c], [a, b, c]], 17)" 39 """ 40 unused(kwargs) 41 42 context = self.context 43 zem = self._getEventManager() 44 45 if hasattr(context, 'getResultFields'): 46 fields = context.getResultFields() 47 else: 48 # Use default result fields 49 if hasattr(context, 'event_key'): 50 base = context 51 else: 52 base = zem.dmd.Events 53 fields = zem.lookupManagedEntityResultFields(base.event_key) 54 55 data, totalCount = zem.getEventListME(context, 56 offset=offset, rows=count, resultFields=fields, 57 getTotalCount=getTotalCount, filter=filter, severity=severity, 58 state=state, orderby=orderby, startdate=startdate, enddate=enddate) 59 60 results = [self._extract_data_from_zevent(ev, fields) for ev in data] 61 return (results, totalCount)
62
63 - def _getEventManager(self):
64 return self.context.dmd.ZenEventManager
65
66 - def _extract_data_from_zevent(self, zevent, fields):
67 """return a list of data elements that map to the fields parameter. 68 """ 69 def _sanitize(val): 70 return val.replace('<', '&lt;').replace('>','&gt;')
71 72 from Products.ZenModel.Device import Device 73 contextIsDevice = isinstance( self.context, Device ) 74 #import pdb;pdb.set_trace() 75 data = [] 76 for field in fields: 77 value = getattr(zevent, field) 78 _shortvalue = str(value) or '' 79 if field == "device": 80 dev = self.context.dmd.searchDevices( value ) 81 if len(dev) == 1: 82 devUrl = '%s/viewEvents' % dev[0].getPrimaryUrlPath() 83 value = dev[0].urlLink(url=devUrl) 84 else: 85 value = urllib.quote('<a class="%s"' % ('') + 86 ' href="/zport/dmd/deviceSearchResults' 87 '?query=%s">%s</a>' % (value, _shortvalue)) 88 elif field == 'eventClass': 89 _shortvalue = _shortvalue.replace('/','/&shy;') 90 if not zevent.eventPermission: 91 value = _shortvalue 92 else: 93 value = urllib.quote('<a class="%s" ' % ('') + 94 'href="/zport/dmd/Events%s">%s</a>' % (value,_shortvalue)) 95 elif field == 'component' and ( getattr(zevent, 'device', None) or 96 contextIsDevice ): 97 #import pdb;pdb.set_trace() 98 component = getattr(zevent, 'component') 99 if contextIsDevice: 100 device = self.context.id 101 else: 102 device = getattr(zevent,'device') 103 104 comp = self.context.dmd.searchComponents( device, component ) 105 if len(comp) == 1: 106 compUrl = '%s/viewEvents' % comp[0].getPrimaryUrlPath() 107 value = comp[0].urlLink(url=compUrl) 108 else: 109 value = urllib.quote('<a class="%s"' % ('') + 110 ' href="/zport/dmd/searchComponents' 111 '?device=%s&component=%s">%s</a>' % ( 112 device, value, _shortvalue)) 113 elif field == 'summary': 114 value = urllib.quote( 115 value.replace('<','&lt;').replace('>','&gt;')) 116 elif field == 'prodState': 117 value = self.context.dmd.convertProdState(value) 118 else: 119 value = _shortvalue 120 data.append(value) 121 data.append(zevent.evid) 122 data.append(zevent.getCssClass()) 123 return data
124 125
126 -class EventConsoleFields(BrowserView):
127 """ 128 Get the fields for the event console. This is a separate call so that the 129 header DOM elements can be created first. 130 131 FIXME: Make the event console a single call. 132 """
133 - def __call__(self):
134 return self._getFields()
135 136 @json
137 - def _getFields(self):
138 """ 139 @return: A list of tuples representing fields and their relative 140 lengths 141 @rtype: [('field1', 10), ('field2', 4), ...] 142 """ 143 context = self.context 144 zem = self._getEventManager() 145 if hasattr(context, 'getResultFields'): 146 fields = context.getResultFields() 147 else: 148 if hasattr(context, 'event_key'): base = context 149 else: base = zem.dmd.Events 150 fields = zem.lookupManagedEntityResultFields(base.event_key) 151 lens = map(zem.getAvgFieldLength, fields) 152 total = sum(lens) 153 lens = map(lambda x:x/total*100, lens) 154 zipped = zip(fields, lens) 155 return zipped
156
157 - def _getEventManager(self):
158 return self.context.dmd.ZenEventManager
159 160
161 -class HistoryConsole(EventConsole):
162 """ 163 Same as the event console, only it accesses the history table. 164 """
165 - def _getEventManager(self):
166 return self.context.dmd.ZenEventHistory
167 168
169 -class HistoryConsoleFields(EventConsoleFields):
170 """ 171 Same as the event console fields, only for history. 172 173 FIXME: Is this used anywhere? 174 """
175 - def _getEventManager(self):
176 return self.context.dmd.ZenEventHistory
177