From 88edb6a57524e3712ec2b45cb34cac9a6caf8b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Sat, 29 Jun 2024 16:30:34 +0200 Subject: [PATCH 1/2] Improve `riemann-hwmon` reliability Some old hardware do not report CPU package temperature and the creation of `temp1_*` (reserved for package temperature) files for the CPU is skipped. When this happen, other `temp2_*`, `temp3_*` etc. files are available as usual for each core temperature. Because we tried to enumerate the possible device names, we failed to catpure these sensor values. Moreover, people report on the internet "holes" in the numbering of the hwmon files, skipping from 8 to 10. In order to avoid these issues, rely on globbing to find all appropriate files rather than on enumerations. --- lib/riemann/tools/hwmon.rb | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/lib/riemann/tools/hwmon.rb b/lib/riemann/tools/hwmon.rb index 5b0f06a6..04dca0b5 100644 --- a/lib/riemann/tools/hwmon.rb +++ b/lib/riemann/tools/hwmon.rb @@ -83,16 +83,6 @@ def read_hwmon_file(file) end end - FIRST_NUMBER = { - in: 0, - fan: 1, - temp: 1, - curr: 1, - power: 1, - energy: 1, - humidity: 1, - }.freeze - attr_reader :devices def initialize @@ -104,18 +94,9 @@ def initialize def poll_devices res = [] - hwmon = 0 - while File.exist?("/sys/class/hwmon/hwmon#{hwmon}") - %i[in fan temp curr power energy humidity].each do |type| - number = FIRST_NUMBER[type] - while File.exist?("/sys/class/hwmon/hwmon#{hwmon}/#{type}#{number}_input") - res << Device.new(hwmon, type, number) - - number += 1 - end - end - - hwmon += 1 + Dir['/sys/class/hwmon/hwmon[0-9]*/{in,fan,temp,curr,power,energy,humidity}[0-9]*_input'].each do |filename| + m = filename.match(%r{/sys/class/hwmon/hwmon(\d+)/([[:alpha:]]+)(\d+)_input}) + res << Device.new(m[1].to_i, m[2].to_sym, m[3].to_i) end res From fbd0775e57d4ff81b2df50011cdd609cc3bbc6bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Sat, 29 Jun 2024 17:08:15 +0200 Subject: [PATCH 2/2] Avoid trailing space when sensor has no label Some sensors do not have a label. In this case, we do not want to have a trailing space in the service name. --- lib/riemann/tools/hwmon.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/riemann/tools/hwmon.rb b/lib/riemann/tools/hwmon.rb index 04dca0b5..cdcc674a 100644 --- a/lib/riemann/tools/hwmon.rb +++ b/lib/riemann/tools/hwmon.rb @@ -9,7 +9,7 @@ class Hwmon include Riemann::Tools class Device - attr_reader :hwmon, :type, :number, :crit, :lcrit, :label, :name + attr_reader :hwmon, :type, :number, :crit, :lcrit, :service def initialize(hwmon, type, number) @hwmon = hwmon @@ -18,8 +18,7 @@ def initialize(hwmon, type, number) @crit = scale(read_hwmon_i('crit')) @lcrit = scale(read_hwmon_i('lcrit')) - @label = read_hwmon_s('label') - @name = read_hwmon_file('name') + @service = ['hwmon', read_hwmon_file('name'), read_hwmon_s('label')].compact.join(' ') end def input @@ -33,7 +32,7 @@ def report state = :critical if crit && value >= crit state = :critical if lcrit && value <= lcrit { - service: "hwmon #{name} #{label}", + service: service, state: state, metric: value, description: fromat_input(value),