From 2addff6c4f0bebfcaa515240c84ff7f37377fca6 Mon Sep 17 00:00:00 2001 From: Jake Hunsaker Date: Wed, 12 Feb 2020 13:09:37 -0500 Subject: [InitSystem|Plugin] Add method to get service names matching regex Adds a new `get_service_names()` method to the `InitSystem` class which is then exposed to plugins via `Plugin.get_service_names()` to allow plugins to fetch a list of service names discovered on the system that match a provided regex. Included in this is a fix to avoid the header line from the discovery command being entered in the dict of known services, so that we avoid inadvertant regex matches. Resolves: #1943 Signed-off-by: Jake Hunsaker Signed-off-by: Bryan Quigley --- sos/plugins/__init__.py | 4 ++++ sos/policies/__init__.py | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py index 541e8822..cc284d06 100644 --- a/sos/plugins/__init__.py +++ b/sos/plugins/__init__.py @@ -517,6 +517,10 @@ class Plugin(object): """Return the reported status for service $name""" return self.policy.init_system.get_service_status(name)['status'] + def get_service_names(self, regex): + """Get all service names matching regex""" + return self.policy.init_system.get_service_names(regex) + def set_predicate(self, pred): """Set or clear the default predicate for this plugin. """ diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py index f1a88b90..c12fc8c3 100644 --- a/sos/policies/__init__.py +++ b/sos/policies/__init__.py @@ -129,6 +129,13 @@ class InitSystem(object): """ return output + def get_service_names(self, regex): + """Get a list of all services discovered on the system that match the + given regex. + """ + reg = re.compile(regex, re.I) + return [s for s in self.services.keys() if reg.match(s)] + def get_service_status(self, name): """Returns the status for the given service name along with the output of the query command @@ -168,7 +175,7 @@ class SystemdInit(InitSystem): return 'unknown' def load_all_services(self): - svcs = shell_out(self.list_cmd).splitlines() + svcs = shell_out(self.list_cmd).splitlines()[1:] for line in svcs: try: name = line.split('.service')[0] -- cgit