Package Products :: Package ZenUtils :: Module qverify |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.qverify

      1  #!/usr/bin/env python
      2  ##############################################################################
      3  #
      4  # Copyright (C) Zenoss, Inc. 2010, all rights reserved.
      5  #
      6  # This content is made available according to terms specified in
      7  # License.zenoss under the directory where your Zenoss product is installed.
      8  #
      9  ##############################################################################
     10
     11
     12  import sys
     13  from optparse import OptionParser
     14  from amqplib.client_0_8.connection import Connection
     15  from contextlib import closing
     16
     17  import Globals
     18  from Products.ZenUtils.GlobalConfig import getGlobalConfiguration
     19
     20  import logging
     21  LOG = logging.getLogger("zen.qverify")
     22
     23 -def _addSetting(name, settings, globalSettings, default=None):
     24      setting = globalSettings.get(name, default)
     25      if setting is not None:
     26          settings[name] = setting
     27
     28  _ZEN_AMQP_SETTINGS =  {
     29     'amqphost': 'localhost',
     30     'amqpport': 5672,
     31     'amqpuser': 'zenoss',
     32     'amqppassword': 'zenoss',
     33     'amqpvhost': '/zenoss',
     34     'amqpusessl': False,
     35     'amqpadminport': 55672,
     36     'amqpadminusessl': False,
     37  }
     38
     39 -class ZenAmqp(object):
     40
     41 -    def __init__(self):
     42          self._global_conf = getGlobalConfiguration()
     43
     44 -    def getAdminConnectionSettings(self, extraParams={}):
     45          zenSettings = {}
     46          for setting, default in _ZEN_AMQP_SETTINGS.items():
     47              _addSetting(setting, zenSettings, self._global_conf, default)
     48          zenSettings.update(extraParams)
     49          ssl = zenSettings.get('amqpadminusessl', False) in ('1', 'True', 'true', 1, True)
     50          settings = {
     51              'host': '%(amqphost)s:%(amqpadminport)s' % zenSettings,
     52              'userid': '%(amqpuser)s' % zenSettings,
     53              'password': '%(amqppassword)s' % zenSettings,
     54              'virtual_host': '%(amqpvhost)s' % zenSettings,
     55              'ssl': ssl,
     56          }
     57          # provide a method for overriding system supplied values
     58          for name, value in extraParams.items():
     59              if name not in _ZEN_AMQP_SETTINGS:
     60                  settings[name] = value
     61          return settings
     62
     63 -    def getConnectionSettings(self, extraParams={}):
     64          zenSettings = {}
     65          for setting, default in _ZEN_AMQP_SETTINGS.items():
     66              _addSetting(setting, zenSettings, self._global_conf, default)
     67          zenSettings.update(extraParams)
     68          ssl = zenSettings.get('amqpusessl', False) in ('1', 'True', 'true', 1, True)
     69          settings = {
     70              'host': '%(amqphost)s:%(amqpport)s' % zenSettings,
     71              'userid': '%(amqpuser)s' % zenSettings,
     72              'password': '%(amqppassword)s' % zenSettings,
     73              'virtual_host': '%(amqpvhost)s' % zenSettings,
     74              'ssl': ssl,
     75          }
     76          # provide a method for overriding system supplied values
     77          for name, value in extraParams.items():
     78              if name not in _ZEN_AMQP_SETTINGS:
     79                  settings[name] = value
     80          return settings
     81
     82 -    def getConnection(self):
     83          settings = self.getConnectionSettings()
     84          return Connection(**settings)
     85
     86 -    def getVersion(self):
     87          with closing(self.getConnection()) as conn:
     88               return conn.server_properties.get('version')
     89
     90 -class Main(object):
     91
     92 -    def __init__(self, verbose=False):
     93          self._verbose = verbose
     94          LOG.debug("Getting global conf")
     95          self._global_conf = getGlobalConfiguration()
     96
     97 -    def _get_setting(self, name):
     98          val = self._global_conf.get(name, None)
     99          if val is None:
    100              print >> sys.stderr, "global.conf setting %s must be set." % name
    101              sys.exit(1)
    102          return val
    103
    104 -    def verify(self, expected_version):
    105          conn = None
    106          rc = 1
    107          try:
    108              # verify all settings exist
    109              for setting in _ZEN_AMQP_SETTINGS:
    110                  self._get_setting(setting)
    111              conn = ZenAmqp().getConnection()
    112              server_version = conn.server_properties.get('version')
    113              e_ver = tuple(int(v) for v in expected_version.split('.'))
    114              s_ver = tuple(int(v) for v in server_version.split('.'))
    115              if s_ver < e_ver:
    116                  print >> sys.stderr, "Server version: %s < Expected version: %s" % (
    117                      server_version, expected_version)
    118                  rc = 2
    119              else:
    120                  if self._verbose:
    121                      print "Server version: %s" % server_version
    122                  rc = 0
    123          finally:
    124              if conn:
    125                  conn.close()
    126          sys.exit(rc)
    127
    128  if __name__=="__main__":
    129      usage = "%prog VERSION_NUMBER"
    130      epilog = "Verifies connectivity with the amqp server configued in global.conf and " \
    131               "checks if server version is >= VERSION_NUMBER. Returns exit code 1 if " \
    132               "connection fails, 2 if server version < VERSION_NUMBER, and 0 if " \
    133               "connection is OK and server version >= VERSION_NUMBER."
    134      parser = OptionParser(usage=usage, epilog=epilog)
    135      parser.add_option("--verbose", "-v", default=False, action='store_true')
    136      (options, args) = parser.parse_args()
    137
    138      if len(args) != 1:
    139          parser.print_help()
    140          sys.exit(1)
    141
    142      main = Main(options.verbose)
    143      main.verify(args[0])
    144