Package Products :: Package ZenStatus :: Module zenping |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenStatus.zenping

     1  #! /usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  ##############################################################################
     4  #
     5  # Copyright (C) Zenoss, Inc. 2007, 2010, 2011, all rights reserved.
     6  #
     7  # This content is made available according to terms specified in
     8  # License.zenoss under the directory where your Zenoss product is installed.
     9  #
    10  ##############################################################################
    11
    12
    13  __doc__ = """zenping
    14
    15  Determines the availability of a IP addresses using ping (ICMP).
    16
    17  """
    18  import os.path
    19  import re
    20  import socket
    21  import time
    22  import logging
    23  log = logging.getLogger("zen.zenping")
    24
    25  import Globals
    26  import zope.interface
    27  import zope.component
    28
    29  from Products import ZenStatus
    30  from Products.ZenCollector import daemon
    31  from Products.ZenCollector import interfaces
    32  from Products.ZenCollector import tasks
    33  from Products.ZenUtils import IpUtil
    34
    35  # perform some imports to allow twisted's PB to serialize these objects
    36  from Products.ZenUtils.Utils import unused
    37  from Products.ZenCollector.services.config import DeviceProxy
    38  from Products.ZenHub.services.PingPerformanceConfig import PingPerformanceConfig
    39  unused(DeviceProxy)
    40  unused(PingPerformanceConfig)
    41
    42  # define some constants strings
    43  COLLECTOR_NAME = "zenping"
    44  CONFIG_SERVICE = 'Products.ZenHub.services.PingPerformanceConfig'
    45
    46 -class PerIpAddressTaskSplitter(tasks.SubConfigurationTaskSplitter):
    47      subconfigName = 'monitoredIps'
    48
    49 -    def makeConfigKey(self, config, subconfig):
    50          return (config.id, subconfig.cycleTime, IpUtil.ipunwrap(subconfig.ip))
    51
    52 -def getPingBackend():
    53      """
    54      Introspect the command line args to find --ping-backend because
    55      buildOptions doesn't get called until later.
    56      """
    57      import sys
    58      backend = 'nmap'
    59      try:
    60          for i, arg in enumerate(sys.argv):
    61              if arg == '--ping-backend':
    62                  return sys.argv[i+1]
    63              elif arg.startswith('--ping-backend='):
    64                  return arg.split('=', 1)[1]
    65      except IndexError:
    66          pass
    67      return backend
    68
    69
    70  if __name__ == '__main__':
    71
    72      # load zcml for the product
    73      import Products.ZenossStartup
    74      from Products.Five import zcml
    75      zcml.load_site()
    76      pingBackend = getPingBackend()
    77
    78      myPreferences = zope.component.getUtility(ZenStatus.interfaces.IPingCollectionPreferences, pingBackend)
    79      myTaskFactory = zope.component.getUtility(ZenStatus.interfaces.IPingTaskFactory, pingBackend)
    80      myTaskSplitter = PerIpAddressTaskSplitter(myTaskFactory)
    81      myDaemon = daemon.CollectorDaemon(
    82          myPreferences,
    83          myTaskSplitter,
    84          stoppingCallback=myPreferences.preShutdown,
    85      )
    86      myDaemon.run()
    87