From 20212ba8591c017d915f8e905d85b20bda4e3402 Mon Sep 17 00:00:00 2001 From: Gorka Eguileor Date: Wed, 19 Jun 2024 15:58:28 +0200 Subject: [PATCH] [container_log] Add pods support In Kubernetes/OpenShift rotated logs have no symbolic link in /var/log/containers and they cannot be retrieved using `oc logs` either, so there is no way to get these rotated logs in a SOS report. This patch proposes extending the `container_log` to make it also capable of retrieving logs from `var/log/pods` using a plugin boolean option called `rotated`. When retrieving the pods directory, all logs will be retrieved from it regardless of size, as it is implicit that the intent is to get all rotated logs. This patch also adds the `maxage` option to the `container_log` plugin, allowing the retrieval of logs that are not older than `maxage` hours. This affects bot the normal containers log gathering as well as the pods gathering. Closes #3677 Signed-off-by: Gorka Eguileor --- sos/report/plugins/container_log.py | 35 ++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/sos/report/plugins/container_log.py b/sos/report/plugins/container_log.py index 98d9637ace..3c7497e005 100644 --- a/sos/report/plugins/container_log.py +++ b/sos/report/plugins/container_log.py @@ -9,7 +9,7 @@ # See the LICENSE file in the source distribution for further information. import os -from sos.report.plugins import Plugin, IndependentPlugin +from sos.report.plugins import Plugin, IndependentPlugin, PluginOpt class ContainerLog(Plugin, IndependentPlugin): @@ -17,18 +17,43 @@ class ContainerLog(Plugin, IndependentPlugin): short_desc = 'All logs under /var/log/containers' plugin_name = 'container_log' logdir = '/var/log/containers/' + poddir = '/var/log/pods/' files = (logdir, ) + option_list = [ + PluginOpt('rotated', default=False, val_type=bool, + desc='get all logs from /var/log/pods regardless of size, ' + 'which will include all rotated logs'), + PluginOpt('maxage', default=None, val_type=int, + desc='gather only logs with `mtime` not older than this many' + ' hours') + ] + def setup(self): + maxage = self.get_option('maxage') + sizelimit = None + # Remove size limit from containers logs when getting rotated, since + # they are just symlinks to the pods logs + if self.get_option('rotated'): + # This should not happen, but better safe than sorry + if not os.path.exists(self.poddir): + self._log_warn(f"could not find {self.poddir}, skipping it") + else: + sizelimit = 0 + self.add_copy_spec(self.poddir, sizelimit=sizelimit, + maxage=maxage) + if self.get_option('all_logs'): - self.add_copy_spec(self.logdir) + self.add_copy_spec(self.logdir, maxage=maxage) else: - self.collect_subdirs() + self.collect_subdirs(sizelimit=sizelimit, maxage=maxage) - def collect_subdirs(self, root=logdir): + def collect_subdirs(self, root=logdir, sizelimit=None, maxage=None): """Collect *.log files from subdirs of passed root path """ for dir_name, _, _ in os.walk(root): - self.add_copy_spec(self.path_join(dir_name, '*.log')) + self.add_copy_spec(self.path_join(dir_name, '*.log'), + sizelimit=sizelimit, + maxage=maxage) # vim: set et ts=4 sw=4 :