commit 56241de85f8d48f20ed208b4e1bded594823513c Author: Bryn M. Reeves Date: Wed Jan 9 14:41:57 2013 +0000 Fix parameter handling and pg_dump collection in psql Re-work the parameter usage and pg_dump collection in the PostgreSQL plug-in so that it works correctly on systems without postgres even when the user specifies -o psql with or without -a. The pgdump tarballs are no longer created in tmp and collected using addCopySpec(); it's better to just send them straight into the sos_commands path in the first place (avoids the need for a postproc() method to do cleanup). Resolves: bz852049 diff --git a/sos/plugins/psql.py b/sos/plugins/psql.py index c9f337c..6143277 100644 --- a/sos/plugins/psql.py +++ b/sos/plugins/psql.py @@ -32,10 +32,8 @@ class psql(sos.plugintools.PluginBase): packages = [ 'postgresql' ] - tmp_dir = None - optionList = [ - ("pghome", 'PostgreSQL server home directory (default=/var/lib/pgsql)', '', False), + ("pghome", 'PostgreSQL server home directory (default=/var/lib/pgsql)', '', __pghome), ("username", 'username for pg_dump (default=postgres)', '', False), ("password", 'password for pg_dump (default=None)', '', False), ("dbname", 'database name to dump for pg_dump (default=None)', '', False), @@ -44,7 +42,12 @@ class psql(sos.plugintools.PluginBase): ] def pg_dump(self): - dest_file = os.path.join(self.tmp_dir, "sos_pgdump.tar") + dest_dir = os.path.join(self.cInfo['cmddir'], "psql") + dest_file = os.path.join(dest_dir, "sos_pgdump.tar") + try: + os.makedirs(dest_dir) + except: + self.soslog.error("could not create pg_dump output path %s" % dest_dir) old_env_pgpassword = os.environ.get("PGPASSWORD") os.environ["PGPASSWORD"] = "%s" % (self.getOption("password")) if self.getOption("dbhost"): @@ -62,42 +65,42 @@ class psql(sos.plugintools.PluginBase): if old_env_pgpassword is not None: os.environ["PGPASSWORD"] = str(old_env_pgpassword) - if (status == 0): - self.addCopySpec(dest_file) - else: - self.addAlert("ERROR: Unable to execute pg_dump. Error(%s)" % (output)) + if status != 0: + self.soslog.error("unable to execute pg_dump. Error(%s)" % (output)) def setup(self): if self.getOption("pghome"): - self.__pghome = self.getOption("pghome") + self.__pghome = str(self.getOption("pghome")).strip() + + if os.path.isdir(self.__pghome): + # Copy PostgreSQL log files. + for file in find("*.log", self.__pghome): + self.addCopySpec(file) + # Copy PostgreSQL config files. + for file in find("*.conf", self.__pghome): + self.addCopySpec(file) + self.addCopySpec(os.path.join(self.__pghome, + "data" , "PG_VERSION")) + self.addCopySpec(os.path.join(self.__pghome, + "data" , "postmaster.opts")) if self.getOption("dbname"): - if self.getOption("password"): + if self.getOption("dbname") == True: + # dbname must have a value + self.soslog.warn("pgsql.dbname requires a database name") + return + if self.getOption("password") != False: if self.getOption("username"): + if self.getOption("username") == True: + self.soslog.warn("pgsql.username requires a user name") + return self.__username = self.getOption("username") if self.getOption("dbport"): + if self.getOption("dbport") == True: + self.soslog.warn("pgsql.dbport requires a port value") + return self.__dbport = self.getOption("dbport") - self.tmp_dir = tempfile.mkdtemp() self.pg_dump() else: - self.addAlert("WARN: password must be supplied to dump a database.") - - # Copy PostgreSQL log files. - for file in find("*.log", self.__pghome): - self.addCopySpec(file) - # Copy PostgreSQL config files. - for file in find("*.conf", self.__pghome): - self.addCopySpec(file) + self.soslog.warn("password must be supplied to dump a database.") - self.addCopySpec(os.path.join(self.__pghome, "data" , "PG_VERSION")) - self.addCopySpec(os.path.join(self.__pghome, "data" , "postmaster.opts")) - - - def postproc(self): - import shutil - if self.tmp_dir == None: - return - try: - shutil.rmtree(self.tmp_dir) - except: - self.addAlert("ERROR: Unable to remove %s." % (self.tmp_dir)) commit 04f3b2f6068e3518dccf00a5d98ae07784fb3c88 Author: Bryn M. Reeves Date: Wed Jan 9 14:45:04 2013 +0000 Rename psql as pgsql in rhel-6 Rename the PostgreSQL module to "pgsql" in the rhel-6 branch as suggested in bugzilla. Resolves: bz852049 diff --git a/sos/plugins/pgsql.py b/sos/plugins/pgsql.py new file mode 100644 index 0000000..ca7972e --- /dev/null +++ b/sos/plugins/pgsql.py @@ -0,0 +1,106 @@ +import fnmatch +import os +import sos.plugintools +import tempfile + +def find(file_pattern, top_dir, max_depth=None, path_pattern=None): + """generate function to find files recursively. Usage: + + for filename in find("*.properties", /var/log/foobar): + print filename + """ + if max_depth: + base_depth = os.path.dirname(top_dir).count(os.path.sep) + max_depth += base_depth + + for path, dirlist, filelist in os.walk(top_dir): + if max_depth and path.count(os.path.sep) >= max_depth: + del dirlist[:] + + if path_pattern and not fnmatch.fnmatch(path, path_pattern): + continue + + for name in fnmatch.filter(filelist, file_pattern): + yield os.path.join(path,name) + +# Class name must be the same as file name and method names must not change +class pgsql(sos.plugintools.PluginBase): + """PostgreSQL related information""" + __pghome = '/var/lib/pgsql' + __username = 'postgres' + __dbport = 5432 + + packages = [ 'postgresql' ] + + optionList = [ + ("pghome", 'PostgreSQL server home directory (default=/var/lib/pgsql)', '', __pghome), + ("username", 'username for pg_dump (default=postgres)', '', False), + ("password", 'password for pg_dump (default=None)', '', False), + ("dbname", 'database name to dump for pg_dump (default=None)', '', False), + ("dbhost", 'hostname/IP of the server upon which the DB is running (default=localhost)', '', False), + ("dbport", 'database server port number (default=5432)', '', False) + ] + + def pg_dump(self): + dest_dir = os.path.join(self.cInfo['cmddir'], "pgsql") + dest_file = os.path.join(dest_dir, "sos_pgdump.tar") + try: + os.makedirs(dest_dir) + except: + self.soslog.error("could not create pg_dump output path %s" % dest_dir) + old_env_pgpassword = os.environ.get("PGPASSWORD") + os.environ["PGPASSWORD"] = "%s" % (self.getOption("password")) + if self.getOption("dbhost"): + (status, output, rtime) = self.callExtProg("pg_dump -U %s -h %s -p %s -w -f %s -F t %s" % + (self.__username, + self.getOption("dbhost"), + self.__dbport, + dest_file, + self.getOption("dbname"))) + else: + (status, output, rtime) = self.callExtProg("pg_dump -C -U %s -w -f %s -F t %s " % + (self.__username, + dest_file, + self.getOption("dbname"))) + + if old_env_pgpassword is not None: + os.environ["PGPASSWORD"] = str(old_env_pgpassword) + if status != 0: + self.soslog.error("unable to execute pg_dump. Error(%s)" % (output)) + + def setup(self): + if self.getOption("pghome"): + self.__pghome = str(self.getOption("pghome")).strip() + + if os.path.isdir(self.__pghome): + # Copy PostgreSQL log files. + for file in find("*.log", self.__pghome): + self.addCopySpec(file) + # Copy PostgreSQL config files. + for file in find("*.conf", self.__pghome): + self.addCopySpec(file) + self.addCopySpec(os.path.join(self.__pghome, + "data" , "PG_VERSION")) + self.addCopySpec(os.path.join(self.__pghome, + "data" , "postmaster.opts")) + + if self.getOption("dbname"): + if self.getOption("dbname") == True: + # dbname must have a value + self.soslog.warn("pgsql.dbname requires a database name") + return + if self.getOption("password") != False: + if self.getOption("username"): + if self.getOption("username") == True: + self.soslog.warn("pgsql.username requires a user name") + return + self.__username = self.getOption("username") + if self.getOption("dbport"): + if self.getOption("dbport") == True: + self.soslog.warn("pgsql.dbport requires a port value") + return + self.__dbport = self.getOption("dbport") + self.pg_dump() + else: + self.soslog.warn("password must be supplied to dump a database.") + diff --git a/sos/plugins/psql.py b/sos/plugins/psql.py deleted file mode 100644 index 6143277..0000000 --- a/sos/plugins/psql.py +++ /dev/null @@ -1,106 +0,0 @@ -import fnmatch -import os -import sos.plugintools -import tempfile - -def find(file_pattern, top_dir, max_depth=None, path_pattern=None): - """generate function to find files recursively. Usage: - - for filename in find("*.properties", /var/log/foobar): - print filename - """ - if max_depth: - base_depth = os.path.dirname(top_dir).count(os.path.sep) - max_depth += base_depth - - for path, dirlist, filelist in os.walk(top_dir): - if max_depth and path.count(os.path.sep) >= max_depth: - del dirlist[:] - - if path_pattern and not fnmatch.fnmatch(path, path_pattern): - continue - - for name in fnmatch.filter(filelist, file_pattern): - yield os.path.join(path,name) - -# Class name must be the same as file name and method names must not change -class psql(sos.plugintools.PluginBase): - """PostgreSQL related information""" - __pghome = '/var/lib/pgsql' - __username = 'postgres' - __dbport = 5432 - - packages = [ 'postgresql' ] - - optionList = [ - ("pghome", 'PostgreSQL server home directory (default=/var/lib/pgsql)', '', __pghome), - ("username", 'username for pg_dump (default=postgres)', '', False), - ("password", 'password for pg_dump (default=None)', '', False), - ("dbname", 'database name to dump for pg_dump (default=None)', '', False), - ("dbhost", 'hostname/IP of the server upon which the DB is running (default=localhost)', '', False), - ("dbport", 'database server port number (default=5432)', '', False) - ] - - def pg_dump(self): - dest_dir = os.path.join(self.cInfo['cmddir'], "psql") - dest_file = os.path.join(dest_dir, "sos_pgdump.tar") - try: - os.makedirs(dest_dir) - except: - self.soslog.error("could not create pg_dump output path %s" % dest_dir) - old_env_pgpassword = os.environ.get("PGPASSWORD") - os.environ["PGPASSWORD"] = "%s" % (self.getOption("password")) - if self.getOption("dbhost"): - (status, output, rtime) = self.callExtProg("pg_dump -U %s -h %s -p %s -w -f %s -F t %s" % - (self.__username, - self.getOption("dbhost"), - self.__dbport, - dest_file, - self.getOption("dbname"))) - else: - (status, output, rtime) = self.callExtProg("pg_dump -C -U %s -w -f %s -F t %s " % - (self.__username, - dest_file, - self.getOption("dbname"))) - - if old_env_pgpassword is not None: - os.environ["PGPASSWORD"] = str(old_env_pgpassword) - if status != 0: - self.soslog.error("unable to execute pg_dump. Error(%s)" % (output)) - - def setup(self): - if self.getOption("pghome"): - self.__pghome = str(self.getOption("pghome")).strip() - - if os.path.isdir(self.__pghome): - # Copy PostgreSQL log files. - for file in find("*.log", self.__pghome): - self.addCopySpec(file) - # Copy PostgreSQL config files. - for file in find("*.conf", self.__pghome): - self.addCopySpec(file) - self.addCopySpec(os.path.join(self.__pghome, - "data" , "PG_VERSION")) - self.addCopySpec(os.path.join(self.__pghome, - "data" , "postmaster.opts")) - - if self.getOption("dbname"): - if self.getOption("dbname") == True: - # dbname must have a value - self.soslog.warn("pgsql.dbname requires a database name") - return - if self.getOption("password") != False: - if self.getOption("username"): - if self.getOption("username") == True: - self.soslog.warn("pgsql.username requires a user name") - return - self.__username = self.getOption("username") - if self.getOption("dbport"): - if self.getOption("dbport") == True: - self.soslog.warn("pgsql.dbport requires a port value") - return - self.__dbport = self.getOption("dbport") - self.pg_dump() - else: - self.soslog.warn("password must be supplied to dump a database.") -