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

Source Code for Module 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 data = [] 73 for field in fields: 74 value = getattr(zevent, field) 75 _shortvalue = str(value) or '' 76 if field == "device": 77 value = urllib.quote('<a class="%s"' % ('') + 78 ' href="/zport/dmd/deviceSearchResults' 79 '?query=%s">%s</a>' % (value, _shortvalue)) 80 elif field == 'eventClass': 81 _shortvalue = _shortvalue.replace('/','/&shy;') 82 if not zevent.eventPermission: 83 value = _shortvalue 84 else: 85 value = urllib.quote('<a class="%s" ' % ('') + 86 'href="/zport/dmd/Events%s">%s</a>' % (value,_shortvalue)) 87 elif field == 'component' and getattr(zevent, 'device', None): 88 value = urllib.quote('<a class="%s"' % ('') + 89 ' href="/zport/dmd/searchComponents' 90 '?device=%s&component=%s">%s</a>' % ( 91 getattr(zevent, 'device'), value, _shortvalue)) 92 elif field == 'summary': 93 value = urllib.quote( 94 value.replace('<','&lt;').replace('>','&gt;')) 95 else: 96 value = _shortvalue 97 data.append(value) 98 data.append(zevent.evid) 99 data.append(zevent.getCssClass()) 100 return data
101 102
103 -class EventConsoleFields(BrowserView):
104 """ 105 Get the fields for the event console. This is a separate call so that the 106 header DOM elements can be created first. 107 108 FIXME: Make the event console a single call. 109 """
110 - def __call__(self):
111 return self._getFields()
112 113 @json
114 - def _getFields(self):
115 """ 116 @return: A list of tuples representing fields and their relative 117 lengths 118 @rtype: [('field1', 10), ('field2', 4), ...] 119 """ 120 context = self.context 121 zem = self._getEventManager() 122 if hasattr(context, 'getResultFields'): 123 fields = context.getResultFields() 124 else: 125 if hasattr(context, 'event_key'): base = context 126 else: base = zem.dmd.Events 127 fields = zem.lookupManagedEntityResultFields(base.event_key) 128 lens = map(zem.getAvgFieldLength, fields) 129 total = sum(lens) 130 lens = map(lambda x:x/total*100, lens) 131 zipped = zip(fields, lens) 132 return zipped
133
134 - def _getEventManager(self):
135 return self.context.dmd.ZenEventManager
136 137
138 -class HistoryConsole(EventConsole):
139 """ 140 Same as the event console, only it accesses the history table. 141 """
142 - def _getEventManager(self):
143 return self.context.dmd.ZenEventHistory
144 145
146 -class HistoryConsoleFields(EventConsoleFields):
147 """ 148 Same as the event console fields, only for history. 149 150 FIXME: Is this used anywhere? 151 """
152 - def _getEventManager(self):
153 return self.context.dmd.ZenEventHistory
154