]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm:Add listening ports to gather-facts output
authorPaul Cuzner <pcuzner@redhat.com>
Wed, 18 Aug 2021 05:02:32 +0000 (17:02 +1200)
committerSebastian Wagner <sewagner@redhat.com>
Thu, 2 Sep 2021 14:24:40 +0000 (16:24 +0200)
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 <pcuzner@redhat.com>
(cherry picked from commit 0c4e88993bc04d248b1616ba4d46ebe24bf17381)

src/cephadm/cephadm

index 832879d4c8a55256ce393932dd2f6029a8796228..857945ec85e356dd792d34c1c0ad198dc32956e3 100755 (executable)
@@ -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 <port>)
+        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"""