From 87dbc4d961d9e98f4e8b7b54010800ff3bdb5a73 Mon Sep 17 00:00:00 2001 From: Jake Hunsaker Date: Mon, 11 Nov 2019 12:43:01 -0500 Subject: [Plugin|Policy] Only call lsmod once and standardize kmod checks This commit makes two changes to how sos deals with kernel modules and their state during a run of sosreport. First, no longer call `lsmod` for every individual plugin during its enablement check. Instead, call `lsmod` only once during `Policy` initialization, and cache the output for later checks. Second, have `Plugin.is_module_loaded()` check for kmod presence in the saved policy class attr for kernel_mods, rather than checking through `/proc/modules`. Have the plugin enablement checks now also use `is_module_loaded()` to standardize with how `SoSPredicate`s are checked. Note that this change results in a significant performance increase for sos initialization times in a RHEL 7 container. Resolves: #1854 Signed-off-by: Jake Hunsaker --- sos/plugins/__init__.py | 13 +++---------- sos/policies/__init__.py | 8 +++++--- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/sos/plugins/__init__.py b/sos/plugins/__init__.py index 1a1464c1..b7a47b6a 100644 --- a/sos/plugins/__init__.py +++ b/sos/plugins/__init__.py @@ -1278,11 +1278,8 @@ class Plugin(object): chdir=runat, binary=binary, env=env) def is_module_loaded(self, module_name): - """Return whether specified moudle as module_name is loaded or not""" - if len(grep("^" + module_name + " ", "/proc/modules")) == 0: - return False - else: - return True + """Return whether specified module as module_name is loaded or not""" + return module_name in self.policy.kernel_mods # For adding output def add_alert(self, alertstring): @@ -1541,15 +1538,11 @@ class Plugin(object): return True def _check_plugin_triggers(self, files, packages, commands, services): - kernel_mods = self.policy.lsmod() - - def have_kmod(kmod): - return kmod in kernel_mods return (any(os.path.exists(fname) for fname in files) or any(self.is_installed(pkg) for pkg in packages) or any(is_executable(cmd) for cmd in commands) or - any(have_kmod(kmod) for kmod in self.kernel_mods) or + any(self.is_module_loaded(mod) for mod in self.kernel_mods) or any(self.is_service(svc) for svc in services)) def default_enabled(self): diff --git a/sos/policies/__init__.py b/sos/policies/__init__.py index a19daf22..f4aa3180 100644 --- a/sos/policies/__init__.py +++ b/sos/policies/__init__.py @@ -822,6 +822,7 @@ class LinuxPolicy(Policy): def __init__(self, sysroot=None): super(LinuxPolicy, self).__init__(sysroot=sysroot) + self.init_kernel_modules() if self.init == 'systemd': self.init_system = SystemdInit() else: @@ -874,11 +875,12 @@ class LinuxPolicy(Policy): def sanitize_filename(self, name): return re.sub(r"[^-a-z,A-Z.0-9]", "", name) - def lsmod(self): - """Return a list of kernel module names as strings. + def init_kernel_modules(self): + """Obtain a list of loaded kernel modules to reference later for plugin + enablement and SoSPredicate checks """ lines = shell_out("lsmod", timeout=0).splitlines() - return [line.split()[0].strip() for line in lines] + self.kernel_mods = [line.split()[0].strip() for line in lines] def pre_work(self): # this method will be called before the gathering begins -- cgit