Skip to content

Commit

Permalink
[container_log] Add maxage option
Browse files Browse the repository at this point in the history
This patch 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.

Signed-off-by: Gorka Eguileor <[email protected]>
  • Loading branch information
Akrog committed Jun 20, 2024
1 parent f070eef commit 213b056
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions sos/report/plugins/container_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ class ContainerLog(Plugin, IndependentPlugin):
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
Expand All @@ -36,18 +40,20 @@ def setup(self):
self._log_warn(f"could not find {self.poddir}, skipping it")
else:
sizelimit = 0
self.add_copy_spec(self.poddir, sizelimit=sizelimit)
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(sizelimit=sizelimit)
self.collect_subdirs(sizelimit=sizelimit, maxage=maxage)

def collect_subdirs(self, root=logdir, sizelimit=None):
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'),
sizelimit=sizelimit)
sizelimit=sizelimit,
maxage=maxage)

# vim: set et ts=4 sw=4 :

0 comments on commit 213b056

Please sign in to comment.