Package Products :: Package ZenWidgets :: Package browser :: Module Portlets
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenWidgets.browser.Portlets

  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 re 
 15  import json 
 16   
 17  from Products.Five.browser import BrowserView 
 18  from Products.AdvancedQuery import Eq, Or 
 19   
 20  from Products.ZenUtils.Utils import relative_time 
 21  from Products.ZenUtils.jsonutils import json 
 22  from Products.ZenUtils.Utils import nocache, formreq, extractPostContent 
 23  from Products.ZenWidgets import messaging 
 24  from Products.ZenModel.ZenossSecurity import * 
 25  from Products.ZenEvents.browser.EventPillsAndSummaries import \ 
 26                                     getDashboardObjectsEventSummary, \ 
 27                                     ObjectsEventSummary,    \ 
 28                                     getEventPillME 
29 30 31 -class TopLevelOrganizerPortletView(ObjectsEventSummary):
32 """ 33 Return JSON event summaries for a root organizer. 34 """ 35 @nocache 36 @formreq
37 - def __call__(self, dataRoot):
38 self.dataRoot = dataRoot 39 return super(TopLevelOrganizerPortletView, self).__call__()
40
41 - def _getObs(self):
42 return self.context.dmd.getDmdRoot(self.dataRoot).children()
43
44 45 -class ProductionStatePortletView(BrowserView):
46 """ 47 Return a map of device to production state in a format suitable for a 48 YUI data table. 49 """ 50 @nocache 51 @formreq
52 - def __call__(self, *args, **kwargs):
53 return self.getDevProdStateJSON(*args, **kwargs)
54 55 @json
56 - def getDevProdStateJSON(self, prodStates=['Maintenance']):
57 """ 58 Return a map of device to production state in a format suitable for a 59 YUI data table. 60 61 @return: A JSON representation of a dictionary describing devices 62 @rtype: "{ 63 'columns':['Device', 'Prod State'], 64 'data':[ 65 {'Device':'<a href=/>', 'Prod State':'Production'}, 66 {'Device':'<a href=/>', 'Prod State':'Maintenance'}, 67 ]}" 68 """ 69 devroot = self.context.dmd.Devices 70 if type(prodStates)==type(''): 71 prodStates = [prodStates] 72 orderby, orderdir = 'id', 'asc' 73 catalog = getattr(devroot, devroot.default_catalog) 74 queries = [] 75 for state in prodStates: 76 queries.append(Eq('getProdState', state)) 77 query = Or(*queries) 78 objects = catalog.evalAdvancedQuery(query, ((orderby, orderdir),)) 79 devs = (x.getObject() for x in objects) 80 mydict = {'columns':['Device', 'Prod State'], 'data':[]} 81 for dev in devs: 82 if not self.context.checkRemotePerm(ZEN_VIEW, dev): continue 83 mydict['data'].append({ 84 'Device' : dev.getPrettyLink(), 85 'Prod State' : dev.getProdState() 86 }) 87 mydict['data'] = mydict['data'][:100] 88 return mydict
89
90 91 -class WatchListPortletView(BrowserView):
92 """ 93 Accepts a list of paths to Zope objects which it then attempts to resolve. 94 If no list of paths is given, it will try to read them from the POST data 95 of the REQUEST object. 96 97 @param entities: A list of paths that should be resolved into objects 98 and passed to L{getDashboardObjectsEventSummaryJSON}. 99 @type entities: list 100 @return: A JSON-formatted string representation of the columns and rows 101 of the table 102 @rtype: string 103 """ 104 @nocache 105 @formreq
106 - def __call__(self, *args, **kwargs):
107 return self.getEntityListEventSummary(*args, **kwargs)
108 109 @json
110 - def getEntityListEventSummary(self, entities=None):
111 if entities is None: 112 entities = [] 113 elif isinstance(entities, basestring): 114 entities = [entities] 115 def getob(e): 116 e = str(e) 117 try: 118 if not e.startswith('/zport/dmd'): 119 bigdev = '/zport/dmd' + e 120 obj = self.context.dmd.unrestrictedTraverse(bigdev) 121 except (AttributeError, KeyError): 122 obj = self.context.dmd.Devices.findDevice(e) 123 if self.context.has_permission("View", obj): return obj
124 entities = filter(lambda x:x is not None, map(getob, entities)) 125 return getDashboardObjectsEventSummary( 126 self.context.dmd.ZenEventManager, entities)
127
128 129 -class DeviceIssuesPortletView(BrowserView):
130 """ 131 A list of devices with issues. 132 """ 133 @nocache
134 - def __call__(self):
135 return self.getDeviceIssuesJSON()
136 137 @json
138 - def getDeviceIssuesJSON(self):
139 """ 140 Get devices with issues in a form suitable for a portlet on the 141 dashboard. 142 143 @return: A JSON representation of a dictionary describing devices 144 @rtype: "{ 145 'columns':['Device', "Events'], 146 'data':[ 147 {'Device':'<a href=/>', 'Events':'<div/>'}, 148 {'Device':'<a href=/>', 'Events':'<div/>'}, 149 ]}" 150 """ 151 mydict = {'columns':[], 'data':[]} 152 mydict['columns'] = ['Device', 'Events'] 153 deviceinfo = self.getDeviceDashboard() 154 for alink, pill in deviceinfo: 155 mydict['data'].append({'Device':alink, 156 'Events':pill}) 157 return mydict
158
159 - def getDeviceDashboard(self):
160 """return device info for bad device to dashboard""" 161 zem = self.context.dmd.ZenEventManager 162 devices = [d[0] for d in zem.getDeviceIssues( 163 severity=4, state=1)] 164 devdata = [] 165 devclass = zem.getDmdRoot("Devices") 166 colors = "red orange yellow blue grey green".split() 167 for devname in devices: 168 dev = devclass.findDevice(devname) 169 if dev and ( dev.id == devname or dev.titleOrId() == devname ): 170 if (not zem.checkRemotePerm(ZEN_VIEW, dev) 171 or dev.productionState < zem.prodStateDashboardThresh 172 or dev.priority < zem.priorityDashboardThresh): 173 continue 174 alink = dev.getPrettyLink() 175 try: 176 pill = getEventPillME(zem, dev) 177 except IndexError: 178 continue 179 evts = [alink,pill] 180 devdata.append(evts) 181 return devdata[:100]
182
183 184 -class HeartbeatPortletView(BrowserView):
185 """ 186 Heartbeat issues in YUI table form, for the dashboard portlet 187 """ 188 @nocache
189 - def __call__(self):
190 return self.getHeartbeatIssuesJSON()
191 192 @json
193 - def getHeartbeatIssuesJSON(self):
194 """ 195 Get heartbeat issues in a form suitable for a portlet on the dashboard. 196 197 @return: A JSON representation of a dictionary describing heartbeats 198 @rtype: "{ 199 'columns':['Device', 'Daemon', 'Seconds'], 200 'data':[ 201 {'Device':'<a href=/>', 'Daemon':'zenhub', 'Seconds':10} 202 ]}" 203 """ 204 mydict = {'columns':[], 'data':[]} 205 mydict['columns'] = ['Device', 'Daemon', 'Seconds'] 206 heartbeats = self.context.dmd.ZenEventManager.getHeartbeat() 207 for Device, Daemon, Seconds, dummy in heartbeats: 208 mydict['data'].append({'Device':Device, 209 'Daemon':Daemon, 'Seconds':Seconds}) 210 return mydict
211
212 213 -class UserMessagesPortletView(BrowserView):
214 """ 215 User messages in YUI table form, for the dashboard portlet. 216 """ 217 @nocache 218 @json
219 - def __call__(self):
220 """ 221 Get heartbeat issues in a form suitable for a portlet on the dashboard. 222 223 @return: A JSON representation of a dictionary describing heartbeats 224 @rtype: "{ 225 'columns':['Device', 'Daemon', 'Seconds'], 226 'data':[ 227 {'Device':'<a href=/>', 'Daemon':'zenhub', 'Seconds':10} 228 ]}" 229 """ 230 ICONS = ['/zport/dmd/img/agt_action_success-32.png', 231 '/zport/dmd/img/messagebox_warning-32.png', 232 '/zport/dmd/img/agt_stop-32.png'] 233 msgbox = messaging.IUserMessages(self.context) 234 msgs = msgbox.get_messages() 235 cols = ['Message'] 236 res = [] 237 for msg in msgs: 238 res.append(dict( 239 title = msg.title, 240 imgpath = ICONS[msg.priority], 241 body = msg.body, 242 ago = relative_time(msg.timestamp), 243 deletelink = msg.absolute_url_path() + '/delMsg' 244 )) 245 res.reverse() 246 return { 'columns': cols, 'data': res }
247