From 7b9284e948f2e9076c92741ed5b95fec7934af8d Mon Sep 17 00:00:00 2001 From: Jake Hunsaker Date: Fri, 15 Feb 2019 16:03:53 -0500 Subject: [policy|plugin] Add 'is_running' check for services Adds a method to the InitSystem class used by policies and plugins to check if a given service name is running. Plugins can make use of this through the new self.service_is_running() method. For policies that use the base InitSystem class, this method will always return True as the service_is_running() method is likely to be used when determining if we should run commands or not, and we do not want to incorrectly stop running those commands where they would collect meaningful output today. The SystemD init system for policies properly checks to see if the given service is active or not when reporting is the service is running. Resolves: #1567 Signed-off-by: Jake Hunsaker Signed-off-by: Bryn M. Reeves --- sos/plugins/__init__.py | 6 +++++- sos/policies/__init__.py | 22 ++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py index 47b028a8..030e7a30 100644 --- a/sos/plugins/__init__.py +++ b/sos/plugins/__init__.py @@ -215,9 +215,13 @@ class Plugin(object): '''Is the service $name disabled?''' return self.policy.init_system.is_disabled(name) + def service_is_running(self, name): + '''Is the service $name currently running?''' + return self.policy.init_system.is_running(name) + def get_service_status(self, name): '''Return the reported status for service $name''' - return self.policy.init_system.get_service_status(name) + return self.policy.init_system.get_service_status(name)['status'] def do_cmd_private_sub(self, cmd): '''Remove certificate and key output archived by sosreport. cmd diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py index d6255d3e..d0b18015 100644 --- a/sos/policies/__init__.py +++ b/sos/policies/__init__.py @@ -86,6 +86,17 @@ class InitSystem(object): """ return name in self.services + def is_running(self, name): + """Checks if the given service name is in a running state. + + This should be overridden by initsystems that subclass InitSystem + """ + # This is going to be primarily used in gating if service related + # commands are going to be run or not. Default to always returning + # True when an actual init system is not specified by policy so that + # we don't inadvertantly restrict sosreports on those systems + return True + def load_all_services(self): """This loads all services known to the init system into a dict. The dict should be keyed by the service name, and contain a dict of the @@ -96,10 +107,9 @@ class InitSystem(object): def _query_service(self, name): """Query an individual service""" if self.query_cmd: - res = sos_get_command_output("%s %s" % (self.query_cmd, name)) - if res['status'] == 0: - return res - else: + try: + return sos_get_command_output("%s %s" % (self.query_cmd, name)) + except Exception: return None return None @@ -156,6 +166,10 @@ class SystemdInit(InitSystem): except IndexError: pass + def is_running(self, name): + svc = self.get_service_status(name) + return svc['status'] == 'active' + class PackageManager(object): """Encapsulates a package manager. If you provide a query_command to the -- cgit