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

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

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, 2010, 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  __doc__ = '''SnmpPerformanceConfig
     12
     13  Provides configuration to zenperfsnmp clients.
     14  '''
     15
     16  import re
     17  from pprint import pformat
     18  import logging
     19  log = logging.getLogger('zen.HubService.SnmpPerformanceConfig')
     20
     21  import Globals
     22  from twisted.spread import pb
     23  from Products.ZenCollector.services.config import DeviceProxy, CollectorConfigService
     24
     25 -def get_component_manage_ip(component, default=None):
     26      get_manage_ip = getattr(component, "getManageIp", None)
     27      if get_manage_ip is None:
     28          return default
     29      return get_manage_ip()
     30
     31 -class SnmpDeviceProxy(DeviceProxy, pb.Copyable, pb.RemoteCopy):
     32
     33 -    def __repr__(self):
     34          sci = getattr(self, "snmpConnInfo", None)
     35          scimi = None if (sci is None) else sci.manageIp
     36          return pformat({"id": self.id,
     37                          "_config_id": getattr(self, "_config_id", None),
     38                          "manageIp": self.manageIp,
     39                          "snmpConnInfo.manageIp": scimi,
     40                          "oids": getattr(self, "oids", None)})
     41
     42  pb.setUnjellyableForClass(SnmpDeviceProxy, SnmpDeviceProxy)
     43
     44
     45 -class SnmpPerformanceConfig(CollectorConfigService):
     46 -    def __init__(self, dmd, instance):
     47          deviceProxyAttributes = ('zMaxOIDPerRequest',
     48                                   'zSnmpMonitorIgnore',
     49                                   'zSnmpAuthPassword',
     50                                   'zSnmpAuthType',
     51                                   'zSnmpCommunity',
     52                                   'zSnmpPort',
     53                                   'zSnmpPrivPassword',
     54                                   'zSnmpPrivType',
     55                                   'zSnmpSecurityName',
     56                                   'zSnmpTimeout',
     57                                   'zSnmpTries',
     58                                   'zSnmpVer',
     59                                   'zSnmpCollectionInterval',
     60                                  )
     61          CollectorConfigService.__init__(self, dmd, instance,
     62                                          deviceProxyAttributes)
     63
     64 -    def _filterDevice(self, device):
     65          include = CollectorConfigService._filterDevice(self, device)
     66
     67          if getattr(device, 'zSnmpMonitorIgnore', False):
     68              self.log.debug("Device %s skipped because zSnmpMonitorIgnore is True",
     69                             device.id)
     70              include = False
     71
     72          if not device.getManageIp():
     73              self.log.debug("Device %s skipped because its management IP address is blank.",
     74                             device.id)
     75              include = False
     76
     77          return include
     78
     79
     80 -    def _transform_oid(self, oid, comp):
     81          """lookup the index"""
     82          index = None
     83          snmpindex_dct = getattr(comp, "snmpindex_dct", None)
     84          if snmpindex_dct is not None:
     85              for prefix, index_ in snmpindex_dct.iteritems():
     86                  if oid.startswith(prefix):
     87                      index = index_
     88                      break
     89          if index is None:
     90              index = getattr(comp, "ifindex", comp.snmpindex)
     91          return "{0}.{1}".format(oid, index) if index else oid
     92
     93
     94 -    def _getComponentConfig(self, comp, perfServer, oids):
     95          """
     96          SNMP components can build up the actual OID based on a base OID and
     97          the snmpindex of the component.
     98          """
     99          if comp.snmpIgnore():
    100              return None
    101
    102          validOID = re.compile(r'(?:\.?\d+)+$')
    103          basepath = comp.rrdPath()
    104          for templ in comp.getRRDTemplates():
    105              for ds in templ.getRRDDataSources("SNMP"):
    106                  if not ds.enabled or not ds.oid:
    107                      continue
    108
    109                  oid = self._transform_oid(ds.oid.strip("."), comp)
    110                  if not oid:
    111                      log.warn("The data source %s OID is blank -- ignoring", ds.id)
    112                      continue
    113                  elif not validOID.match(oid):
    114                      msg =  "The OID %s is invalid -- ignoring" % oid
    115                      self.sendEvent(dict(
    116                          device=comp.device().id, component=ds.getPrimaryUrlPath(),
    117                          eventClass='/Status/Snmp', severity=Warning, summary=msg,
    118                      ))
    119
    120                  for dp in ds.getRRDDataPoints():
    121                      # Everything under ZenModel *should* use titleOrId but it doesn't
    122                      cname = comp.viewName() if comp.meta_type != "Device" else dp.id
    123                      oidData = (cname,
    124                                   "/".join((basepath, dp.name())),
    125                                   dp.rrdtype,
    126                                   dp.getRRDCreateCommand(perfServer).strip(),
    127                                   dp.rrdmin, dp.rrdmax)
    128
    129                      # An OID can appear in multiple data sources/data points
    130                      oids.setdefault(oid, []).append(oidData)
    131
    132          return comp.getThresholdInstances('SNMP')
    133
    134 -    def _createDeviceProxies(self, device):
    135          manage_ips = {device.manageIp: ([], False)}
    136          components = device.os.getMonitoredComponents(collector="zenperfsnmp")
    137          for component in components:
    138              manage_ip = get_component_manage_ip(component, device.manageIp)
    139              if manage_ip not in manage_ips:
    140                  log.debug("Adding manage IP %s from %r" % (manage_ip, component))
    141                  manage_ips[manage_ip] = ([], True)
    142              manage_ips[manage_ip][0].append(component)
    143          proxies = []
    144          for manage_ip, (components, components_only) in manage_ips.items():
    145              proxy = self._createDeviceProxy(device, manage_ip, components, components_only)
    146              if proxy is not None:
    147                  proxies.append(proxy)
    148          return proxies
    149
    150 -    def _createDeviceProxy(self, device, manage_ip=None, components=(), components_only=False):
    151          proxy = SnmpDeviceProxy()
    152          proxy = CollectorConfigService._createDeviceProxy(self, device, proxy)
    153          proxy.snmpConnInfo = device.getSnmpConnInfo()
    154          if manage_ip is not None and manage_ip != device.manageIp:
    155              proxy._config_id = device.id + "_" + manage_ip
    156              proxy.snmpConnInfo.manageIp = manage_ip
    157          proxy.configCycleInterval = self._prefs.perfsnmpCycleInterval
    158          proxy.cycleInterval = getattr(device, 'zSnmpCollectionInterval', 300)
    159          proxy.name = device.id
    160          proxy.device = device.id
    161          proxy.lastmodeltime = device.getLastChangeString()
    162          proxy.lastChangeTime = float(device.getLastChange())
    163
    164          # Gather the datapoints to retrieve
    165          perfServer = device.getPerformanceServer()
    166          proxy.oids = {}
    167          proxy.thresholds = []
    168          if not components_only:
    169              # First for the device....
    170              threshs = self._getComponentConfig(device, perfServer, proxy.oids)
    171              if threshs:
    172                  proxy.thresholds.extend(threshs)
    173          # And now for its components
    174          for comp in components:
    175              threshs = self._getComponentConfig(comp, perfServer, proxy.oids)
    176              if threshs:
    177                  proxy.thresholds.extend(threshs)
    178
    179          if proxy.oids:
    180              return proxy
    181
    182
    183  if __name__ == '__main__':
    184      from Products.ZenHub.ServiceTester import ServiceTester
    185      tester = ServiceTester(SnmpPerformanceConfig)
    186 -    def printer(proxy):
    187          for oid in sorted(proxy.oids):
    188              print oid
    189      tester.printDeviceProxy = printer
    190      tester.showDeviceInfo()
    191