From: Paul Cuzner Date: Wed, 18 Aug 2021 05:02:32 +0000 (+1200) Subject: cephadm:Add listening ports to gather-facts output X-Git-Tag: v16.2.6~25^2~5 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d875d99fd39debe18eafbff1a222a81672a1e2af;p=ceph.git cephadm:Add listening ports to gather-facts output This patch adds tcp and udp listening ports to the data returned by gather-facts. This can be used to check port availability prior to trying to deploying daemons, to catch port conflicts earlier. IPv4 and IPv6 are supported Fixes: #52038 Signed-off-by: Paul Cuzner (cherry picked from commit 0c4e88993bc04d248b1616ba4d46ebe24bf17381) --- diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index 832879d4c8a5..857945ec85e3 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -6940,6 +6940,48 @@ class HostFacts(): return k_param + @staticmethod + def _process_net_data(tcp_file: str, protocol: str = 'tcp') -> List[int]: + listening_ports = [] + # Connections state documentation + # tcp - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/tcp_states.h + # udp - uses 07 (TCP_CLOSE or UNCONN, since udp is stateless. test with netcat -ul ) + listening_state = { + 'tcp': '0A', + 'udp': '07' + } + + if protocol not in listening_state.keys(): + return [] + + if os.path.exists(tcp_file): + with open(tcp_file) as f: + tcp_data = f.readlines()[1:] + + for con in tcp_data: + con_info = con.strip().split() + if con_info[3] == listening_state[protocol]: + local_port = int(con_info[1].split(':')[1], 16) + listening_ports.append(local_port) + + return listening_ports + + @property + def tcp_ports_used(self) -> List[int]: + return HostFacts._process_net_data('/proc/net/tcp') + + @property + def tcp6_ports_used(self) -> List[int]: + return HostFacts._process_net_data('/proc/net/tcp6') + + @property + def udp_ports_used(self) -> List[int]: + return HostFacts._process_net_data('/proc/net/udp', 'udp') + + @property + def udp6_ports_used(self) -> List[int]: + return HostFacts._process_net_data('/proc/net/udp6', 'udp') + def dump(self): # type: () -> str """Return the attributes of this HostFacts object as json"""