1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__ = """DeviceOrganizer
15 Base class for device organizers
16 """
17
18 from itertools import ifilter
19 from types import StringTypes
20
21 from AccessControl import ClassSecurityInfo
22 from Globals import InitializeClass
23
24 from Organizer import Organizer
25 from DeviceManagerBase import DeviceManagerBase
26 from Commandable import Commandable
27 from ZenMenuable import ZenMenuable
28 from MaintenanceWindowable import MaintenanceWindowable
29 from AdministrativeRoleable import AdministrativeRoleable
30
31 from Products.CMFCore.utils import getToolByName
32
33 from Products.ZenRelations.RelSchema import *
34 from Products.ZenWidgets.interfaces import IMessageSender
35
36 from ZenossSecurity import *
37
38 from Products.ZenUtils.Utils import unused
39 from Products.ZenWidgets import messaging
40
41 import logging
42 LOG = logging.getLogger('ZenModel.DeviceOrganizer')
43
44 -class DeviceOrganizer(Organizer, DeviceManagerBase, Commandable, ZenMenuable,
45 MaintenanceWindowable, AdministrativeRoleable):
46 """
47 DeviceOrganizer is the base class for device organizers.
48 It has lots of methods for rolling up device statistics and information.
49 """
50
51 security = ClassSecurityInfo()
52
53
54 factory_type_information = (
55 {
56 'immediate_view' : 'deviceOrganizerStatus',
57 'actions' :
58 (
59 { 'id' : 'status'
60 , 'name' : 'Status'
61 , 'action' : 'deviceOrganizerStatus'
62 , 'permissions' : (ZEN_VIEW, )
63 },
64 { 'id' : 'events'
65 , 'name' : 'Events'
66 , 'action' : 'viewEvents'
67 , 'permissions' : (ZEN_VIEW, )
68 },
69
70
71
72
73
74 { 'id' : 'manage'
75 , 'name' : 'Administration'
76 , 'action' : 'deviceOrganizerManage'
77 , 'permissions' : (ZEN_MANAGE_DMD,)
78 },
79 )
80 },
81 )
82
83 _relations = Organizer._relations + (
84 ("maintenanceWindows", ToManyCont(
85 ToOne, "Products.ZenModel.MaintenanceWindow", "productionState")),
86 ("adminRoles", ToManyCont(
87 ToOne,"Products.ZenModel.AdministrativeRole","managedObject")),
88 ('userCommands', ToManyCont(
89 ToOne, 'Products.ZenModel.UserCommand', 'commandable')),
90 ('zenMenus', ToManyCont(
91 ToOne, 'Products.ZenModel.ZenMenu', 'menuable')),
92 )
93
94 security.declareProtected(ZEN_COMMON, "getSubDevices")
96 """
97 Get all the devices under an instance of a DeviceOrganizer
98
99 @param devfilter: Filter function applied to returned list
100 @type devfilter: function
101 @return: Devices
102 @rtype: list
103
104 """
105 catalog = getToolByName(self.dmd.Devices, self.dmd.Devices.default_catalog)
106
107 if not 'path' in catalog.indexes():
108 LOG.warn('Please run zenmigrate to create device path indexes.')
109 return self.getSubDevices_recursive(devfilter)
110
111 brains = catalog(path="/".join(self.getPhysicalPath()))
112 devices = (b.getObject() for b in brains)
113 devices = ifilter(lambda dev:self.checkRemotePerm(ZEN_VIEW, dev),
114 devices)
115 devices = ifilter(devfilter, devices)
116 return list(devices)
117
118 security.declareProtected(ZEN_VIEW, "getSubDevicesGen")
133
134 security.declareProtected(ZEN_COMMON, "getSubDevices_recursive")
145
146 security.declareProtected(ZEN_VIEW, "getSubDevicesGen")
148 """get all the devices under and instance of a DeviceGroup"""
149 devrelobj = getattr(self, devrel, None)
150 if not devrelobj:
151 raise AttributeError( "%s not found on %s" % (devrel, self.id) )
152 for dev in devrelobj.objectValuesGen():
153 yield dev
154 for subgroup in self.children():
155 for dev in subgroup.getSubDevicesGen_recursive(devrel):
156 yield dev
157
159 """get all the devices under and instance of a DeviceGroup"""
160 devices = getattr(self, devrel, None)
161 if not devices:
162 raise AttributeError( "%s not found on %s" % (devrel, self.id) )
163
164
166 """Return monitored components for devices within this DeviceOrganizer.
167 """
168 cmps = []
169 for dev in self.getSubDevicesGen():
170 cmps.extend(dev.getMonitoredComponents())
171 return cmps
172
173
191
192
200
201
208
209
216
217
219 """Build a device list for set methods"""
220 if isinstance(deviceNames, basestring):
221 deviceNames = [deviceNames]
222 return [d.primaryAq() for d in self.getSubDevices()
223 if deviceNames is None or d.id in deviceNames
224 or d.getPrimaryId() in deviceNames]
225
226
228 """Return list of all organizers excluding our self."""
229 targets = filter(lambda x: x != self.getOrganizerName(),
230 self.dmd.Devices.getOrganizerNames())
231 targets.sort(lambda x,y: cmp(x.lower(), y.lower()))
232 return targets
233
234
247
248
249 - def _handleOrganizerCall(self, arg=None, deviceNames=None, \
250 isOrganizer=False, REQUEST=None, \
251 deviceMethod=None):
252 """ Handle the many many methods that simply call one
253 method on device differently"""
254
255 if not deviceMethod: return
256 if deviceNames is None and not isOrganizer:
257 if REQUEST:
258 messaging.IMessageSender(self).sendToBrowser(
259 'Error',
260 'No devices were selected',
261 priority=messaging.WARNING
262 )
263 return self.callZenScreen(REQUEST)
264 for dev in self._buildDeviceList(deviceNames):
265 devMethod = getattr(dev, deviceMethod, None)
266 if devMethod and arg:
267 devMethod(arg)
268 elif devMethod:
269 devMethod()
270
271
274 """build the standard return message for the various set
275 methods"""
276 if checkPaths:
277 if paths:
278 if type(paths) not in StringTypes:
279 paths = ", ".join(paths)
280 message += paths
281 else:
282 message = "%s unset" % message.split(" ")[0]
283 if self.REQUEST.has_key('oneKeyValueSoInstanceIsntEmptyAndEvalToFalse'):
284 return message
285 else:
286 IMessageSender(self).sendToBrowser(title, message)
287 return self.callZenScreen(self.REQUEST)
288
289
290 - def setProdState(self, state, deviceNames=None,
291 isOrganizer=False, REQUEST=None):
301
302
303 - def setPriority(self, priority, deviceNames=None,
304 isOrganizer=False, REQUEST=None):
314
315
332
333
334 - def setGroups(self, groupPaths=None, deviceNames=None,
335 isOrganizer=False, REQUEST=None):
336 """ Provide a method to set device groups from any organizer """
337 if not groupPaths: groupPaths = []
338 self._handleOrganizerCall(groupPaths, deviceNames, isOrganizer, \
339 REQUEST, "setGroups")
340 if REQUEST:
341 msg = "Groups set to"
342 return self._buildReturnMessage('Groups Set', msg, groupPaths, True)
343
344
345 - def setSystems(self, systemPaths=None, deviceNames=None,
346 isOrganizer=False, REQUEST=None):
347 """ Provide a method to set device systems from any organizer """
348 if not systemPaths: systemPaths = []
349 self._handleOrganizerCall(systemPaths, deviceNames, isOrganizer, \
350 REQUEST, "setSystems")
351 if REQUEST:
352 msg = "Systems set to"
353 return self._buildReturnMessage('Systems Set', msg, systemPaths, True)
354
355 - def setLocation(self, locationPath="", deviceNames=None,
356 isOrganizer=False, REQUEST=None):
363
364 - def unlockDevices(self, deviceNames=None, isOrganizer=False, REQUEST=None):
371
372 - def lockDevicesFromDeletion(self, deviceNames=None,
373 sendEventWhenBlocked=None, isOrganizer=False, REQUEST=None):
380
381 - def lockDevicesFromUpdates(self, deviceNames=None,
382 sendEventWhenBlocked=None, isOrganizer=False, REQUEST=None):
389
390
392 """No action.
393 Index of subdevices will happen in manage_addAdministrativeRole
394 """
395 pass
396
398 """No action.
399 Unindex of subdevices will happen in manage_deleteAdministrativeRole
400 """
401 pass
402
421
422
440
441
461
462
469
476
483
484 - def _status(self, type, devrel="devices"):
485 """build status info for device in this device group"""
486 status = 0
487 statatt = "get%sStatusNumber" % type
488 devices = getattr(self, devrel, None)
489 if not devices:
490 raise AttributeError( "%s not found on %s" % (devrel, self.id) )
491 for device in devices():
492 if getattr(device, statatt, -1)() > 0:
493 status += 1
494 return status
495
497 """colors for status fields for device groups"""
498 retval = '#00ff00'
499 if status == -1:
500 retval = "#d02090"
501 elif status == 1:
502 retval = '#ffff00'
503 elif status == 2:
504 retval = '#ff9900'
505 elif status > 2:
506 retval = '#ff0000'
507 return retval
508
510 ''' Called by Commandable.doCommand() to ascertain objects on which
511 a UserCommand should be executed.
512 '''
513 return self.getSubDevices()
514
517
530
531 security.declareProtected(ZEN_VIEW, 'getIconPath')
533 """ Override the zProperty icon path and return a folder
534 """
535 return "/zport/dmd/img/icons/folder.png"
536
537 security.declareProtected(ZEN_VIEW, 'getPrettyLink')
539 """ Gets a link to this object, plus an icon """
540 href = self.getPrimaryUrlPath().replace('%','%%')
541 linktemplate = "<a href='"+href+"' class='prettylink'>%s</a>"
542 icon = ("<div class='device-icon-container'> "
543 "<img class='device-icon' src='%s'/> "
544 "</div>") % self.getIconPath()
545 name = self.getPrimaryDmdId()
546 if noicon: icon=''
547 if shortDesc: name = self.id
548 rendered = icon + name
549 if not self.checkRemotePerm("View", self):
550 return rendered
551 else:
552 return linktemplate % rendered
553
554 InitializeClass(DeviceOrganizer)
555