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

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

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2009, 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 re
     12  import sre_constants
     13  import logging
     14  log = logging.getLogger('zen.HubService.ProcessConfig')
     15
     16  import Globals
     17
     18  from Products.ZenCollector.services.config import CollectorConfigService
     19  from Products.ZenUtils.Utils import unused
     20  from Products.ZenCollector.services.config import DeviceProxy
     21  from Products.ZenEvents import Event
     22  from Products.ZenModel.OSProcessClass import OSProcessClass
     23  from Products.ZenModel.OSProcessOrganizer import OSProcessOrganizer
     24  from Products.ZenHub.zodb import onUpdate
     25  from Products.Zuul.interfaces import ICatalogTool
     26  unused(DeviceProxy)
     27
     28  from twisted.spread import pb
     29
     30 -class ProcessProxy(pb.Copyable, pb.RemoteCopy):
     31      """
     32      Track process-specific configuration data
     33      """
     34      name = None
     35      originalName = None
     36      ignoreParameters = False
     37      restart = None
     38      regex = None
     39      severity = Event.Warning
     40      cycleTime = None
     41      processClass = None
     42
     43 -    def __init__(self):
     44          pass
     45
     46 -    def __str__(self):
     47          """
     48          Override the Python default to represent ourselves as a string
     49          """
     50          return str(self.name)
     51      __repr__ = __str__
     52
     53
     54  pb.setUnjellyableForClass(ProcessProxy, ProcessProxy)
     55
     56
     57 -class ProcessConfig(CollectorConfigService):
     58
     59 -    def __init__(self, dmd, instance):
     60          deviceProxyAttributes = ('zMaxOIDPerRequest',)
     61          CollectorConfigService.__init__(self, dmd, instance, deviceProxyAttributes)
     62
     63 -    def _filterDevice(self, device):
     64          include = CollectorConfigService._filterDevice(self, device)
     65          include = include and device.snmpMonitorDevice()
     66
     67          return include
     68
     69 -    def _createDeviceProxy(self, device):
     70          procs = device.getMonitoredComponents(collector='zenprocess')
     71          if not procs:
     72              log.debug("Device %s has no monitored processes -- ignoring",
     73                        device.titleOrId())
     74              return None
     75
     76          proxy = CollectorConfigService._createDeviceProxy(self, device)
     77          proxy.configCycleInterval = self._prefs.processCycleInterval
     78
     79          proxy.name = device.id
     80          proxy.lastmodeltime = device.getLastChangeString()
     81          proxy.thresholds = []
     82          proxy.processes = {}
     83          proxy.snmpConnInfo = device.getSnmpConnInfo()
     84          for p in procs:
     85              # In case the catalog is out of sync above
     86              if not p.monitored():
     87                  log.debug("Skipping process %r - zMonitor disabled", p)
     88                  continue
     89              regex = getattr(p.osProcessClass(), 'regex', False)
     90              if regex:
     91                  try:
     92                      re.compile(regex)
     93                  except sre_constants.error, ex:
     94                      log.warn("OS process class %s has an invalid regex (%s): %s",
     95                               p.getOSProcessClass(), regex, ex)
     96                      continue
     97              else:
     98                  log.warn("OS process class %s has no defined regex, this process not being monitored",
     99                           p.getOSProcessClass())
    100                  continue
    101
    102              proc = ProcessProxy()
    103              proc.regex = regex
    104              proc.name = p.id
    105              proc.originalName = p.name()
    106              proc.ignoreParameters = (
    107                  getattr(p.osProcessClass(), 'ignoreParameters', False))
    108              proc.restart = p.alertOnRestart()
    109              proc.severity = p.getFailSeverity()
    110              proc.processClass = p.getOSProcessClass()
    111              proxy.processes[p.id] = proc
    112              proxy.thresholds.extend(p.getThresholdInstances('SNMP'))
    113
    114          if proxy.processes:
    115              return proxy
    116
    117      @onUpdate(OSProcessClass)
    118 -    def processClassUpdated(self, object, event):
    119          devices = set()
    120          for process in object.instances():
    121              device = process.device()
    122              if not device:
    123                  continue
    124              device = device.primaryAq()
    125              device_path = device.getPrimaryUrlPath()
    126              if not device_path in devices:
    127                  self._notifyAll(device)
    128                  devices.add(device_path)
    129
    130      @onUpdate(OSProcessOrganizer)
    131 -    def processOrganizerUpdated(self, object, event):
    132          catalog = ICatalogTool(object.primaryAq())
    133          results = catalog.search(OSProcessClass)
    134          if not results.total:
    135              return
    136          devices = set()
    137          for organizer in results:
    138              if results.areBrains:
    139                  organizer = organizer.getObject()
    140              for process in organizer.instances():
    141                  device = process.device()
    142                  if not device:
    143                      continue
    144                  device = device.primaryAq()
    145                  device_path = device.getPrimaryUrlPath()
    146                  if not device_path in devices:
    147                      self._notifyAll(device)
    148                      devices.add(device_path)
    149
    150
    151  if __name__ == '__main__':
    152      from Products.ZenHub.ServiceTester import ServiceTester
    153      tester = ServiceTester(ProcessConfig)
    154 -    def printer(config):
    155          for proc in config.processes.values():
    156              print '\t'.join([proc.name, str(proc.ignoreParameters), proc.regex])
    157      tester.printDeviceProxy = printer
    158      tester.showDeviceInfo()
    159