From 471af157fdc1d2a6c55279d2ccb52fcd43ef2566 Mon Sep 17 00:00:00 2001 From: Christian Henkel <6976069+ct2034@users.noreply.github.com> Date: Wed, 18 Dec 2024 13:51:27 +0100 Subject: [PATCH] common_diagnostics cleaned hostname string (#405) (#421) * Hostnames are properly cleaned to only contain alphanumeric characters or underscore. * changed string sanitation (symbols must be ascii and alphanumeric) * Changes now conforming to linting rules * flake8 fixes Signed-off-by: Christian Henkel --------- Signed-off-by: Christian Henkel Co-authored-by: Christian Henkel (cherry picked from commit cca0f14917870cb6902132071f66502600db2229) Co-authored-by: sjusner --- .../diagnostic_common_diagnostics/cpu_monitor.py | 6 +++++- .../diagnostic_common_diagnostics/hd_monitor.py | 8 ++++++-- .../diagnostic_common_diagnostics/ram_monitor.py | 6 +++++- .../diagnostic_common_diagnostics/sensors_monitor.py | 7 +++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/diagnostic_common_diagnostics/diagnostic_common_diagnostics/cpu_monitor.py b/diagnostic_common_diagnostics/diagnostic_common_diagnostics/cpu_monitor.py index 86662957..32dd60eb 100755 --- a/diagnostic_common_diagnostics/diagnostic_common_diagnostics/cpu_monitor.py +++ b/diagnostic_common_diagnostics/diagnostic_common_diagnostics/cpu_monitor.py @@ -92,7 +92,11 @@ def main(args=None): # Create the node hostname = socket.gethostname() - node = Node(f'cpu_monitor_{hostname.replace("-", "_")}') + # Every invalid symbol is replaced by underscore. + # isalnum() alone also allows invalid symbols depending on the locale + cleaned_hostname = ''.join( + c if (c.isascii() and c.isalnum()) else '_' for c in hostname) + node = Node(f'cpu_monitor_{cleaned_hostname}') # Declare and get parameters node.declare_parameter('warning_percentage', 90) diff --git a/diagnostic_common_diagnostics/diagnostic_common_diagnostics/hd_monitor.py b/diagnostic_common_diagnostics/diagnostic_common_diagnostics/hd_monitor.py index ac4eae74..3b577f1c 100755 --- a/diagnostic_common_diagnostics/diagnostic_common_diagnostics/hd_monitor.py +++ b/diagnostic_common_diagnostics/diagnostic_common_diagnostics/hd_monitor.py @@ -74,8 +74,12 @@ class HDMonitor(Node): """ def __init__(self): - hostname = gethostname().replace('.', '_').replace('-', '_') - super().__init__(f'hd_monitor_{hostname}') + hostname = gethostname() + # Every invalid symbol is replaced by underscore. + # isalnum() alone also allows invalid symbols depending on the locale + cleaned_hostname = ''.join( + c if (c.isascii() and c.isalnum()) else '_' for c in hostname) + super().__init__(f'hd_monitor_{cleaned_hostname}') self._path = '~' self._free_percent_low = 0.05 diff --git a/diagnostic_common_diagnostics/diagnostic_common_diagnostics/ram_monitor.py b/diagnostic_common_diagnostics/diagnostic_common_diagnostics/ram_monitor.py index d6250b6b..da59a6d2 100755 --- a/diagnostic_common_diagnostics/diagnostic_common_diagnostics/ram_monitor.py +++ b/diagnostic_common_diagnostics/diagnostic_common_diagnostics/ram_monitor.py @@ -72,8 +72,12 @@ def run(self, stat): def main(): hostname = socket.gethostname() + # Every invalid symbol is replaced by underscore. + # isalnum() alone also allows invalid symbols depending on the locale + cleaned_hostname = ''.join( + c if (c.isascii() and c.isalnum()) else '_' for c in hostname) rclpy.init() - node = rclpy.create_node(f'ram_monitor_{hostname.replace("-", "_")}') + node = rclpy.create_node(f'ram_monitor_{cleaned_hostname}') updater = Updater(node) updater.setHardwareID(hostname) diff --git a/diagnostic_common_diagnostics/diagnostic_common_diagnostics/sensors_monitor.py b/diagnostic_common_diagnostics/diagnostic_common_diagnostics/sensors_monitor.py index c6733e11..8bcd1aa9 100755 --- a/diagnostic_common_diagnostics/diagnostic_common_diagnostics/sensors_monitor.py +++ b/diagnostic_common_diagnostics/diagnostic_common_diagnostics/sensors_monitor.py @@ -245,8 +245,11 @@ def monitor(self, stat): if __name__ == '__main__': rclpy.init() hostname = socket.gethostname() - hostname_clean = hostname.translate(hostname.maketrans('-', '_')) - node = rclpy.create_node('sensors_monitor_%s' % hostname_clean) + # Every invalid symbol is replaced by underscore. + # isalnum() alone also allows invalid symbols depending on the locale + cleaned_hostname = ''.join( + c if (c.isascii() and c.isalnum()) else '_' for c in hostname) + node = rclpy.create_node('sensors_monitor_%s' % cleaned_hostname) monitor = SensorsMonitor(node, hostname) try: