Skip to content

Commit

Permalink
Escape label values
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Mar 18, 2024
1 parent ad8d5eb commit 1b0bfd2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 7 additions & 3 deletions collectors/ohai/ohai_collector
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
require "json"
require "ohai"

def escape_value(v)
v.to_s.gsub(/(["\\])/, "\\\\\\1")
end

controllers = []
arrays = []
disks = []
Expand Down Expand Up @@ -83,7 +87,7 @@ puts "# HELP ohai_controller_info Disk controller information"
puts "# TYPE ohai_controller_info gauge"

controllers.each do |controller|
labels = controller.filter { |key, value| !value.nil? }.collect { |key, value| "#{key}=\"#{value}\"" }.join(",")
labels = controller.filter { |key, value| !value.nil? }.collect { |key, value| "#{key}=\"#{escape_value(value)}\"" }.join(",")

puts "ohai_controller_info{#{labels}} 1"
end
Expand All @@ -92,7 +96,7 @@ puts "# HELP ohai_array_info Disk array information"
puts "# TYPE ohai_array_info gauge"

arrays.each do |array|
labels = array.collect { |key, value| "#{key}=\"#{value}\"" }.join(",")
labels = array.collect { |key, value| "#{key}=\"#{escape_value(value)}\"" }.join(",")

puts "ohai_array_info{#{labels}} 1"
end
Expand All @@ -101,7 +105,7 @@ puts "# HELP ohai_disk_info Disk information"
puts "# TYPE ohai_disk_info gauge"

disks.each do |disk|
labels = disk.collect { |key, value| "#{key}=\"#{value}\"" }.join(",")
labels = disk.collect { |key, value| "#{key}=\"#{escape_value(value)}\"" }.join(",")

puts "ohai_disk_info{#{labels}} 1"
end
6 changes: 5 additions & 1 deletion collectors/smart/smart_collector
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

require "json"

def escape_value(v)
v.to_s.gsub(/(["\\])/, "\\\\\\1")
end

STATUS_BITS = [
"command_not_parsed",
"device_open_failed",
Expand Down Expand Up @@ -52,7 +56,7 @@ IO.read("/etc/prometheus/collectors/smart.devices").split("\n") do |line|
data[:rotation_rate] = output["rotation_rate"]
end

data[:labels] = data.collect { |key, value| "#{key}=\"#{value}\"" }.join(",")
data[:labels] = data.collect { |key, value| "#{key}=\"#{escape_value(value)}\"" }.join(",")

data[:exit_status] = output["smartctl"]["exit_status"]

Expand Down

0 comments on commit 1b0bfd2

Please sign in to comment.