| From fa15f163c340e73b53cd703bb199d15808f7bec8 Mon Sep 17 00:00:00 2001 |
| From: Sam Kunz <samkunz@google.com> |
| Date: Tue, 28 Sep 2021 18:33:01 +0000 |
| Subject: [PATCH] [plugins] Add crictl plugin. |
| |
| --- |
| sos/report/plugins/crictl.py | 84 ++++++++++++++++++++++++++++++++++++ |
| 1 file changed, 84 insertions(+) |
| create mode 100644 sos/report/plugins/crictl.py |
| |
| diff --git a/sos/report/plugins/crictl.py b/sos/report/plugins/crictl.py |
| new file mode 100644 |
| index 00000000..48367a73 |
| --- /dev/null |
| +++ b/sos/report/plugins/crictl.py |
| @@ -0,0 +1,84 @@ |
| +# This file is part of the sos project: https://github.com/sosreport/sos |
| +# |
| +# This copyrighted material is made available to anyone wishing to use, |
| +# modify, copy, or redistribute it subject to the terms and conditions of |
| +# version 2 of the GNU General Public License. |
| +# |
| +# See the LICENSE file in the source distribution for further information. |
| + |
| +from sos.report.plugins import Plugin, CosPlugin, SoSPredicate |
| + |
| + |
| +class Crictl(Plugin, CosPlugin): |
| + |
| + short_desc = 'crictl containers' |
| + plugin_name = 'crictl' |
| + profiles = ('container',) |
| + packages = ('cri-tools') |
| + |
| + option_list = [ |
| + ("all", "enable capture for all containers, even containers " |
| + "that have terminated", 'fast', False), |
| + ("logs", "capture logs for running containers", |
| + 'fast', False), |
| + ] |
| + |
| + def setup(self): |
| + self.add_copy_spec([ |
| + "/etc/crictl.yaml", |
| + ]) |
| + |
| + self.add_journal(units='containerd') |
| + |
| + # cri-tools supplies the crictl utility |
| + self.set_cmd_predicate(SoSPredicate(self, packages=['cri-tools'])) |
| + |
| + subcmds = [ |
| + 'info', |
| + 'images', |
| + 'pods', |
| + 'pods -v', |
| + 'ps', |
| + 'ps -a', |
| + 'ps -v', |
| + 'stats', |
| + 'version', |
| + ] |
| + |
| + self.add_cmd_output(["crictl %s" % s for s in subcmds]) |
| + |
| + ps_cmd = 'crictl ps --quiet' |
| + if self.get_option('all'): |
| + ps_cmd = "%s -a" % ps_cmd |
| + |
| + img_cmd = 'crictl images --quiet' |
| + pod_cmd = 'crictl pods --quiet' |
| + |
| + containers = self._get_crictl_list(ps_cmd) |
| + images = self._get_crictl_list(img_cmd) |
| + pods = self._get_crictl_list(pod_cmd) |
| + |
| + for container in containers: |
| + self.add_cmd_output("crictl inspect %s" % container) |
| + if self.get_option('logs'): |
| + self.add_cmd_output("crictl logs -t %s" % container, |
| + subdir="containers") |
| + |
| + for image in images: |
| + self.add_cmd_output("crictl inspecti %s" % image, subdir="images") |
| + |
| + for pod in pods: |
| + self.add_cmd_output("crictl inspectp %s" % pod, subdir="pods") |
| + |
| + def _get_crictl_list(self, cmd): |
| + ret = [] |
| + result = self.exec_cmd(cmd) |
| + if result['status'] == 0: |
| + for ent in result['output'].splitlines(): |
| + ret.append(ent) |
| + # Prevent the socket deprecation warning from being iterated over |
| + if ret and 'deprecated' in ret[0]: |
| + ret.pop(0) |
| + return ret |
| + |
| +# vim: set et ts=4 sw=4 : |
| -- |
| 2.31.0 |
| |