Skip to content

Commit 3fb1547

Browse files
committed
common_diagnostics cleaned hostname string (ros#405)
* 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 <[email protected]> --------- Signed-off-by: Christian Henkel <[email protected]> Co-authored-by: Christian Henkel <[email protected]> (cherry picked from commit cca0f14)
1 parent 251699f commit 3fb1547

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

diagnostic_common_diagnostics/diagnostic_common_diagnostics/cpu_monitor.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ def main(args=None):
9292

9393
# Create the node
9494
hostname = socket.gethostname()
95-
node = Node(f'cpu_monitor_{hostname.replace("-", "_")}')
95+
# Every invalid symbol is replaced by underscore.
96+
# isalnum() alone also allows invalid symbols depending on the locale
97+
cleaned_hostname = ''.join(
98+
c if (c.isascii() and c.isalnum()) else '_' for c in hostname)
99+
node = Node(f'cpu_monitor_{cleaned_hostname}')
96100

97101
# Declare and get parameters
98102
node.declare_parameter('warning_percentage', 90)

diagnostic_common_diagnostics/diagnostic_common_diagnostics/hd_monitor.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ class HDMonitor(Node):
7474
"""
7575

7676
def __init__(self):
77-
hostname = gethostname().replace('.', '_').replace('-', '_')
78-
super().__init__(f'hd_monitor_{hostname}')
77+
hostname = gethostname()
78+
# Every invalid symbol is replaced by underscore.
79+
# isalnum() alone also allows invalid symbols depending on the locale
80+
cleaned_hostname = ''.join(
81+
c if (c.isascii() and c.isalnum()) else '_' for c in hostname)
82+
super().__init__(f'hd_monitor_{cleaned_hostname}')
7983

8084
self._path = '~'
8185
self._free_percent_low = 0.05

diagnostic_common_diagnostics/diagnostic_common_diagnostics/ram_monitor.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ def run(self, stat):
7272

7373
def main():
7474
hostname = socket.gethostname()
75+
# Every invalid symbol is replaced by underscore.
76+
# isalnum() alone also allows invalid symbols depending on the locale
77+
cleaned_hostname = ''.join(
78+
c if (c.isascii() and c.isalnum()) else '_' for c in hostname)
7579
rclpy.init()
76-
node = rclpy.create_node(f'ram_monitor_{hostname.replace("-", "_")}')
80+
node = rclpy.create_node(f'ram_monitor_{cleaned_hostname}')
7781

7882
updater = Updater(node)
7983
updater.setHardwareID(hostname)

diagnostic_common_diagnostics/diagnostic_common_diagnostics/sensors_monitor.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,11 @@ def monitor(self, stat):
245245
if __name__ == '__main__':
246246
rclpy.init()
247247
hostname = socket.gethostname()
248-
hostname_clean = hostname.translate(hostname.maketrans('-', '_'))
249-
node = rclpy.create_node('sensors_monitor_%s' % hostname_clean)
248+
# Every invalid symbol is replaced by underscore.
249+
# isalnum() alone also allows invalid symbols depending on the locale
250+
cleaned_hostname = ''.join(
251+
c if (c.isascii() and c.isalnum()) else '_' for c in hostname)
252+
node = rclpy.create_node('sensors_monitor_%s' % cleaned_hostname)
250253

251254
monitor = SensorsMonitor(node, hostname)
252255
try:

0 commit comments

Comments
 (0)