Skip to content
Snippets Groups Projects
Commit 86f1688d authored by Andreas Unterkircher's avatar Andreas Unterkircher
Browse files

sensors.rb, convert to class

parent 29390aa0
Branches
Tags
No related merge requests found
......@@ -21,196 +21,242 @@ require 'facter'
$blacklist_conf = '/etc/facter/sensors_blacklist.conf'
#
# has_raw_sensor_data? returns TRUE if lm-sensors returned output at all.
# Otherwise returns FALSE.
#
def has_raw_sensor_data?
if !defined?($sensor_data_raw) or
!$sensor_data_raw.is_a?(String) or
$sensor_data_raw.nil? or
$sensor_data_raw.empty?
return FALSE
class PuppetModuleUnkiSensors
private
@sensor_data_raw = nil
@sensor_data = nil
@blacklist = nil
#
# has_raw_sensor_data? returns TRUE, if lm-sensors returned output
# at all. Otherwise it returns FALSE.
#
def has_raw_sensor_data?
if !defined?(@sensor_data_raw) or
!@sensor_data_raw.is_a?(String) or
@sensor_data_raw.nil? or
@sensor_data_raw.empty?
return FALSE
end
return TRUE
end
return TRUE
end
#
# has_sensor_data? returns TRUE, if lm-sensors returned output
# at all. Otherwise it returns FALSE. It acts similar to
# has_raw_sensor_data? but work on the parsed sensor data
# retrieved from the raw-data.
#
def has_sensor_data?
if !defined?(@sensor_data) or
!@sensor_data.is_a?(Hash) or
@sensor_data.nil? or
@sensor_data.empty?
return FALSE
end
#
# has_sensor_data? returns TRUE if lm-sensors returned output at all.
# Otherwise returns FALSE.
#
def has_sensor_data?
if !defined?($sensor_data) or
!$sensor_data.is_a?(Hash) or
$sensor_data.nil? or
$sensor_data.empty?
return FALSE
return TRUE
end
return TRUE
end
#
# parse_sensor_data parses the read values from lm-sensors
# on puts those readings into the @sensor_data hash variable.
#
# On success it returns TRUE, otherwise FALSE.
#
def parse_sensor_data
if ! has_raw_sensor_data?
return FALSE
end
def parse_sensor_data
if ! has_raw_sensor_data?
return FALSE
end
have_chip = FALSE
have_sensor = FALSE
sensor = nil
chip = nil
@sensor_data_raw.split("\n").each { |line|
#
# if we already found a chip before in @sensor_data_raw _AND_ the
# current line contains only a newline-character ('\n'), it most
# probably signals the end of the chip's sensor data.
#
# so prepare us for the next chip to parse.
#
if have_chip == TRUE and line.empty?
chip = nil
sensor = nil
have_chip = FALSE
have_sensor = FALSE
next
end
have_chip = FALSE
have_sensor = FALSE
sensor = nil
chip = nil
#
# if we haven't located a chip and even the current line does not
# look like a chip's name, move on to the next line.
#
next if have_chip == FALSE and line !~ /^([[:graph:]]+[^:])$/
$sensor_data_raw.split("\n").each { |line|
if have_chip == FALSE
chip = $1.gsub(' ', '_')
#
# if we already found a chip before in $sensor_data_raw _AND_ the
# current line contains only a newline-character ('\n'), it most
# probably signals the end of the chip's sensor data.
#
# so prepare us for the next chip to parse.
#
if have_chip == TRUE and line.empty?
chip = nil
sensor = nil
have_chip = FALSE
have_sensor = FALSE
next
end
if @blacklist.include?(chip)
Facter.debug("Skipping blacklisted chip: #{chip}")
next
end
#
# if we haven't located a chip and even the current line does not
# look like a chip's name, move on to the next line.
# #
next if have_chip == FALSE and line !~ /^([[:graph:]]+[^:])$/
have_chip = TRUE
have_sensor = FALSE
sensor = nil
if have_chip == FALSE
chip = $1.gsub(' ', '_')
if !defined?(@sensor_data[chip]) or
@sensor_data[chip].nil? or
!@sensor_data[chip].is_a?(Hash)
if $blacklist.include?(chip)
Facter.debug("Skipping blacklisted chip: #{chip}")
@sensor_data[chip] = Hash.new
end
Facter.debug("Found chip: #{chip}")
next
end
have_chip = TRUE
have_sensor = FALSE
sensor = nil
#
# if we haven't located a sensor and even the current line does not
# look like a sensor's name, move on to the next line.
#
next if have_sensor == FALSE and line !~ /^([[:print:]]+):$/
if !defined?($sensor_data[chip]) or
$sensor_data[chip].nil? or
!$sensor_data[chip].is_a?(Hash)
# if we haven't located a sensor before, but now got a line that does
# look like a sensor's name, record it and move on to the next line.
if have_sensor == FALSE
sensor = $1.gsub(' ', '_')
$sensor_data[chip] = Hash.new
end
if @blacklist.include?("#{chip}_#{sensor}")
Facter.debug("Skipping blacklisted sensor: #{chip}_#{sensor}")
next
end
Facter.debug("Found chip: #{chip}")
next
end
have_sensor = TRUE
#
# if we haven't located a sensor and even the current line does not
# look like a sensor's name, move on to the next line.
#
next if have_sensor == FALSE and line !~ /^([[:print:]]+):$/
if !defined?(@sensor_data[chip][sensor]) or
@sensor_data[chip][sensor].nil? or
!@sensor_data[chip][sensor].is_a?(Hash)
# if we haven't located a sensor before, but now got a line that does
# look like a sensor's name, record it and move on to the next line.
if have_sensor == FALSE
sensor = $1.gsub(' ', '_')
@sensor_data[chip][sensor] = Hash.new
end
if $blacklist.include?("#{chip}_#{sensor}")
Facter.debug("Skipping blacklisted sensor: #{chip}_#{sensor}")
Facter.debug("Found sensor: #{sensor}")
next
end
have_sensor = TRUE
#
# here we await, that the line contains a sensors measurement values.
# otherwise it would signals the end of the sensors data and we have
# to clear for the next sensor.
#
if line !~ /^[[:blank:]]+([[:alnum:]]+)_([[:graph:]]+):[[:blank:]]([[:graph:]]+)/
sensor = nil
have_sensor = FALSE
redo
end
if !defined?($sensor_data[chip][sensor]) or
$sensor_data[chip][sensor].nil? or
!$sensor_data[chip][sensor].is_a?(Hash)
@sensor_data[chip][sensor]["#{$1}_#{$2}"] = $3
Facter.debug("Found data: #{$1}_#{$2} = #{$3}")
}
$sensor_data[chip][sensor] = Hash.new
end
return TRUE
end
def load_blacklist
return TRUE if ! File.exist?($blacklist_conf)
Facter.debug("Found sensor: #{sensor}")
next
if ! File.readable?($blacklist_conf)
Facter.warn("Sensor blacklist #{$blacklist_conf} is not readable!")
return FALSE
end
#
# here we await, that the line contains a sensors measurement values.
# otherwise it would signals the end of the sensors data and we have
# to clear for the next sensor.
#
if line !~ /^[[:blank:]]+([[:alnum:]]+)_([[:graph:]]+):[[:blank:]]([[:graph:]]+)/
sensor = nil
have_sensor = FALSE
redo
begin
file = File.new($blacklist_conf, 'r')
rescue Exception => err
Facter.log_exception("Failed to load blacklist config: #{err}")
return FALSE
end
$sensor_data[chip][sensor]["#{$1}_#{$2}"] = $3
Facter.debug("Found data: #{$1}_#{$2} = #{$3}")
}
while (line = file.gets)
next if !defined?(line) or line.nil? or !line.is_a?(String) or line.chomp.empty?
Facter.debug("Sensor blacklist entry: #{line.chomp}")
@blacklist.push(line.chomp)
end
return TRUE
end
file.close
return TRUE
end
def check_requirements
if ! Facter::Core::Execution::which('sensors')
Facter.warn("Failed to locate lm-sensors 'sensors' binary!")
return FALSE
end
return TRUE
end
def get_sensor_data
@sensor_data_raw = Facter::Core::Execution.exec('sensors -u -A')
def load_blacklist
return TRUE if ! File.exist?($blacklist_conf)
if !has_raw_sensor_data?
Facter.warn("Failed to read RAW-data from 'sensors' binary!")
return FALSE
end
return TRUE
end
def initialize
@blacklist = Array.new
@sensor_data = Hash.new
return if !check_requirements
return if !get_sensor_data
return if !load_blacklist()
if ! parse_sensor_data()
Facter.warn("parse_sensor_data() failed!")
return FALSE
end
if ! File.readable?($blacklist_conf)
Facter.warn("Sensor blacklist #{$blacklist_conf} is not readable!")
return FALSE
Facter.debug("Result: #{@sensor_data}")
# what @sensor_data now actually contain can be handled like this:
#
#@sensor_data.each { |chip, sensors|
# sensors.each { |sensor, data|
# data.each { |key, value|
# }
# }
#}
end
file = File.new($blacklist_conf, 'r')
while (line = file.gets)
next if !defined?(line) or line.nil? or !line.is_a?(String) or line.chomp.empty?
Facter.debug("Sensor blacklist entry: #{line.chomp}")
$blacklist.push(line.chomp)
public
def dump
return if !has_sensor_data?
return @sensor_data
end
file.close
end
return TRUE
begin
$sensors = PuppetModuleUnkiSensors.new
rescue Exception,LoadError => err
Facter.log_exception("Failed to load PuppetModuleUnkiSensors class: #{err}")
end
Facter.add(:sensors) do
confine :kernel => 'Linux'
confine :virtual => 'physical'
setcode do
begin
if ! Facter::Core::Execution::which('sensors')
Facter.warn("Failed to locate lm-sensors 'sensors' binary!")
else
$sensor_data_raw = Facter::Core::Execution.exec('sensors -u -A')
if !has_raw_sensor_data?
Facter.warn("Failed to read RAW-data from 'sensors' binary!")
else
$blacklist = Array.new
$sensor_data = Hash.new
if !load_blacklist()
Facter.warn('load_blacklist() failed!')
end
if ! parse_sensor_data() || !has_sensor_data?
Facter.warn("parse_sensor_data() failed!")
else
Facter.debug("Result: #{$sensor_data}")
# what $sensor_data now actually contain can be handled like this:
#
#$sensor_data.each { |chip, sensors|
# sensors.each { |sensor, data|
# data.each { |key, value|
# }
# }
#}
$sensor_data
end
end
end
rescue LoadError => err
Facter.log_exception("Overall error ocurred: #{err}")
end
$sensors.dump
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment