Package Products :: Package ZenHub :: Package services :: Module ModelerService |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenHub.services.ModelerService

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2008, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      7  #
      8  ##############################################################################
      9
     10
     11  import Globals
     12
     13  from Acquisition import aq_base
     14  from twisted.internet import defer, reactor
     15  from ZODB.transact import transact
     16  from PerformanceConfig import PerformanceConfig
     17  from Products.ZenHub.PBDaemon import translateError
     18  from Products.DataCollector.DeviceProxy import DeviceProxy
     19  from Products.DataCollector.Plugins import loadPlugins
     20
     21  import logging
     22  log = logging.getLogger('zen.ModelerService')
     23
     24 -class ModelerService(PerformanceConfig):
     25
     26      plugins = None
     27
     28 -    def __init__(self, dmd, instance):
     29          PerformanceConfig.__init__(self, dmd, instance)
     30          self.config = self.dmd.Monitors.Performance._getOb(self.instance)
     31          self.methodPriorityMap = {
     32              'applyDataMaps': 0.5,
     33              }
     34
     35 -    def createDeviceProxy(self, dev, skipModelMsg=''):
     36          if self.plugins is None:
     37              self.plugins = {}
     38              for loader in loadPlugins(self.dmd):
     39                  try:
     40                      plugin = loader.create()
     41                      plugin.loader = loader
     42                      self.plugins[plugin.name()] = plugin
     43                  except Exception, ex:
     44                      log.exception(ex)
     45
     46          result = DeviceProxy()
     47          result.id = dev.getId()
     48          result.skipModelMsg = skipModelMsg
     49
     50          if not skipModelMsg:
     51              if not dev.manageIp:
     52                  dev.setManageIp()
     53              result.manageIp = dev.manageIp
     54              result.plugins = []
     55              for name in dev.zCollectorPlugins:
     56                  plugin = self.plugins.get(name, None)
     57                  log.debug('checking plugin %s for device %s' % (name, dev.getId()))
     58                  if plugin and plugin.condition(dev, log):
     59                      log.debug('adding plugin %s for device %s' % (name,dev.getId()))
     60                      result.plugins.append(plugin.loader)
     61                      plugin.copyDataToProxy(dev, result)
     62              result.temp_device = dev.isTempDevice()
     63          return result
     64
     65      @translateError
     66 -    def remote_getClassCollectorPlugins(self):
     67          result = []
     68          for dc in self.dmd.Devices.getSubOrganizers():
     69              localPlugins = getattr(aq_base(dc), 'zCollectorPlugins', False)
     70              if not localPlugins: continue
     71              result.append((dc.getOrganizerName(), localPlugins))
     72          return result
     73
     74      @translateError
     75 -    def remote_getDeviceConfig(self, names, checkStatus=False):
     76          result = []
     77          for name in names:
     78              device = self.getPerformanceMonitor().findDevice(name)
     79              if not device:
     80                  continue
     81              device = device.primaryAq()
     82              skipModelMsg = ''
     83
     84              if device.isLockedFromUpdates():
     85                  skipModelMsg = "device %s is locked, skipping modeling" % device.id
     86              if checkStatus and (device.getPingStatus() > 0
     87                                  or device.getSnmpStatus() > 0):
     88                  skipModelMsg = "device %s is down skipping modeling" % device.id
     89              if (device.productionState <
     90                  device.getProperty('zProdStateThreshold', 0)):
     91                  skipModelMsg = "device %s is below zProdStateThreshold" % device.id
     92              if skipModelMsg:
     93                  log.info(skipModelMsg)
     94
     95              result.append(self.createDeviceProxy(device, skipModelMsg))
     96          return result
     97
     98      @translateError
     99 -    def remote_getDeviceListByMonitor(self, monitor=None):
    100          if monitor is None:
    101              monitor = self.instance
    102          monitor = self.dmd.Monitors.Performance._getOb(monitor)
    103          return [d.id for d in monitor.devices.objectValuesGen()]
    104
    105      @translateError
    106 -    def remote_getDeviceListByOrganizer(self, organizer, monitor=None):
    107          if monitor is None:
    108              monitor = self.instance
    109          root = self.dmd.Devices.getOrganizer(organizer)
    110          return [d.id for d in root.getSubDevicesGen()
    111              if d.getPerformanceServerName() == monitor]
    112
    113      @translateError
    114 -    def remote_applyDataMaps(self, device, maps, devclass=None, setLastCollection=False):
    115          from Products.DataCollector.ApplyDataMap import ApplyDataMap
    116          device = self.getPerformanceMonitor().findDevice(device)
    117          adm = ApplyDataMap(self)
    118          adm.setDeviceClass(device, devclass)
    119          def inner(map):
    120              def action():
    121                  return bool(adm._applyDataMap(device, map))
    122              return self._do_with_retries(action)
    123
    124          changed = False
    125          for map in maps:
    126              result = inner(map)
    127              changed = changed or result
    128
    129          if setLastCollection:
    130              self._setSnmpLastCollection(device)
    131
    132          return changed
    133
    134 -    def _setSnmpLastCollection(self, device):
    135          transactional = transact(device.setSnmpLastCollection)
    136          return self._do_with_retries(transactional)
    137
    138      @translateError
    139 -    def remote_setSnmpLastCollection(self, device):
    140          device = self.getPerformanceMonitor().findDevice(device)
    141          self._setSnmpLastCollection(device)
    142
    143 -    def _do_with_retries(self, action):
    144          from ZODB.POSException import StorageError
    145          max_attempts = 3
    146          for attempt_num in range(max_attempts):
    147              try:
    148                  return action()
    149              except StorageError as e:
    150                  if attempt_num == max_attempts-1:
    151                      msg = "{0}, maximum retries reached".format(e)
    152                  else:
    153                      msg = "{0}, retrying".format(e)
    154                  log.info(msg)
    155
    156
    157      @transact
    158      @translateError
    159 -    def remote_setSnmpConnectionInfo(self, device, version, port, community):
    160          device = self.getPerformanceMonitor().findDevice(device)
    161          device.updateDevice(zSnmpVer=version,
    162                              zSnmpPort=port,
    163                              zSnmpCommunity=community)
    164
    165 -    def pushConfig(self, device):
    166          from twisted.internet.defer import succeed
    167          return succeed(device)
    168
    169
    170  if __name__ == '__main__':
    171      from Products.ZenHub.ServiceTester import ServiceTester
    172      tester = ServiceTester(ModelerService)
    173
    174 -    def configprinter(config):
    175          print "%s (%s) Plugins" % (config.id, config.manageIp)
    176          print sorted(x.pluginName for x in config.plugins)
    177
    178 -    def showDeviceInfo():
    179          if tester.options.device:
    180              name = tester.options.device
    181              config = tester.service.remote_getDeviceConfig([name])
    182              if config:
    183                  print "Config for %s =" % name
    184                  configprinter(config[0])
    185              else:
    186                  log.warn("No configs found for %s", name)
    187          else:
    188              collector = tester.options.monitor
    189              devices = tester.service.remote_getDeviceListByMonitor(collector)
    190              devices = sorted(devices)
    191              print "Device list = %s" % devices
    192
    193      showDeviceInfo()
    194