1
2
3
4
5
6
7
8
9
10
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
24 """
25 Adapter providing list of devices according to various criteria.
26 """
29
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
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
64 """
65 Populates the device list.
66 """
67 @formreq
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
94 """
95 Given various criteria, figure out what devices are relevant and execute
96 the action specified.
97 """
98 @formreq
101
102 @property
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
147
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