aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMauricio Faria de Oliveira <mfo@canonical.com>2020-11-18 22:01:34 +0000
committerJake Hunsaker <jhunsake@redhat.com>2020-11-23 10:45:10 -0500
commitf104b083962a53c3c3e890be85f28a30eaccbf60 (patch)
tree66dc472a7da0967188882ce921042ef20858c4a1
parent0f20b79fe5694bb5bd022a38d6796141fe124375 (diff)
downloadsos-f104b083962a53c3c3e890be85f28a30eaccbf60.tar.gz
[Plugin,networking] Move namespace filtering to get_network_namespaces()
Copy the logic over from the networking plugin to `filter_namespaces()`, which is generic, taking any list of namespaces; and then wrap it in `get_network_namespaces()` for the particular network namespace type. Now other plugins may be able to filter namespaces as well, and not only network namespaces, but other types that may be added (which can get convenience wrappers `get_X_namespaces()` then.) In order to make the review simpler, the changes for the networking plugin are intentionally small (keep the optional `if self.g_n_n():` and only filter in `out_ns` which is used in `ethtool_namespaces`.) The next patch addresses that, with the larger indentation changes. Testing with the networking plugins' options to filter namespaces by a regex pattern, and limit by the number: ``` $ ip netns ns3 ns2 ns1 ``` Option `networking.namespace_pattern` (only namespaces with trailing 2): ``` $ sudo ./bin/sos report -o networking \ -k networking.namespace_pattern='.*2' --batch $ find sosreport-*/sos_commands/networking \ | cut -d/ -f2- | grep ip_netns_exec sos_commands/networking/ip_netns_exec_ns2_iptables-save sos_commands/networking/ip_netns_exec_ns2_ip_-s_-s_neigh_show sos_commands/networking/ip_netns_exec_ns2_ip_address_show sos_commands/networking/ip_netns_exec_ns2_ip_route_show_table_all sos_commands/networking/ip_netns_exec_ns2_ethtool_-i_lo sos_commands/networking/ip_netns_exec_ns2_ethtool_-k_lo sos_commands/networking/ip_netns_exec_ns2_ethtool_-S_lo sos_commands/networking/ip_netns_exec_ns2_ethtool_lo sos_commands/networking/ip_netns_exec_ns2_ip6tables-save sos_commands/networking/ip_netns_exec_ns2_netstat_-W_-agn sos_commands/networking/ip_netns_exec_ns2_netstat_-W_-neopa sos_commands/networking/ip_netns_exec_ns2_ip_rule_list sos_commands/networking/ip_netns_exec_ns2_netstat_-s ``` Option `networking.namespaces` (only one namespace): ``` $ sudo ./bin/sos report -o networking \ -k networking.namespaces=1 --batch $ grep 'Limiting namespace iteration' sosreport-*/sos_logs/sos.log 2020-11-18 22:48:00,263 WARNING: [plugin:networking] \ Limiting namespace iteration to first 1 namespaces found $ find sosreport-*/sos_commands/networking \ | cut -d/ -f2- | grep ip_netns_exec sos_commands/networking/ip_netns_exec_ns3_ethtool_-i_lo sos_commands/networking/ip_netns_exec_ns3_ip_rule_list sos_commands/networking/ip_netns_exec_ns3_ip_route_show_table_all sos_commands/networking/ip_netns_exec_ns3_netstat_-W_-neopa sos_commands/networking/ip_netns_exec_ns3_netstat_-s sos_commands/networking/ip_netns_exec_ns3_ethtool_-k_lo sos_commands/networking/ip_netns_exec_ns3_netstat_-W_-agn sos_commands/networking/ip_netns_exec_ns3_iptables-save sos_commands/networking/ip_netns_exec_ns3_ip_address_show sos_commands/networking/ip_netns_exec_ns3_ip_-s_-s_neigh_show sos_commands/networking/ip_netns_exec_ns3_ip6tables-save sos_commands/networking/ip_netns_exec_ns3_ethtool_-S_lo sos_commands/networking/ip_netns_exec_ns3_ethtool_lo ``` Signed-off-by: Mauricio Faria de Oliveira <mfo@canonical.com> Signed-off-by: Jake Hunsaker <jhunsake@redhat.com>
-rw-r--r--sos/report/plugins/__init__.py36
-rw-r--r--sos/report/plugins/networking.py28
2 files changed, 37 insertions, 27 deletions
diff --git a/sos/report/plugins/__init__.py b/sos/report/plugins/__init__.py
index b60113ad..deb46c93 100644
--- a/sos/report/plugins/__init__.py
+++ b/sos/report/plugins/__init__.py
@@ -2680,8 +2680,40 @@ class Plugin(object):
continue
return pids
- def get_network_namespaces(self):
- return self.commons['namespaces']['network']
+ def get_network_namespaces(self, ns_pattern=None, ns_max=0):
+ return self.filter_namespaces(self.commons['namespaces']['network'],
+ ns_pattern, ns_max)
+
+ def filter_namespaces(self, ns_list, ns_pattern=None, ns_max=0):
+ """Filter a list of namespaces by regex pattern or max number of
+ namespaces (options originally present in the networking plugin.)
+ """
+ out_ns = []
+
+ # Regex initialization outside of for loop
+ if ns_pattern:
+ pattern = (
+ '(?:%s$)' % '$|'.join(ns_pattern.split()).replace('*', '.*')
+ )
+ for ns in ns_list:
+ # if ns_pattern defined, append only namespaces
+ # matching with pattern
+ if ns_pattern:
+ if bool(re.match(pattern, ns)):
+ out_ns.append(ns)
+
+ # if ns_max is defined and ns_pattern is not defined
+ # remove from out_ns namespaces with higher index than defined
+ elif ns_max != 0:
+ out_ns.append(ns)
+ if len(out_ns) == ns_max:
+ self._log_warn("Limiting namespace iteration "
+ "to first %s namespaces found"
+ % ns_max)
+ break
+ else:
+ out_ns.append(ns)
+ return out_ns
class RedHatPlugin(object):
diff --git a/sos/report/plugins/networking.py b/sos/report/plugins/networking.py
index 7718901b..a919fa17 100644
--- a/sos/report/plugins/networking.py
+++ b/sos/report/plugins/networking.py
@@ -9,7 +9,6 @@
from sos.report.plugins import (Plugin, RedHatPlugin, UbuntuPlugin,
DebianPlugin, SoSPredicate)
from os import listdir
-from re import match
class Networking(Plugin):
@@ -233,30 +232,9 @@ class Networking(Plugin):
self.add_cmd_output("ip netns")
cmd_prefix = "ip netns exec "
if self.get_network_namespaces():
- out_ns = []
- # Regex initialization outside of for loop
- if self.get_option("namespace_pattern"):
- pattern = '(?:%s$)' % '$|'.join(
- self.get_option("namespace_pattern").split()
- ).replace('*', '.*')
- for ns in self.get_network_namespaces():
- # if namespace_pattern defined, append only namespaces
- # matching with pattern
- if self.get_option("namespace_pattern"):
- if bool(match(pattern, ns)):
- out_ns.append(ns)
-
- # if namespaces is defined and namespace_pattern is not defined
- # remove from out_ns namespaces with higher index than defined
- elif self.get_option("namespaces") != 0:
- out_ns.append(ns)
- if len(out_ns) == self.get_option("namespaces"):
- self._log_warn("Limiting namespace iteration " +
- "to first %s namespaces found"
- % self.get_option("namespaces"))
- break
- else:
- out_ns.append(ns)
+ out_ns = self.get_network_namespaces(
+ self.get_option("namespace_pattern"),
+ self.get_option("namespaces"))
for namespace in out_ns:
ns_cmd_prefix = cmd_prefix + namespace + " "