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

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

      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__ = """PingConfig
     12
     13  Provides configuration to zenping per-device based on:
     14   * the zPingMonitorIgnore
     15   * whether the datasource is enabled or not for the interface
     16  """
     17
     18  import logging
     19  log = logging.getLogger('zen.HubService.PingPerformanceConfig')
     20
     21  from ipaddr import IPAddress
     22  from twisted.spread import pb
     23
     24  import Globals
     25  from Products.ZenCollector.services.config import CollectorConfigService
     26  from Products.ZenEvents.ZenEventClasses import Error, Clear
     27  from Products.ZenUtils.IpUtil import ipunwrap, ipstrip
     28
     29
     30 -class IpAddressProxy(pb.Copyable, pb.RemoteCopy):
     31      """
     32      Represents a pingable IP address on an IP interface. A single
     33      DeviceProxy config will have multiple IP address proxy components
     34      (for each IP address on each IP interface zenping should monitor)
     35      """
     36 -    def __init__(self, ip, ipVersion=4, iface='', basepath='', ds=None,
     37                   perfServer='localhost'):
     38          self.ip = ipunwrap(ip)
     39          self.ipVersion = ipVersion
     40          self.iface = iface
     41          self.cycleTime =  getattr(ds, 'cycleTime', 60)
     42          self.tries =      getattr(ds, 'attempts', 2)
     43          self.sampleSize = getattr(ds, 'sampleSize', 1)
     44          self.points = []
     45
     46          if not ds:
     47              # Don't need the datapoints to get the IP monitored
     48              return
     49
     50          log.debug("Using the %s template settings for IP %s",
     51                    ds.rrdTemplate().getPrimaryUrlPath(), self.ip)
     52          for dp in ds.getRRDDataPoints():
     53              ipdData = (dp.id,
     54                         "/".join((basepath, dp.name())),
     55                         dp.rrdtype,
     56                         dp.getRRDCreateCommand(perfServer).strip(),
     57                         dp.rrdmin, dp.rrdmax)
     58
     59              self.points.append(ipdData)
     60
     61 -    def __str__(self):
     62          return "IPv%d %s iface: '%s' cycleTime: %ss ping_attempts: %d retries: %d" % (
     63                 self.ipVersion, self.ip, self.iface, self.cycleTime,
     64                 self.sampleSize, self.tries)
     65
     66
     67  pb.setUnjellyableForClass(IpAddressProxy, IpAddressProxy)
     68
     69
     70 -class PingPerformanceConfig(CollectorConfigService):
     71 -    def __init__(self, dmd, instance):
     72          deviceProxyAttributes = (
     73                                   'zPingMonitorIgnore',
     74                                  )
     75          CollectorConfigService.__init__(self, dmd, instance,
     76                                          deviceProxyAttributes)
     77
     78 -    def _filterDevice(self, device):
     79          include = CollectorConfigService._filterDevice(self, device)
     80
     81          if not device.monitorDevice():
     82              include = False
     83
     84          if device.zPingMonitorIgnore:
     85              include = False
     86
     87          if not device.getManageIp():
     88              self.log.debug("Device %s skipped because its management IP address is blank.",
     89                             device.id)
     90              include = False
     91
     92          return include
     93
     94 -    def _getComponentConfig(self, iface, perfServer, monitoredIps):
     95          """
     96          All IP addresses on all IP interfaces can be pingable.
     97          """
     98          basepath = iface.rrdPath()
     99          title = iface.titleOrId()
    100          for templ in iface.getRRDTemplates():
    101              for ipAddress in iface.ipaddresses():
    102                  ip = ipAddress.id
    103                  if not ip or ip in ('127.0.0.1', '0.0.0.0', '::', '::1'):
    104                      log.debug("The %s interface IP is '%s' -- ignoring",
    105                                title, ip)
    106                      continue
    107
    108                  dsList = [ds for ds in templ.getRRDDataSources('PING') \
    109                               if ds.enabled]
    110                  if dsList:
    111                      ipVersion = getattr(ipAddress, 'version', 4)
    112                      ipProxy = IpAddressProxy(ip, ipVersion=ipVersion,
    113                                               iface=title, ds=dsList[0],
    114                                               basepath=basepath, perfServer=perfServer)
    115                      monitoredIps.append(ipProxy)
    116
    117 -    def _addManageIp(self, device, perfServer, proxy):
    118          """
    119          Add the management IP and any associated datapoints to the IPs to monitor.
    120          """
    121          basepath = device.rrdPath()
    122          title = ''
    123          ip = device.manageIp
    124          if not ip or ip in ('127.0.0.1', '0.0.0.0', '::', '::1'):
    125              return
    126          ipObj = IPAddress(ipstrip(ip))
    127
    128          # Look for device-level templates with PING datasources
    129          addedIp = False
    130          for templ in device.getRRDTemplates():
    131              dsList = [ds for ds in templ.getRRDDataSources('PING') \
    132                               if ds.enabled]
    133              if dsList:
    134                  ipProxy = IpAddressProxy(ip, ipVersion=ipObj.version,
    135                                           iface=title, ds=dsList[0],
    136                                           basepath=basepath, perfServer=perfServer)
    137                  proxy.monitoredIps.append(ipProxy)
    138                  addedIp = True
    139
    140          threshs = device.getThresholdInstances('PING')
    141          if threshs:
    142              proxy.thresholds.extend(threshs)
    143
    144          # Add without datapoints if nothing's defined....
    145          if not addedIp:
    146              ipProxy = IpAddressProxy(ip, ipVersion=ipObj.version,
    147                                       iface=title,
    148                                       basepath=basepath, perfServer=perfServer)
    149              proxy.monitoredIps.append(ipProxy)
    150
    151 -    def _createDeviceProxy(self, device):
    152          proxy = CollectorConfigService._createDeviceProxy(self, device)
    153
    154          proxy.name = device.id
    155          proxy.device = device.id
    156          proxy.lastmodeltime = device.getLastChangeString()
    157          proxy.lastChangeTime = float(device.getLastChange())
    158
    159          perfServer = device.getPerformanceServer()
    160          proxy.thresholds = []
    161          proxy.monitoredIps = []
    162          for iface in device.os.interfaces():
    163              self._getComponentConfig(iface, perfServer, proxy.monitoredIps)
    164              threshs = iface.getThresholdInstances('PING')
    165              if threshs:
    166                  proxy.thresholds.extend(threshs)
    167
    168          if not proxy.monitoredIps:
    169              log.debug("%s has no interface templates -- just using management IP %s",
    170                        device.titleOrId(), device.manageIp)
    171              self._addManageIp(device, perfServer, proxy)
    172
    173          elif device.manageIp not in [x.ip for x in proxy.monitoredIps]:
    174              # Note: most commonly occurs for SNMP fakeout devices which replay
    175              #       data from real devices, but from a different IP address
    176              #       than what captured the data
    177              log.debug("%s doesn't have an interface for management IP %s",
    178                        device.titleOrId(), device.manageIp)
    179              self._addManageIp(device, perfServer, proxy)
    180
    181          return proxy
    182
    183
    184  if __name__ == '__main__':
    185      from Products.ZenHub.ServiceTester import ServiceTester
    186      tester = ServiceTester(PingPerformanceConfig)
    187 -    def printer(config):
    188          for ip in config.monitoredIps:
    189              print '\t', ip, '\t', [x[0] for x in ip.points]
    190      tester.printDeviceProxy = printer
    191      tester.showDeviceInfo()
    192