Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor riemann-hwmon improvements #298

Merged
merged 2 commits into from
Jun 29, 2024
Merged
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
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