Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
puppet-module-unki-sensors
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
puppet-modules
puppet-module-unki-sensors
Commits
86f1688d
Commit
86f1688d
authored
7 years ago
by
Andreas Unterkircher
Browse files
Options
Downloads
Patches
Plain Diff
sensors.rb, convert to class
parent
29390aa0
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/facter/sensors.rb
+193
-147
193 additions, 147 deletions
lib/facter/sensors.rb
with
193 additions
and
147 deletions
lib/facter/sensors.rb
+
193
−
147
View file @
86f1688d
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment