Package ZenModel :: Package browser :: Module DeviceList
[hide private]
[frames] | no frames]

Source Code for Module ZenModel.browser.DeviceList

  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  from Products.Five.browser import BrowserView 
 15  from Products.ZenModel.DeviceOrganizer import DeviceOrganizer 
 16  from Products.ZenUtils.json import json 
 17  from Products.ZenUtils.Utils import formreq, unused, ipsort 
 18  from Products.AdvancedQuery import MatchRegexp, Or, Eq, In 
 19  from Products.ZenUtils.FakeRequest import FakeRequest 
 20  from Products.ZenWidgets import messaging 
 21  from Products.ZenWidgets.interfaces import IMessageSender 
 22   
23 -class AdvancedQueryDeviceList(object):
24 """ 25 Adapter providing list of devices according to various criteria. 26 """
27 - def __init__(self, context):
28 self.context = context
29
30 - def __call__(self, *args, **kwargs):
31 """ 32 Needs to be definition rather than simple reference due to possibility 33 of monkeypatching the hook. 34 """ 35 return self._getAdvancedQueryDeviceList(*args, **kwargs)
36
37 - def _getAdvancedQueryDeviceList(self, offset=0, count=50, filter='', 38 orderby='id', orderdir='asc'):
39 """ 40 Ask the catalog for devices matching the criteria specified. 41 """ 42 context = self.context 43 if not isinstance(context, DeviceOrganizer): 44 context = self.context.dmd.Devices 45 catalog = getattr(context, context.default_catalog) 46 filter = '(?is).*%s.*' % filter 47 filterquery = Or( 48 MatchRegexp('id', filter), 49 MatchRegexp('getDeviceIp', filter), 50 MatchRegexp('getProdState', filter), 51 MatchRegexp('getDeviceClassPath', filter) 52 ) 53 query = Eq('getPhysicalPath', context.absolute_url_path() 54 ) & filterquery 55 objects = catalog.evalAdvancedQuery(query, ((orderby, orderdir),)) 56 objects = list(objects) 57 totalCount = len(objects) 58 offset, count = int(offset), int(count) 59 obs = objects[offset:offset+count] 60 return totalCount, obs
61 62
63 -class DeviceList(BrowserView):
64 """ 65 Populates the device list. 66 """ 67 @formreq
68 - def __call__(self, *args, **kwargs):
69 return self._getJSONDeviceInfo(*args, **kwargs)
70 71 @json
72 - def _getJSONDeviceInfo(self, offset=0, count=50, filter='', 73 orderby='id', orderdir='asc'):
74 """ 75 Get devices under self according to criteria and return results as 76 JSON. 77 78 @return: A JSON representation of a tuple containing a list of lists of 79 device info, and the total number of matching devices 80 @rtype: "([[a, b, c], [a, b, c]], 17)" 81 """ 82 devList = AdvancedQueryDeviceList(self.context) 83 totalCount, devicelist = devList(offset, count, filter, orderby, 84 orderdir) 85 obs = [x.getObject() for x in devicelist] 86 if orderby=='getDeviceIp': 87 obs.sort(lambda a,b:ipsort(a.getDeviceIp(), b.getDeviceIp())) 88 if orderdir=='desc': obs.reverse() 89 results = [ob.getDataForJSON(minSeverity=2) + ['odd'] for ob in obs] 90 return results, totalCount
91 92
93 -class DeviceBatch(BrowserView):
94 """ 95 Given various criteria, figure out what devices are relevant and execute 96 the action specified. 97 """ 98 @formreq
99 - def __call__(self, *args, **kwargs):
100 return self._setDeviceBatchProps(*args, **kwargs)
101 102 @property
103 - def id(self):
104 """ 105 This can appear in the acquisition chain, and ZenModelBase.getDmd needs 106 an id attribute. 107 """ 108 return self.context.id
109
110 - def _setDeviceBatchProps(self, method='', extraarg=None, 111 selectstatus='none', goodevids=[], 112 badevids=[], offset=0, count=50, filter='', 113 orderby='id', orderdir='asc', **kwargs):
114 d = {'lockDevicesFromUpdates':'sendEventWhenBlocked', 115 'lockDevicesFromDeletion':'sendEventWhenBlocked', 116 'unlockDevices':'', 117 'setGroups':'groupPaths', 118 'setSystems':'systemPaths', 119 'setLocation':'locationPath', 120 'setPerformanceMonitor':'performanceMonitor', 121 'moveDevices':'moveTarget', 122 'removeDevices':('deleteStatus', 'deleteHistory', 'deletePerf'), 123 'setProdState':'state', 124 'setPriority':'priority' 125 } 126 if not method or not method in d: 127 IMessageSender(self.request).sendToBrowser( 128 'Unable to Perform Action', 129 'An empty or invalid action was attempted.', 130 priority=messaging.CRITICAL 131 ) 132 return self() 133 134 request = FakeRequest() 135 argdict = dict(REQUEST=request) 136 if d[method]: 137 if type(d[method]) in [tuple, list]: 138 for argName in d[method]: 139 argdict[argName] = self.request.get(argName, None) 140 else: 141 argdict[d[method]] = extraarg 142 action = getattr(self.context, method) 143 argdict['deviceNames'] = self._getDeviceBatch(selectstatus, 144 goodevids, badevids, offset, count, 145 filter, orderby, orderdir) 146 # This will call the method on the context, which will redirect to a 147 # new (or the same) screen and set a message 148 try: 149 result = action(**argdict) 150 except: 151 msgs = {'lockDevicesFromUpdates':'lock devices from updates', 152 'lockDevicesFromDeletion':'lock devices from deletion', 153 'unlockDevices':'unlock devices', 154 'setGroups':'change device groups', 155 'setSystems':'change device systems', 156 'setLocation':'set the location', 157 'setPerformanceMonitor':'set the performance monitor', 158 'moveDevices':'move devices', 159 'removeDevices':'delete devices', 160 'setProdState':'set production state', 161 'setPriority':'set priority' 162 } 163 IMessageSender(self.request).sendToBrowser( 164 'Unable to Perform Action', 165 'There was an error attempting to %s.' % msgs[method], 166 priority=messaging.CRITICAL 167 ) 168 else: 169 return result
170
171 - def _getDeviceBatch(self, selectstatus='none', goodevids=[], 172 badevids=[], offset=0, count=50, filter='', 173 orderby='id', orderdir='asc'):
174 unused(count, offset, orderby, orderdir) 175 if not isinstance(goodevids, (list, tuple)): 176 goodevids = [goodevids] 177 if not isinstance(badevids, (list, tuple)): 178 badevids = [badevids] 179 if selectstatus=='all': 180 idquery = ~In('id', badevids) 181 else: 182 idquery = In('id', goodevids) 183 filter = '(?is).*%s.*' % filter 184 filterquery = Or( 185 MatchRegexp('id', filter), 186 MatchRegexp('getDeviceIp', filter), 187 MatchRegexp('getProdState', filter), 188 MatchRegexp('getDeviceClassPath', filter) 189 ) 190 query = Eq('getPhysicalPath', self.context.absolute_url_path()) & idquery 191 query = query & filterquery 192 catalog = getattr(self.context, self.context.default_catalog) 193 objects = catalog.evalAdvancedQuery(query) 194 return [x['id'] for x in objects]
195