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

# Source Code for Module Products.ZenUtils.dbverify

     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  import subprocess
    15  import re
    16  import Globals
    17  from Products.ZenUtils.Utils import zenPath
    18
    19 -class Main(object):
    20
    21 -    def verify(self, component, expected_version):
    22
    23          s = None
    24          # connect to out db client abstraction
    25          zendb = zenPath("Products", "ZenUtils", "ZenDB.py")
    26          s = subprocess.Popen([zendb, "--usedb", component, "--execsql", "SELECT VERSION();"],
    27              stdout=subprocess.PIPE, stderr=subprocess.PIPE
    28          )
    29
    30          stdout, stderr = s.communicate()
    31          if s.returncode != 0:
    32             print >> sys.stderr, "Error executing db cmdline client: %s %s " % (stdout, stderr,)
    33             sys.exit(1)
    34          match = re.compile("\d+(\.\d+){1,2}").search(stdout)
    35          if match is None:
    36             print >> sys.stderr, "Could not extract version info from database connection."
    37
    38          server_version = match.group()
    39
    40          e_ver = tuple(int(v) for v in expected_version.split('.'))
    41          s_ver = tuple(int(v) for v in server_version.split('.'))
    42          if s_ver < e_ver:
    43              print >> sys.stderr, "Server version: %s < Expected version: %s" % (
    44                  server_version, expected_version)
    45
    46  if __name__=="__main__":
    47
    48      usage = "Usage: %prog [zodb|zep] [version]"
    49      epilog = "Verifies connectivity with the db server for zodb or zep configued in global.conf."
    50      parser = OptionParser(usage=usage, epilog=epilog)
    51      (options, args) = parser.parse_args()
    52
    53      if len(args) != 2:
    54          parser.print_usage()
    55          sys.exit(1)
    56
    57      component = args[0]
    58      if component not in ('zodb', 'zep'):
    59          print >> sys.stderr, "Invalid component given."
    60          parser.print_usage()
    61          sys.exit(1)
    62
    63      main = Main()
    64      main.verify(component, args[1])
    65