blib/lib/Monitoring/Sneck/Checks/sensors.pm

filter: Statementstmt 100% 147 / 147 Branchbran 86.0% 74 / 86 Conditioncond 66.2% 47 / 71 MC/DCmcdc 40.9% 18 / 44 Subroutinesub 100% 19 / 19 Podpod 100% 10 / 10 totaltotal 83.5% 315 / 377 CC 79 SCAR 26.5
CC
79
Cov
88%
sensor_status
31.2
linux_sensors
17.3
perf_ranges
9.3
CRAP 14.2
« blib/lib/Monitoring/Sneck/Checks/http.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/kernel_modules.pm »
1 package Monitoring::Sneck::Checks::sensors;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Monitoring::Sneck::Checks::sensors - checks temperatures, fans, and voltages
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 3 our $VERSION = '0.0.1';
20  
21 # Where Linux's hwmon devices are. Set by tests.
22 3 our $HWMON = '/sys/class/hwmon';
23  
24 # The hwmon sensor types, what their inputs are divided by, and their units.
25 3 our %TYPES = ( temp => [ 1000, 'C' ], fan => [ 1, 'RPM' ], in => [ 1000, 'V' ] );
26  
27 =head1 SYNOPSIS
28  
29 sensors|perl:sensors
30 cpu_temp|perl:sensors -s '^coretemp' -w 80 -c 90
31 no_gpu|perl:sensors -x '^nouveau' -i
32  
33 =head1 DESCRIPTION
34  
35 On Linux, reads every temperature, fan, and voltage from
36 /sys/class/hwmon, so lm-sensors is not needed, and checks each against
37 the limits its chip gives, as well as its alarm and fault flags. With
38 --warning or --critical, temperatures are checked against those alone,
39 their alarm flags left out.
40  
41 - temperatures :: At or over crit, or a crit alarm, is CRITICAL. At or
42 over max, or a max or plain alarm, is WARNING.
43  
44 - fans :: Under min, or a alarm, is CRITICAL, as a fan that stopped is
45 one.
46  
47 - voltages :: Outside of min and max, or a alarm, is WARNING.
48  
49 A fault, a sensor that is broken or not hooked up, is UNKNOWN unless
50 --ignore-fault is given. A sensor that can not be read, such as on a
51 GPU that is powered down, is left out. Limits of 0 or less are taken
52 as not set, as some chips report them that way.
53  
54 On FreeBSD, only temperatures are known, from dev.cpu.N.temperature,
55 with coretemp's tjmax as crit, and the ACPI thermal zones, with _CRT as
56 crit and _HOT as max, through L<BSD::Sysctl>. Other OSes are UNKNOWN,
57 as is finding no sensors.
58  
59 On Linux each sensor is named by its chip and its label, such as
60 "coretemp Core 0", or its file, such as "nct6775 fan2". On FreeBSD they
61 are cpuN and tzN. Every one with a reading goes into the perfdata,
62 with what it is checked against as its ranges.
63  
64 =head1 OPTIONS
65  
66 - -s, --sensor :: A regex sensors names must match to be checked. May
67 be given more than once, matching any is enough.
68  
69 - -x, --exclude :: A regex for sensors to leave out. May be given more
70 than once.
71  
72 - -w, --warning :: Range of degrees C for temperatures, in place of
73 what the chips say.
74  
75 - -c, --critical :: Range of degrees C for temperatures, in place of
76 what the chips say.
77  
78 - -i, --ignore-fault :: Faults are OK.
79  
80 =cut
81  
82 15 ▶sub option_spec { return ( 'sensor|s=s@', 'exclude|x=s@', 'warning|w=s', 'critical|c=s', 'ignore-fault|i' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
83  
84 15 ▶sub ranges { return ( 'warning', 'critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
85  
86 15 ▶sub validate {
Subroutine validate: called CC=5 SCAR=16.1
Pod documented
87 15 my ( $self, $options ) = @_;
88  
89 15 my @problems;
90 15 foreach my $name ( 'sensor', 'exclude' ) {
91 30 ▶ foreach my $regex ( @{ $options->{$name} // [] } ) {
Condition100%2/2
$options->{$name} // []
l!l
525
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
Aresult
11
00
92 5 ▶ push( @problems, '--' . $name . ' "' . $regex . '" is not a valid regex' ) if !eval { qr/$regex/; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { qr/$regex/u; 1 } } 1 4 5
93 }
94 }
95  
96 15 return @problems;
97 } ## end sub validate
98  
99 =head1 METHODS
100  
101 =head2 check
102  
103 Does the check.
104  
105 =cut
106  
107 14 ▶sub check {
Subroutine check: called CC=7 SCAR=19.5
Pod documented
108 14 my ($self) = @_;
109  
110 14 ▶ my @sensors = grep { $self->wanted( $_->{name} ) }
Branch: 1 branch
BranchTrueFalseTotal
$self->os eq 'freebsd' ? : 2 12 14
111 ( $self->os eq 'freebsd' ? $self->freebsd_sensors : $self->linux_sensors );
112 14 ▶ if ( !defined( $sensors[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sensors[0]) 1 13 14
113 1 return $self->finish( $self->UNKNOWN, 'no sensors found' );
114 }
115  
116 13 my @codes;
117 my @problems;
118 13 foreach my $sensor (@sensors) {
119 62 my ( $code, $why ) = $self->sensor_status($sensor);
120 62 push( @codes, $code );
121 62 ▶ push( @problems, $sensor->{name} . ' ' . $why ) if $code != $self->OK;
Branch: 1 branch
BranchTrueFalseTotal
if $code != $self->OK 8 54 62
122 62 ▶ next if !defined( $sensor->{value} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sensor->{"value"} 0 62 62
123 62 my ( $warning, $critical ) = $self->perf_ranges($sensor);
124 $self->perfdata(
125 label => $sensor->{name},
126 value => $sensor->{value},
127 62 warning => $warning,
128 critical => $critical
129 );
130 } ## end foreach my $sensor (@sensors)
131  
132 13 my $message = scalar(@sensors) . ' sensors';
133 13 ▶ $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $problems[0] 8 5 13
134  
135 13 return $self->finish( $self->worst(@codes), $message );
136 } ## end sub check
137  
138 =head2 perf_ranges
139  
140 Returns the warning and critical ranges for a sensor's perfdata, as
141 it is checked under L</DESCRIPTION>. For a temperature, max and crit,
142 or --warning and --critical if given. For a fan, critical is under
143 min. For a voltage, warning is outside min to max. Any not known is
144 undef.
145  
146 - sensor :: As for L</sensor_status>.
147  
148 my ( $warning, $critical ) = $check->perf_ranges( { type => 'fan', min => 300 } );
149 # ( undef, '300:' )
150  
151 =cut
152  
153 62 ▶sub perf_ranges {
Subroutine perf_ranges: called CC=9 SCAR=22.3
Pod documented
154 62 my ( $self, $sensor ) = @_;
155  
156 62 ▶ if ( $sensor->{type} eq 'temp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sensor->{"type"} eq "temp") 36 26 62
157 36 ▶ return ( $self->opt('warning'), $self->opt('critical') ) if $self->user_limits;
Branch: 1 branch
BranchTrueFalseTotal
if $self->user_limits 1 35 36
158 35 return ( $sensor->{max}, $sensor->{crit} );
159 }
160 26 ▶ if ( $sensor->{type} eq 'fan' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sensor->{"type"} eq "fan") 9 17 26
161 9 ▶ return ( undef, defined( $sensor->{min} ) ? $sensor->{min} . ':' : undef );
Branch: 1 branch
BranchTrueFalseTotal
defined $sensor->{'min'} ? : 9 0 9
162 }
163 17 ▶ return ( undef, undef ) if !defined( $sensor->{min} ) && !defined( $sensor->{max} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sensor->{"min"} or defined $sensor->{"max"} 8 9 17
Condition66%2/3
defined $sensor->{"min"} or defined $sensor->{"max"}
l!l&&r!l&&!r
908
MC/DC50%1/2
defined $sensor->{"min"} or defined $sensor->{"max"}
defined $sensor->{"min"} defined $sensor->{"max"}
Truth table66%2/3
A or B
A= defined $sensor->{"min"}B= defined $sensor->{"max"}
ABresult
1X1
011
000
164  
165 9 ▶ return ( ( $sensor->{min} // '~' ) . ':' . ( $sensor->{max} // '' ), undef );
Condition50%2/4
$sensor->{"min"} // "~"
l!l
90
$sensor->{"max"} // ""
l!l
90
MC/DC0%0/1
$sensor->{"min"} // "~"
$sensor->{"min"}
MC/DC0%0/1
$sensor->{"max"} // ""
$sensor->{"max"}
Truth table50%1/2
A
A= $sensor->{"min"}
Aresult
11
00
Truth table50%1/2
A
A= $sensor->{"max"}
Aresult
11
00
166 } ## end sub perf_ranges
167  
168 =head2 user_limits
169  
170 Returns 1 if --warning or --critical was given, so they are used for
171 temperatures in place of what the chips say.
172  
173 =cut
174  
175 72 ▶sub user_limits {
Subroutine user_limits: called CC=3 SCAR=11.1
Pod documented
176 72 my ($self) = @_;
177  
178 72 ▶ return ( defined( $self->opt('warning') ) || defined( $self->opt('critical') ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->opt('warning') || defined $self->opt('critical') ? : 2 70 72
Condition66%2/3
defined $self->opt("warning") or defined $self->opt("critical")
l!l&&r!l&&!r
2070
MC/DC50%1/2
defined $self->opt("warning") or defined $self->opt("critical")
defined $self->opt("warning") defined $self->opt("critical")
Truth table66%2/3
A or B
A= defined $self->opt("warning")B= defined $self->opt("critical")
ABresult
1X1
011
000
179 }
180  
181 =head2 wanted
182  
183 Returns 1 if a sensor name is to be checked, as --sensor and --exclude
184 say, else 0.
185  
186 =cut
187  
188 76 ▶sub wanted {
Subroutine wanted: called CC=6 SCAR=17.9
Pod documented
189 76 my ( $self, $name ) = @_;
190  
191 76 ▶ my @want = @{ $self->opt('sensor') // [] };
Condition100%2/2
$self->opt("sensor") // []
l!l
1759
MC/DC100%1/1
$self->opt("sensor") // []
$self->opt("sensor")
Truth table100%2/2
A
A= $self->opt("sensor")
Aresult
11
00
192 76 ▶ return 0 if defined( $want[0] ) && !grep { $name =~ /$_/ } @want;
Branch: 1 branch
BranchTrueFalseTotal
if defined $want[0] and not grep({$name =~ /$_/u;} @want) 13 63 76
Condition100%3/3
defined $want[0] and not grep({$name =~ /$_/u;} @want)
!ll&&!rl&&r
59413
MC/DC100%2/2
defined $want[0] and not grep({$name =~ /$_/u;} @want)
defined $want[0] not grep({$name =~ /$_/u;} @want)
Truth table100%3/3
A and not(B)
A= defined $want[0]B= not grep({$name =~ /$_/u;} @want)
ABresult
0X0
100
111
193 63 ▶ return 0 if grep { $name =~ /$_/ } @{ $self->opt('exclude') // [] };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$name =~ /$_/u;} @{$self->opt("exclude") // [];} 1 62 63
Condition100%2/2
$self->opt("exclude") // []
l!l
360
MC/DC100%1/1
$self->opt("exclude") // []
$self->opt("exclude")
Truth table100%2/2
A
A= $self->opt("exclude")
Aresult
11
00
194  
195 62 return 1;
196 }
197  
198 =head2 sensor_status
199  
200 Works out the status of a sensor, as said under L</DESCRIPTION>.
201  
202 - sensor :: Hash ref of name, type, one of temp, fan, or in, value,
203 min, max, crit, any undef if not known, and alarm, max_alarm,
204 min_alarm, crit_alarm, and fault, each 1 if set.
205  
206 Returns the code and why, or undef for why if OK.
207  
208 =cut
209  
210 62 ▶sub sensor_status {
Subroutine sensor_status: called CC=26 SCAR=34.4
Pod documented
211 62 my ( $self, $sensor ) = @_;
212  
213 62 my $value = $sensor->{value};
214 62 my $unit = $TYPES{ $sensor->{type} }[1];
215 62 ▶ if ( $sensor->{fault} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sensor->{"fault"}) 2 60 62
216 2 ▶ return ( $self->OK, undef ) if $self->opt('ignore-fault');
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt("ignore-fault") 1 1 2
217 1 return ( $self->UNKNOWN, 'has a fault' );
218 }
219 60 ▶ return ( $self->UNKNOWN, 'can not be read' ) if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 60 60
220  
221 60 my $is = 'is ' . $value . ' ' . $unit;
222 60 ▶ if ( $sensor->{type} eq 'temp' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sensor->{'type'} eq 'temp') { } 36 24 60
elsif ($sensor->{'type'} eq 'fan') { } 9 15 24
223 36 ▶ if ( $self->user_limits ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->user_limits) 1 35 36
224 1 my $code = $self->threshold_status( $value, $self->opt('warning'), $self->opt('critical') );
225 1 ▶ return ( $code, $code == $self->OK ? undef : $is );
Branch: 1 branch
BranchTrueFalseTotal
$code == $self->OK ? : 0 1 1
226 }
227 return ( $self->CRITICAL, $is . ', crit is ' . $sensor->{crit} )
228 35 ▶ if defined( $sensor->{crit} ) && $value >= $sensor->{crit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sensor->{"crit"} and $value >= $sensor->{"crit"} 2 33 35
Condition100%3/3
defined $sensor->{"crit"} and $value >= $sensor->{"crit"}
!ll&&!rl&&r
12212
MC/DC100%2/2
defined $sensor->{"crit"} and $value >= $sensor->{"crit"}
defined $sensor->{"crit"} $value >= $sensor->{"crit"}
Truth table100%3/3
A and B
A= defined $sensor->{"crit"}B= $value >= $sensor->{"crit"}
ABresult
0X0
100
111
229 33 ▶ return ( $self->CRITICAL, $is . ', its crit alarm is set' ) if $sensor->{crit_alarm};
Branch: 1 branch
BranchTrueFalseTotal
if $sensor->{"crit_alarm"} 1 32 33
230 return ( $self->WARNING, $is . ', max is ' . $sensor->{max} )
231 32 ▶ if defined( $sensor->{max} ) && $value >= $sensor->{max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sensor->{"max"} and $value >= $sensor->{"max"} 1 31 32
Condition100%3/3
defined $sensor->{"max"} and $value >= $sensor->{"max"}
!ll&&!rl&&r
15161
MC/DC100%2/2
defined $sensor->{"max"} and $value >= $sensor->{"max"}
defined $sensor->{"max"} $value >= $sensor->{"max"}
Truth table100%3/3
A and B
A= defined $sensor->{"max"}B= $value >= $sensor->{"max"}
ABresult
0X0
100
111
232 31 ▶ return ( $self->WARNING, $is . ', its alarm is set' ) if $sensor->{max_alarm} || $sensor->{alarm};
Branch: 1 branch
BranchTrueFalseTotal
if $sensor->{"max_alarm"} or $sensor->{"alarm"} 0 31 31
Condition33%1/3
$sensor->{"max_alarm"} or $sensor->{"alarm"}
l!l&&r!l&&!r
0031
MC/DC0%0/2
$sensor->{"max_alarm"} or $sensor->{"alarm"}
$sensor->{"max_alarm"} $sensor->{"alarm"}
Truth table33%1/3
A or B
A= $sensor->{"max_alarm"}B= $sensor->{"alarm"}
ABresult
1X1
011
000
233 } elsif ( $sensor->{type} eq 'fan' ) {
234 return ( $self->CRITICAL, $is . ', min is ' . $sensor->{min} )
235 9 ▶ if defined( $sensor->{min} ) && $value < $sensor->{min};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sensor->{"min"} and $value < $sensor->{"min"} 1 8 9
Condition66%2/3
defined $sensor->{"min"} and $value < $sensor->{"min"}
!ll&&!rl&&r
081
MC/DC50%1/2
defined $sensor->{"min"} and $value < $sensor->{"min"}
defined $sensor->{"min"} $value < $sensor->{"min"}
Truth table66%2/3
A and B
A= defined $sensor->{"min"}B= $value < $sensor->{"min"}
ABresult
0X0
100
111
236 8 ▶ return ( $self->CRITICAL, $is . ', its alarm is set' ) if $sensor->{alarm} || $sensor->{min_alarm};
Branch: 1 branch
BranchTrueFalseTotal
if $sensor->{"alarm"} or $sensor->{"min_alarm"} 0 8 8
Condition33%1/3
$sensor->{"alarm"} or $sensor->{"min_alarm"}
l!l&&r!l&&!r
008
MC/DC0%0/2
$sensor->{"alarm"} or $sensor->{"min_alarm"}
$sensor->{"alarm"} $sensor->{"min_alarm"}
Truth table33%1/3
A or B
A= $sensor->{"alarm"}B= $sensor->{"min_alarm"}
ABresult
1X1
011
000
237 } else {
238 return ( $self->WARNING, $is . ', min is ' . $sensor->{min} )
239 15 ▶ if defined( $sensor->{min} ) && $value < $sensor->{min};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sensor->{"min"} and $value < $sensor->{"min"} 0 15 15
Condition66%2/3
defined $sensor->{"min"} and $value < $sensor->{"min"}
!ll&&!rl&&r
690
MC/DC0%0/2
defined $sensor->{"min"} and $value < $sensor->{"min"}
defined $sensor->{"min"} $value < $sensor->{"min"}
Truth table66%2/3
A and B
A= defined $sensor->{"min"}B= $value < $sensor->{"min"}
ABresult
0X0
100
111
240 return ( $self->WARNING, $is . ', max is ' . $sensor->{max} )
241 15 ▶ if defined( $sensor->{max} ) && $value > $sensor->{max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sensor->{"max"} and $value > $sensor->{"max"} 1 14 15
Condition100%3/3
defined $sensor->{"max"} and $value > $sensor->{"max"}
!ll&&!rl&&r
681
MC/DC100%2/2
defined $sensor->{"max"} and $value > $sensor->{"max"}
defined $sensor->{"max"} $value > $sensor->{"max"}
Truth table100%3/3
A and B
A= defined $sensor->{"max"}B= $value > $sensor->{"max"}
ABresult
0X0
100
111
242 return ( $self->WARNING, $is . ', its alarm is set' )
243 14 ▶ if $sensor->{alarm} || $sensor->{min_alarm} || $sensor->{max_alarm};
Branch: 1 branch
BranchTrueFalseTotal
if $sensor->{"alarm"} or $sensor->{"min_alarm"} or $sensor->{"max_alarm"} 0 14 14
Condition33%2/6
$sensor->{"alarm"} or $sensor->{"min_alarm"} or $sensor->{"max_alarm"}
l!l&&r!l&&!r
0014
$sensor->{"alarm"} or $sensor->{"min_alarm"}
l!l&&r!l&&!r
0014
MC/DC0%0/3
$sensor->{"alarm"} or $sensor->{"min_alarm"} or $sensor->{"max_alarm"}
$sensor->{"alarm"} $sensor->{"min_alarm"} $sensor->{"max_alarm"}
Truth table25%1/4
A or B or C
A= $sensor->{"alarm"}B= $sensor->{"min_alarm"}C= $sensor->{"max_alarm"}
ABCresult
1XX1
01X1
0011
0000
244 }
245  
246 53 return ( $self->OK, undef );
247 } ## end sub sensor_status
248  
249 =head2 linux_sensors
250  
251 Returns the sensors under /sys/class/hwmon, as hash refs as taken by
252 L</sensor_status>.
253  
254 =cut
255  
256 12 ▶sub linux_sensors {
Subroutine linux_sensors: called CC=16 SCAR=28.5
Pod documented
257 12 my ($self) = @_;
258  
259 12 my @sensors;
260 12 ▶ opendir( my $hwmon, $HWMON ) or return ();
Branch: 1 branch
BranchTrueFalseTotal
opendir my $hwmon, $Monitoring::Sneck::Checks::sensors::HWMON or return () 12 0 12
261 12 foreach my $device ( sort( grep { /\Ahwmon[0-9]+\z/ } readdir($hwmon) ) ) {
262 24 my $dir = $HWMON . '/' . $device;
263 24 ▶ my $chip = _read( $dir . '/name' ) // $device;
Condition33%1/3
_read($dir . '/name') // $device
l!l&&r!l&&!r
2400
MC/DC0%0/2
_read($dir . '/name') // $device
_read($dir . '/name') $device
Truth table33%1/3
A // B
A= _read($dir . '/name')B= $device
ABresult
1X1
011
000
264 24 ▶ opendir( my $files, $dir ) or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $files, $dir or next 24 0 24
265 24 ▶ my %inputs = map { /\A((temp|fan|in)([0-9]+))_input\z/ ? ( $1 => $2 ) : () } readdir($files);
Branch: 1 branch
BranchTrueFalseTotal
/\A((temp|fan|in)([0-9]+))_input\z/u ? : 70 196 266
266 24 foreach my $sensor ( sort { _natural( $a, $b ) } keys(%inputs) ) {
267 70 my $type = $inputs{$sensor};
268 70 my $divisor = $TYPES{$type}[0];
269 280 ▶ my $read = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
270 280 my $value = _read( $dir . '/' . $sensor . '_' . $_[0] );
271 280 ▶ return undef if !defined($value) || $value !~ /\A-?[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /\A-?[0-9]+\z/u 126 154 280
Condition66%2/3
defined $value and $value =~ /\A-?[0-9]+\z/u
!ll&&!rl&&r
1260154
MC/DC50%1/2
defined $value and $value =~ /\A-?[0-9]+\z/u
defined $value $value =~ /\A-?[0-9]+\z/u
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /\A-?[0-9]+\z/u
ABresult
0X0
100
111
272 154 return $value / $divisor;
273 70 };
274 70 ▶ my %info = (
Condition66%2/3
_read($dir . '/' . $sensor . '_label') // $sensor
l!l&&r!l&&!r
24460
MC/DC0%0/2
_read($dir . '/' . $sensor . '_label') // $sensor
_read($dir . '/' . $sensor . '_label') $sensor
Truth table66%2/3
A // B
A= _read($dir . '/' . $sensor . '_label')B= $sensor
ABresult
1X1
011
000
275 name => $chip . ' ' . ( _read( $dir . '/' . $sensor . '_label' ) // $sensor ),
276 type => $type,
277 value => $read->('input'),
278 );
279 70 foreach my $limit ( 'min', 'max', 'crit' ) {
280 210 my $value = $read->($limit);
281 210 ▶ $info{$limit} = ( defined($value) && $value > 0 ) ? $value : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $value && $value > 0 ? : 84 126 210
Condition66%2/3
defined $value and $value > 0
!ll&&!rl&&r
126084
MC/DC50%1/2
defined $value and $value > 0
defined $value $value > 0
Truth table66%2/3
A and B
A= defined $valueB= $value > 0
ABresult
0X0
100
111
282 }
283 70 foreach my $flag ( 'alarm', 'min_alarm', 'max_alarm', 'crit_alarm', 'fault' ) {
284 350 ▶ $info{$flag} = ( _read( $dir . '/' . $sensor . '_' . $flag ) // 0 ) eq '1' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(_read($dir . '/' . $sensor . '_' . $flag) // 0) eq '1' ? : 3 347 350
Condition100%2/2
_read($dir . '/' . $sensor . '_' . $flag) // 0
l!l
14336
MC/DC100%1/1
_read($dir . '/' . $sensor . '_' . $flag) // 0
_read($dir . '/' . $sensor . '_' . $flag)
Truth table100%2/2
A
A= _read($dir . '/' . $sensor . '_' . $flag)
Aresult
11
00
285 }
286 # such as a GPU that is powered down, which lm-sensors shows as N/A
287 70 ▶ next if !defined( $info{value} ) && !$info{fault};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info{"value"} or $info{"fault"} 0 70 70
Condition33%1/3
defined $info{"value"} or $info{"fault"}
l!l&&r!l&&!r
7000
MC/DC0%0/2
defined $info{"value"} or $info{"fault"}
defined $info{"value"} $info{"fault"}
Truth table33%1/3
A or B
A= defined $info{"value"}B= $info{"fault"}
ABresult
1X1
011
000
288 70 push( @sensors, \%info );
289 } ## end foreach my $sensor ( sort { _natural( $a, $b ) ...})
290 } ## end foreach my $device ( sort( grep { /\Ahwmon[0-9]+\z/...}))
291  
292 12 return @sensors;
293 } ## end sub linux_sensors
294  
295 =head2 freebsd_sensors
296  
297 Returns the temperatures FreeBSD knows through sysctl, as hash refs as
298 taken by L</sensor_status>.
299  
300 =cut
301  
302 2 ▶sub freebsd_sensors {
Subroutine freebsd_sensors: called CC=6 SCAR=17.9
Pod documented
303 2 my ($self) = @_;
304  
305 2 my @sensors;
306 2 ▶ my $cpus = eval { $self->sysctl('hw.ncpu') } // 0;
Condition50%1/2
eval { do { $self->sysctl("hw.ncpu") } } // 0
l!l
20
MC/DC0%0/1
eval { do { $self->sysctl("hw.ncpu") } } // 0
eval { do { $self->sysctl("hw.ncpu") } }
Truth table50%1/2
A
A= eval { do { $self->sysctl("hw.ncpu") } }
Aresult
11
00
307 2 foreach my $cpu ( 0 .. $cpus - 1 ) {
308 4 my $value = _celsius( eval { $self->sysctl( 'dev.cpu.' . $cpu . '.temperature' ) } );
309 4 ▶ next if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
310 push(
311 @sensors,
312 {
313 name => 'cpu' . $cpu,
314 type => 'temp',
315 value => $value,
316 4 crit => _celsius( eval { $self->sysctl( 'dev.cpu.' . $cpu . '.coretemp.tjmax' ) } ),
317 }
318 );
319 } ## end foreach my $cpu ( 0 .. $cpus - 1 )
320 2 foreach my $zone ( 0 .. 31 ) {
321 4 my $base = 'hw.acpi.thermal.tz' . $zone;
322 4 my $value = _celsius( eval { $self->sysctl( $base . '.temperature' ) } );
323 4 ▶ last if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 2 4
324 push(
325 @sensors,
326 {
327 name => 'tz' . $zone,
328 type => 'temp',
329 value => $value,
330 2 crit => _celsius( eval { $self->sysctl( $base . '._CRT' ) } ),
331 2 max => _celsius( eval { $self->sysctl( $base . '._HOT' ) } ),
332 }
333 );
334 } ## end foreach my $zone ( 0 .. 31 )
335  
336 2 return @sensors;
337 } ## end sub freebsd_sensors
338  
339 # Reads a sysfs file.
340 #
341 # Args...
342 #
343 # - path :: The file.
344 #
345 # Returns its content without the trailing newline, or undef if it can
346 # not be read, as some sensors that are not there fail to read.
347 #
348 # Example...
349 #
350 # _read('/sys/class/hwmon/hwmon0/name'); # 'coretemp'
351 724 ▶sub _read {
Subroutine _read: called CC=2 SCAR=6.9
352 724 my ($path) = @_;
353  
354 724 my $content = read_file( $path, err_mode => 'quiet' );
355 724 ▶ return undef if !defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 508 216 724
356 216 $content =~ s/\s+\z//;
357  
358 216 return $content;
359 }
360  
361 # Turns a FreeBSD temperature sysctl into degrees C. They may come as
362 # text, such as '45.0C', as deci-Kelvin, as for IK sysctls read raw, or
363 # as degrees C already. -1, which ACPI uses for not set, is undef.
364 #
365 # Args...
366 #
367 # - value :: The value, or undef.
368 #
369 # Returns the degrees C, rounded to a tenth, or undef.
370 #
371 # Example...
372 #
373 # _celsius('45.0C'); # 45.0
374 # _celsius(3181); # 45.0
375 16 ▶sub _celsius {
Subroutine _celsius: called CC=5 SCAR=16.1
376 16 my ($value) = @_;
377  
378 16 ▶ return undef if !defined($value) || $value !~ /\A(-?[0-9]+(?:\.[0-9]+)?)C?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /\A(-?[0-9]+(?:\.[0-9]+)?)C?\z/u 4 12 16
Condition66%2/3
defined $value and $value =~ /\A(-?[0-9]+(?:\.[0-9]+)?)C?\z/u
!ll&&!rl&&r
4012
MC/DC50%1/2
defined $value and $value =~ /\A(-?[0-9]+(?:\.[0-9]+)?)C?\z/u
defined $value $value =~ /\A(-?[0-9]+(?:\.[0-9]+)?)C?\z/u
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /\A(-?[0-9]+(?:\.[0-9]+)?)C?\z/u
ABresult
0X0
100
111
379 12 $value = $1;
380 12 ▶ return undef if $value <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value <= 0 2 10 12
381 10 ▶ $value = $value / 10 - 273.15 if $value > 1000;
Branch: 1 branch
BranchTrueFalseTotal
if $value > 1000 2 8 10
382  
383 10 return sprintf( '%.1f', $value ) + 0;
384 } ## end sub _celsius
385  
386 # Compares sensor file names so temp2 comes before temp10.
387 #
388 # Args...
389 #
390 # - a, b :: The names, such as 'temp2' and 'temp10'.
391 #
392 # Returns -1, 0, or 1, as cmp.
393 #
394 # Example...
395 #
396 # sort { _natural( $a, $b ) } ( 'temp10', 'temp2' ); # ( 'temp2', 'temp10' )
397 58 ▶sub _natural {
Subroutine _natural: called CC=2 SCAR=7.0
398 58 my ( $first, $second ) = @_;
399  
400 58 my ( $first_type, $first_number ) = $first =~ /\A([a-z]+)([0-9]+)\z/;
401 58 my ( $second_type, $second_number ) = $second =~ /\A([a-z]+)([0-9]+)\z/;
402  
403 58 ▶ return $first_type cmp $second_type || $first_number <=> $second_number;
Condition66%2/3
$first_type cmp $second_type || $first_number <=> $second_number
l!l&&r!l&&!r
14440
MC/DC0%0/2
$first_type cmp $second_type || $first_number <=> $second_number
$first_type cmp $second_type $first_number <=> $second_number
Truth table66%2/3
A || B
A= $first_type cmp $second_typeB= $first_number <=> $second_number
ABresult
1X1
011
000
404 }
405  
406 3 1;
« blib/lib/Monitoring/Sneck/Checks/http.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/kernel_modules.pm »