[sos-devel] [PATCH 1/2] Allow commands to discard stderr

John Haxby john.haxby at oracle.com
Mon Jan 19 17:08:42 UTC 2015


Just add "keep_stderr=False" to add_cmd_output() if you don't want the
command's stderr in the sosreport.

Signed-off-by: John Haxby <john.haxby at oracle.com>
---
 sos/plugins/__init__.py | 29 +++++++++++++++++------------
 sos/utilities.py        |  5 +++--
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py
index 4fd85be..12546a4 100644
--- a/sos/plugins/__init__.py
+++ b/sos/plugins/__init__.py
@@ -460,8 +460,9 @@ class Plugin(object):
             self.copy_paths.update(copy_paths)
             self._log_info("added copyspec '%s'" % copyspec)
 
-    def get_command_output(self, prog, timeout=300, runat=None):
-        result = sos_get_command_output(prog, timeout=timeout, runat=runat)
+    def get_command_output(self, prog, timeout=300, runat=None, keep_stderr=True):
+        result = sos_get_command_output(prog, timeout=timeout, runat=runat,
+                                        keep_stderr=keep_stderr)
         if result['status'] == 124:
             self._log_warn("command '%s' timed out after %ds"
                            % (prog, timeout))
@@ -470,11 +471,12 @@ class Plugin(object):
             self._log_debug("could not run '%s': command not found" % prog)
         return result
 
-    def call_ext_prog(self, prog, timeout=300, runat=None):
+    def call_ext_prog(self, prog, timeout=300, runat=None, keep_stderr=True):
         """Execute a command independantly of the output gathering part of
         sosreport.
         """
-        return self.get_command_output(prog, timeout=timeout, runat=runat)
+        return self.get_command_output(prog, timeout=timeout, runat=runat,
+                                       keep_stderr=True)
 
     def check_ext_prog(self, prog):
         """Execute a command independently of the output gathering part of
@@ -484,15 +486,16 @@ class Plugin(object):
         return self.call_ext_prog(prog)['status'] == 0
 
     def add_cmd_output(self, cmds, suggest_filename=None,
-                       root_symlink=None, timeout=300, runat=None):
+                       root_symlink=None, timeout=300, runat=None,
+                       keep_stderr=True):
         """Run a program or a list of programs and collect the output"""
         if isinstance(cmds, six.string_types):
             cmds = [cmds]
         if len(cmds) > 1 and (suggest_filename or root_symlink):
             self._log_warn("ambiguous filename or symlink for command list")
         for cmd in cmds:
-            cmdt = (cmd, suggest_filename, root_symlink, timeout, runat)
-            _logstr = "packed command tuple: ('%s', '%s', '%s', %s, '%s')"
+            cmdt = (cmd, suggest_filename, root_symlink, timeout, runat, keep_stderr)
+            _logstr = "packed command tuple: ('%s', '%s', '%s', %s, '%s', '%s')"
             self._log_debug(_logstr % cmdt)
             self.collect_cmds.append(cmdt)
             self._log_info("added cmd output '%s'" % cmd)
@@ -547,12 +550,13 @@ class Plugin(object):
 
     def get_cmd_output_now(self, exe, suggest_filename=None,
                            root_symlink=False, timeout=300,
-                           runat=None):
+                           runat=None, keep_stderr=True):
         """Execute a command and save the output to a file for inclusion in the
         report.
         """
         start = time()
-        result = self.get_command_output(exe, timeout=timeout, runat=runat)
+        result = self.get_command_output(exe, timeout=timeout, runat=runat,
+                                         keep_stderr=keep_stderr)
         # 126 means 'found but not executable'
         if result['status'] == 126 or result['status'] == 127:
             return None
@@ -601,13 +605,14 @@ class Plugin(object):
 
     def _collect_cmd_output(self):
         for progs in zip(self.collect_cmds):
-            prog, suggest_filename, root_symlink, timeout, runat = progs[0]
+            prog, suggest_filename, root_symlink, timeout, runat, keep_stderr = progs[0]
             self._log_debug("unpacked command tuple: "
-                            + "('%s', '%s', '%s', %s, '%s')" % progs[0])
+                            + "('%s', '%s', '%s', %s, '%s', %s)" % progs[0])
             self._log_info("collecting output of '%s'" % prog)
             self.get_cmd_output_now(prog, suggest_filename=suggest_filename,
                                     root_symlink=root_symlink,
-                                    timeout=timeout, runat=runat)
+                                    timeout=timeout, runat=runat,
+                                    keep_stderr=keep_stderr)
 
     def _collect_strings(self):
         for string, file_name in self.copy_strings:
diff --git a/sos/utilities.py b/sos/utilities.py
index 51909c6..639bc68 100644
--- a/sos/utilities.py
+++ b/sos/utilities.py
@@ -120,7 +120,7 @@ def is_executable(command):
     return any(os.access(path, os.X_OK) for path in candidates)
 
 
-def sos_get_command_output(command, timeout=300, runat=None):
+def sos_get_command_output(command, timeout=300, runat=None, keep_stderr=True):
     """Execute a command through the system shell. First checks to see if the
     requested command is executable. Returns (returncode, stdout, 0)"""
     def _child_chdir():
@@ -142,7 +142,8 @@ def sos_get_command_output(command, timeout=300, runat=None):
         command = command.encode('utf-8', 'ignore')
     args = shlex.split(command)
     try:
-        p = Popen(args, shell=False, stdout=PIPE, stderr=STDOUT,
+        p = Popen(args, shell=False, stdout=PIPE,
+                  stderr=STDOUT if keep_stderr else PIPE,
                   bufsize=-1, env=cmd_env, close_fds=True,
                   preexec_fn=_child_chdir)
     except OSError as e:
-- 
1.8.3.1




More information about the sos-devel mailing list