Skip to content

Commit

Permalink
common_diagnostics cleaned hostname string (#405) (#421)
Browse files Browse the repository at this point in the history
* 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)

Co-authored-by: sjusner <[email protected]>
  • Loading branch information
ct2034 and sjusner authored Dec 18, 2024
1 parent a16b54b commit 471af15
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 471af15

Please sign in to comment.