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
Show 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,15 +21,22 @@ require 'facter'
$blacklist_conf
=
'/etc/facter/sensors_blacklist.conf'
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 returns FALSE.
# 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?
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
...
...
@@ -37,20 +44,28 @@ def has_raw_sensor_data?
end
#
# has_sensor_data? returns TRUE if lm-sensors returned output at all.
# Otherwise returns FALSE.
# 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?
if
!
defined?
(
@
sensor_data
)
or
!
@
sensor_data
.
is_a?
(
Hash
)
or
@
sensor_data
.
nil?
or
@
sensor_data
.
empty?
return
FALSE
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
...
...
@@ -61,10 +76,10 @@ def parse_sensor_data
sensor
=
nil
chip
=
nil
$
sensor_data_raw
.
split
(
"
\n
"
).
each
{
|
line
|
@
sensor_data_raw
.
split
(
"
\n
"
).
each
{
|
line
|
#
# if we already found a chip before in
$
sensor_data_raw _AND_ the
# 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.
#
...
...
@@ -81,13 +96,13 @@ def parse_sensor_data
#
# 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:]]+[^:])$/
if
have_chip
==
FALSE
chip
=
$1
.
gsub
(
' '
,
'_'
)
if
$
blacklist
.
include?
(
chip
)
if
@
blacklist
.
include?
(
chip
)
Facter
.
debug
(
"Skipping blacklisted chip:
#{
chip
}
"
)
next
end
...
...
@@ -96,11 +111,11 @@ def parse_sensor_data
have_sensor
=
FALSE
sensor
=
nil
if
!
defined?
(
$
sensor_data
[
chip
])
or
$
sensor_data
[
chip
].
nil?
or
!
$
sensor_data
[
chip
].
is_a?
(
Hash
)
if
!
defined?
(
@
sensor_data
[
chip
])
or
@
sensor_data
[
chip
].
nil?
or
!
@
sensor_data
[
chip
].
is_a?
(
Hash
)
$
sensor_data
[
chip
]
=
Hash
.
new
@
sensor_data
[
chip
]
=
Hash
.
new
end
Facter
.
debug
(
"Found chip:
#{
chip
}
"
)
...
...
@@ -118,18 +133,18 @@ def parse_sensor_data
if
have_sensor
==
FALSE
sensor
=
$1
.
gsub
(
' '
,
'_'
)
if
$
blacklist
.
include?
(
"
#{
chip
}
_
#{
sensor
}
"
)
if
@
blacklist
.
include?
(
"
#{
chip
}
_
#{
sensor
}
"
)
Facter
.
debug
(
"Skipping blacklisted sensor:
#{
chip
}
_
#{
sensor
}
"
)
next
end
have_sensor
=
TRUE
if
!
defined?
(
$
sensor_data
[
chip
][
sensor
])
or
$
sensor_data
[
chip
][
sensor
].
nil?
or
!
$
sensor_data
[
chip
][
sensor
].
is_a?
(
Hash
)
if
!
defined?
(
@
sensor_data
[
chip
][
sensor
])
or
@
sensor_data
[
chip
][
sensor
].
nil?
or
!
@
sensor_data
[
chip
][
sensor
].
is_a?
(
Hash
)
$
sensor_data
[
chip
][
sensor
]
=
Hash
.
new
@
sensor_data
[
chip
][
sensor
]
=
Hash
.
new
end
Facter
.
debug
(
"Found sensor:
#{
sensor
}
"
)
...
...
@@ -147,7 +162,7 @@ def parse_sensor_data
redo
end
$
sensor_data
[
chip
][
sensor
][
"
#{
$1
}
_
#{
$2
}
"
]
=
$3
@
sensor_data
[
chip
][
sensor
][
"
#{
$1
}
_
#{
$2
}
"
]
=
$3
Facter
.
debug
(
"Found data:
#{
$1
}
_
#{
$2
}
=
#{
$3
}
"
)
}
...
...
@@ -162,55 +177,86 @@ def load_blacklist
return
FALSE
end
begin
file
=
File
.
new
(
$blacklist_conf
,
'r'
)
rescue
Exception
=>
err
Facter
.
log_exception
(
"Failed to load blacklist config:
#{
err
}
"
)
return
FALSE
end
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
)
@
blacklist
.
push
(
line
.
chomp
)
end
file
.
close
return
TRUE
end
Facter
.
add
(
:sensors
)
do
confine
:kernel
=>
'Linux'
confine
:virtual
=>
'physical'
setcode
do
begin
def
check_requirements
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'
)
return
FALSE
end
return
TRUE
end
def
get_sensor_data
@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
return
FALSE
end
if
!
load_blacklist
()
Facter
.
warn
(
'load_blacklist() failed!'
)
return
TRUE
end
if
!
parse_sensor_data
()
||
!
has_sensor_data?
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!"
)
else
Facter
.
debug
(
"Result:
#{
$sensor_data
}
"
)
return
FALSE
end
# what $sensor_data now actually contain can be handled like this:
Facter
.
debug
(
"Result:
#{
@sensor_data
}
"
)
# what @sensor_data now actually contain can be handled like this:
#
#
$
sensor_data.each { |chip, sensors|
#
@
sensor_data.each { |chip, sensors|
# sensors.each { |sensor, data|
# data.each { |key, value|
# }
# }
#}
$sensor_data
end
public
def
dump
return
if
!
has_sensor_data?
return
@sensor_data
end
end
rescue
LoadError
=>
err
Facter
.
log_exception
(
"Overall error ocurred:
#{
err
}
"
)
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
$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