Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/ohai/plugins/linux/hostnamectl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
if hostnamectl_path
shell_out(hostnamectl_path).stdout.split("\n").each do |line|
key, val = line.split(": ", 2)

# strip visual icons produced by newer versions of systemd
val.gsub!(/ [^[:ascii]]*$/, "")
Copy link
Collaborator

@jaymzh jaymzh Dec 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like where you're going with this but non-ascii isn't quite what you want, which is why you're having to add anchors. This will grab all unicode+modifiers and squeeze the spaces:

irb(main):025> re = /\p{So}\p{Mn}*/u
=> /\p{So}\p{Mn}*/
irb(main):031> vals = ["Chassis: desktop 🖥️", "Chassis: 🖥️ desktop", "🖥️ Chassis: desktop"]
irb(main):033> vals.map { |val| val.gsub(re, "").squeeze(" ").strip }
=> ["Chassis: desktop", "Chassis: desktop", "Chassis: desktop"]

This handles all sort of cases they can throw at us without any unnecessary removals.

Suggested change
val.gsub!(/ [^[:ascii]]*$/, "")
val.gsub!(/\p{So}\p{Mn}*/u, "").squeeze(" ").strip

Ideally we would also add a unittest for this with those test cases....there's already a ton of hostname tests in spec/unit/plugins/hostname_spec.rb you can use as a template.


hostnamectl[key.chomp.lstrip.tr(" ", "_").downcase] = val
end
end
Expand Down