« 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} // []
MC/DC100%1/1
$options->{$name} // []
$options->{$name}
Truth table100%2/2
A
A= $options->{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 5 | ▶ | push( @problems, '--' . $name . ' "' . $regex . '" is not a valid regex' ) if !eval { qr/$regex/; 1 }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | ( $self->os eq 'freebsd' ? $self->freebsd_sensors : $self->linux_sensors ); | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 14 | ▶ | if ( !defined( $sensors[0] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 62 | ▶ | next if !defined( $sensor->{value} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 36 | ▶ | return ( $self->opt('warning'), $self->opt('critical') ) if $self->user_limits; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 35 | return ( $sensor->{max}, $sensor->{crit} ); | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 26 | ▶ | if ( $sensor->{type} eq 'fan' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 9 | ▶ | return ( undef, defined( $sensor->{min} ) ? $sensor->{min} . ':' : undef ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 17 | ▶ | return ( undef, undef ) if !defined( $sensor->{min} ) && !defined( $sensor->{max} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $sensor->{"min"} or defined $sensor->{"max"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 9 | ▶ | return ( ( $sensor->{min} // '~' ) . ':' . ( $sensor->{max} // '' ), undef ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$sensor->{"min"} // "~"
$sensor->{"max"} // ""
MC/DC0%0/1
$sensor->{"min"} // "~"
$sensor->{"min"}
MC/DC0%0/1
$sensor->{"max"} // ""
$sensor->{"max"}
Truth table50%1/2
A
A= $sensor->{"min"}
Truth table50%1/2
A
A= $sensor->{"max"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $self->opt("warning") or defined $self->opt("critical")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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") // []
MC/DC100%1/1
$self->opt("sensor") // []
$self->opt("sensor")
Truth table100%2/2
A
A= $self->opt("sensor")
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 76 | ▶ | return 0 if defined( $want[0] ) && !grep { $name =~ /$_/ } @want; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $want[0] and not grep({$name =~ /$_/u;} @want)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 63 | ▶ | return 0 if grep { $name =~ /$_/ } @{ $self->opt('exclude') // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->opt("exclude") // []
MC/DC100%1/1
$self->opt("exclude") // []
$self->opt("exclude")
Truth table100%2/2
A
A= $self->opt("exclude")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 2 | ▶ | return ( $self->OK, undef ) if $self->opt('ignore-fault'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | return ( $self->UNKNOWN, 'has a fault' ); | |||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 60 | ▶ | return ( $self->UNKNOWN, 'can not be read' ) if !defined($value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 60 | my $is = 'is ' . $value . ' ' . $unit; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 60 | ▶ | if ( $sensor->{type} eq 'temp' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 36 | ▶ | if ( $self->user_limits ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | return ( $self->CRITICAL, $is . ', crit is ' . $sensor->{crit} ) | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 35 | ▶ | if defined( $sensor->{crit} ) && $value >= $sensor->{crit}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $sensor->{"crit"} and $value >= $sensor->{"crit"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 33 | ▶ | return ( $self->CRITICAL, $is . ', its crit alarm is set' ) if $sensor->{crit_alarm}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | return ( $self->WARNING, $is . ', max is ' . $sensor->{max} ) | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 32 | ▶ | if defined( $sensor->{max} ) && $value >= $sensor->{max}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $sensor->{"max"} and $value >= $sensor->{"max"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 31 | ▶ | return ( $self->WARNING, $is . ', its alarm is set' ) if $sensor->{max_alarm} || $sensor->{alarm}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sensor->{"max_alarm"} or $sensor->{"alarm"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $sensor->{"min"} and $value < $sensor->{"min"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 8 | ▶ | return ( $self->CRITICAL, $is . ', its alarm is set' ) if $sensor->{alarm} || $sensor->{min_alarm}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sensor->{"alarm"} or $sensor->{"min_alarm"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 238 | return ( $self->WARNING, $is . ', min is ' . $sensor->{min} ) | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 15 | ▶ | if defined( $sensor->{min} ) && $value < $sensor->{min}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $sensor->{"min"} and $value < $sensor->{"min"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | return ( $self->WARNING, $is . ', max is ' . $sensor->{max} ) | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 15 | ▶ | if defined( $sensor->{max} ) && $value > $sensor->{max}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $sensor->{"max"} and $value > $sensor->{"max"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | return ( $self->WARNING, $is . ', its alarm is set' ) | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 14 | ▶ | if $sensor->{alarm} || $sensor->{min_alarm} || $sensor->{max_alarm}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$sensor->{"alarm"} or $sensor->{"min_alarm"} or $sensor->{"max_alarm"}
$sensor->{"alarm"} or $sensor->{"min_alarm"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
_read($dir . '/name') // $device
_read($dir . '/name')
$device
Truth table33%1/3
A // B
A= _read($dir . '/name')B= $device
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 24 | ▶ | opendir( my $files, $dir ) or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 24 | ▶ | my %inputs = map { /\A((temp|fan|in)([0-9]+))_input\z/ ? ( $1 => $2 ) : () } readdir($files); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $value and $value =~ /\A-?[0-9]+\z/u
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
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 154 | return $value / $divisor; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 70 | }; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 70 | ▶ | my %info = ( | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
_read($dir . '/' . $sensor . '_label') // $sensor
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $value and $value > 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
_read($dir . '/' . $sensor . '_' . $flag) // 0
MC/DC100%1/1
_read($dir . '/' . $sensor . '_' . $flag) // 0
_read($dir . '/' . $sensor . '_' . $flag)
Truth table100%2/2
A
A= _read($dir . '/' . $sensor . '_' . $flag)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $info{"value"} or $info{"fault"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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")
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $value and $value =~ /\A(-?[0-9]+(?:\.[0-9]+)?)C?\z/u
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
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 12 | $value = $1; | |||||||||||||||||||||||||||||||||||||||||
| 380 | 12 | ▶ | return undef if $value <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | 10 | ▶ | $value = $value / 10 - 273.15 if $value > 1000; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||