diff options
author | Jake Hunsaker <jhunsake@redhat.com> | 2020-02-12 13:09:37 -0500 |
---|---|---|
committer | Bryan Quigley <bryan.quigley@canonical.com> | 2020-02-12 15:10:32 -0800 |
commit | 2addff6c4f0bebfcaa515240c84ff7f37377fca6 (patch) | |
tree | ee648fe8b9545a4c7cf31f60e6dac1b9ca5202a9 | |
parent | 43d156200ba679ec72f2a591d0b3c39041a7869b (diff) | |
download | sos-2addff6c4f0bebfcaa515240c84ff7f37377fca6.tar.gz |
[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 <jhunsake@redhat.com>
Signed-off-by: Bryan Quigley <bryan.quigley@canonical.com>
-rw-r--r-- | sos/plugins/__init__.py | 4 | ||||
-rw-r--r-- | sos/policies/__init__.py | 9 |
2 files changed, 12 insertions, 1 deletions
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] |