Skip to content

Commit

Permalink
Merge pull request #298 from riemann/improve-hwmon
Browse files Browse the repository at this point in the history
Minor `riemann-hwmon` improvements
  • Loading branch information
jamtur01 authored Jun 29, 2024
2 parents 100b59c + fbd0775 commit d1a8c74
Showing 1 changed file with 6 additions and 26 deletions.
32 changes: 6 additions & 26 deletions lib/riemann/tools/hwmon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -83,16 +82,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
Expand All @@ -104,18 +93,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
Expand Down

0 comments on commit d1a8c74

Please sign in to comment.