class Riemann::Tools::Hwmon::Device

Attributes

crit[R]
hwmon[R]
lcrit[R]
number[R]
service[R]
type[R]

Public Class Methods

new(hwmon, type, number) click to toggle source
# File lib/riemann/tools/hwmon.rb, line 14
def initialize(hwmon, type, number)
  @hwmon = hwmon
  @type = type
  @number = number

  @crit = scale(read_hwmon_i('crit'))
  @lcrit = scale(read_hwmon_i('lcrit'))
  @service = ['hwmon', read_hwmon_file('name'), read_hwmon_s('label')].compact.join(' ')
end

Public Instance Methods

input() click to toggle source
# File lib/riemann/tools/hwmon.rb, line 24
def input
  read_hwmon_i('input')
end
report() click to toggle source
# File lib/riemann/tools/hwmon.rb, line 28
def report
  value = scale(input)

  state = :ok
  state = :critical if crit && value >= crit
  state = :critical if lcrit && value <= lcrit
  {
    service: service,
    state: state,
    metric: value,
    description: fromat_input(value),
  }
end

Private Instance Methods

fromat_input(value) click to toggle source
# File lib/riemann/tools/hwmon.rb, line 55
def fromat_input(value)
  case type
  when :in then format('%<value>.3f V', { value: value })
  when :fan then "#{value} RPM"
  when :temp then format('%<value>.3f °C', { value: value })
  when :curr then format('%<value>.3f A', { value: value })
  when :power then format('%<value>.3f W', { value: value })
  when :energy then format('%<value>.3f J', { value: value })
  when :humidity then format('%<value>d %H', { value: (value * 100).to_i })
  end
end
read_hwmon_file(file) click to toggle source
# File lib/riemann/tools/hwmon.rb, line 78
def read_hwmon_file(file)
  File.read("/sys/class/hwmon/hwmon#{hwmon}/#{file}").chomp
rescue Errno::ENOENT
  nil
end
read_hwmon_i(file) click to toggle source
# File lib/riemann/tools/hwmon.rb, line 67
def read_hwmon_i(file)
  s = read_hwmon_s(file)
  return nil if s.nil?

  s.to_i
end
read_hwmon_s(file) click to toggle source
# File lib/riemann/tools/hwmon.rb, line 74
def read_hwmon_s(file)
  read_hwmon_file("#{type}#{number}_#{file}")
end
scale(value) click to toggle source
# File lib/riemann/tools/hwmon.rb, line 44
def scale(value)
  return nil if value.nil?

  case type
  when :fan then value.to_i # rpm
  when :in, :temp, :curr then value.to_f / 1000 # mV, m°C, mA
  when :humidity then value.to_f / 100 # %H
  when :power, :energy then value.to_f / 1_000_000 # uW, uJ
  end
end