fancontrol.pl support for multiple inputs
PATCH 1/1 fancontrol.pl support for multiple inputs
Hi,
Back in version 2.10.7 there was a perl implementation of fancontrol, this seams to have disappeared in version 3. Anyway I have made a number of changes which can be summarised to:
Modified to work on the average of multiple inputs. Which enables control of one fan using multiple temperature sensors as input. Ex. as found on Intels multicore CPUs which has a sensor per core and usually one fan.
CONFIGFILE Ex:
FCTEMPS=hwmon0/device/pwm1=hwmon1/device/temp1_input +
hwmon2/device/temp1_input
Changed how multiple fans are configured. Max one pwm per option (ex. FCTEMPS) but multiple options of same type per file.
Added configuration file checks.
FCFANS are now optional, so fans with no rpm readout are not boosted to MINSTART in each interval when they should be running. Added removed of pid file on exit/stop.
Added creation of log file directory and redirected all logging to log file in daemon mode.
Added support for reading temperature from a program. Program must output an integer value on stdout.
CONFIGFILE Ex:
FCTEMPS=hwmon0/device/pwm1=script:/usr/sbin/nvidia_gpu0_coretemp +
hwmon2/device/temp1_input
Added support for reading temperature from a file. File must contain an integer value on the first line.
CONFIGFILE Ex:
FCTEMPS=hwmon0/device/pwm1=file:/tmp/nvidia_gpu0_coretemp +
hwmon2/device/temp1_input
If this sounds interesting a full description can be found at
http://www.sluge.dk/egroupware/index.php?menuaction=wiki.uiwiki.view&lang=en&page=linux_lmsensors
A patch to 2.10.7 and the full script is attached
regards
Sonni
=== modified file 'fancontrol.pl'
fancontrol.pl +202 -86
@@ -3,6 +3,33 @@ # # Perl script for temperature dependent fan speed control. # +# Copyright 2009 Sonni Norlov <[email protected]> +# +# Modified to work on the average of multiple inputs. Which enables control +# of one fan using multiple temperatur sensors as input. Ex. as found on +# Intels multicore CPUs which has a sensor per core and usually one fan. +# CONFIGFILE Ex: +# FCTEMPS=hwmon0/device/pwm1=hwmon1/device/temp1_input + hwmon2/device/temp1_input +# +# Changed how multiple fans are configured. Max one pwm per option (ex. FCTEMPS) +# but multiple options of same type per file. +# Added configuration file checks. +# FCFANS are now optional, so fans with no rpm readout are not boosted to +# MINSTART in each interval when they should be running. +# Added removed of pid file on exit/stop. +# Added creation of log file directory and redirected all logging to log file +# in daemon mode. +# +# Added support for reading temperature from program. Program must output +# an integer value on stdout. +# CONFIGFILE Ex: +# FCTEMPS=hwmon0/device/pwm1=script:/usr/sbin/nvidia_gpu0_coretemp + hwmon2/device/temp1_input +# +# Added support for reading temperature from a file. File must contain +# an integer value on the first line. +# CONFIGFILE Ex: +# FCTEMPS=hwmon0/device/pwm1=file:/tmp/nvidia_gpu0_coretemp + hwmon2/device/temp1_input +# # Copyright 2004 dean takemori <[email protected]> # # This is a reimplementation in perl of Marius Reiner's bash script for
@@ -43,6 +70,7 @@ use IO::Handle; use Getopt::Std; use POSIX; +use File::Basename; $ENV{PATH} = "/bin:/usr/bin";
@@ -57,10 +85,8 @@ ### End Configuration ### our $interval; -our $pwmo; -our @afcpwm; our @afctemp; -our @afcfan; +our @afcfans; our @afcmaxtemp; our @afcmintemp; our @afcminstart;
@@ -74,15 +100,29 @@ sub UpdateFanSpeeds(); END { restorefans(); } +our $sysfs = 0; +our $dir = '/proc/sys/dev/sensors'; +### Best guees of the interface? +if (!(-d $dir)) +{ + my $predir = '/sys/class/hwmon'; + if ((-d $predir)) + { + $sysfs = 1; + } + else + { + $predir = '/sys/bus/i2c/devices'; + if ((-d $predir)) + { + $sysfs = 1; + } + } +} + our $opt_d; getopts('d'); -my $config = shift; -if (defined($config)) - { loadconfig($config); } -else - { loadconfig(CONFFILE); } - ### Daemonize if ( defined($opt_d) && ($opt_d == 1) ) {
@@ -92,6 +132,13 @@ unless (defined($pid)) { die("Couldn't fork: $!"); } + my $logdir = dirname(ERRFILE); + if (!(-d $logdir)) + { mkdir($logdir); } + $logdir = dirname(LOGFILE); + if (!(-d $logdir)) + { mkdir($logdir); } + open(*STDERR, '>', ERRFILE); IO::Handle::autoflush(*STDERR); open(*STDOUT, '>>', LOGFILE);
@@ -99,9 +146,14 @@ unless (POSIX::setsid()) { die("Couldn't open new session: $!"); } - } +my $config = shift; +if (defined($config)) + { loadconfig($config); } +else + { loadconfig(CONFFILE); } + ### Pidfile if (open(FILE, ">" . PIDFILE)) {
@@ -113,11 +165,11 @@ ### What kind of interface? -our $sysfs = 0; -our $dir = '/proc/sys/dev/sensors'; +our %afctemp; if (!(-d $dir)) { - if ($afcpwm[0] =~ m/^hwmon\d/) + my @pwms = keys %afctemp; + if ($pwms[0] =~ m/^hwmon\d/) { $dir = '/sys/class/hwmon'; }
@@ -143,13 +195,11 @@ ### Enable PWM print("Enabling PWM on fans...\n"); -my $fcvcount = 0; -while ($fcvcount < $#afcpwm+1) + +foreach my $pwmo (sort(keys %afctemp)) { - $pwmo = $afcpwm[$fcvcount]; unless (pwmenable($pwmo)) { die("Error enabling PWM on $dir/$pwmo : $!\n"); } - $fcvcount++; } print("Starting automatic fan control...\n");
@@ -173,48 +223,76 @@ { die("Unable to read config file $file: $!"); } open(F, $file); - - our ($interval, $fctemps, $fcfans, $mintemp, $maxtemp, $minstart, -$minstop); +# config file format: key=pwm=value + our ($interval, %afctemp, %afcfans, %afcmintemp, %afcmaxtemp, %afcminstart, %afcminstop); while($_ = <F>) { if ($_ =~ /^\s+$/) { next; } elsif ($_ =~ /^INTERVAL=\s*(.*)$/) { $interval = $1; next; } - elsif ($_ =~ /^FCTEMPS=\s*(.*)$/) { $fctemps = $1; next; } - elsif ($_ =~ /^FCFANS=\s*(.*)$/) { $fcfans = $1; next; } - elsif ($_ =~ /^MINTEMP=\s*(.*)$/) { $mintemp = $1; next; } - elsif ($_ =~ /^MAXTEMP=\s*(.*)$/) { $maxtemp = $1; next; } - elsif ($_ =~ /^MINSTART=\s*(.*)$/) { $minstart = $1; next; } - elsif ($_ =~ /^MINSTOP=\s*(.*)$/) { $minstop = $1; next; } + elsif ($_ =~ /^FCTEMPS=\s*(.*)\s*=\s*(.*)\s*$/) { + my $pwm = $1; + my $in = $2; + # strip whitespaces between temps + foreach my $tmp (split(/\+/, $in)) + { + $tmp =~ s/^\s+//; + $tmp =~ s/\s+$//; + if( $tmp ne "" ) { + if (defined($afctemp{$pwm}) ) { + $afctemp{$pwm} .= "+"; + } + $afctemp{$pwm} .= $tmp; + } + } + $afcfans{$pwm} = "none"; + $afcmintemp{$pwm} = 0; + $afcmaxtemp{$pwm} = 0; + $afcminstart{$pwm} = 255; + $afcminstop{$pwm} = 255; + next; + } + elsif ($_ =~ /^FCFANS=\s*(.*)\s*=\s*(.*)\s*$/) { $afcfans{$1} = $2; next; } + elsif ($_ =~ /^MINTEMP=\s*(.*)\s*=\s*(.*)\s*$/) { $afcmintemp{$1} = $2; next; } + elsif ($_ =~ /^MAXTEMP=\s*(.*)\s*=\s*(.*)\s*$/) { $afcmaxtemp{$1} = $2; next; } + elsif ($_ =~ /^MINSTART=\s*(.*)\s*=\s*(.*)\s*$/) { $afcminstart{$1} = $2; next; } + elsif ($_ =~ /^MINSTOP=\s*(.*)\s*=\s*(.*)\s*$/) { $afcminstop{$1} = $2; next; } } close(F); unless (defined($interval)) - { die("Some settings missing ..."); } + { die("Interval setting missing ..."); } + if ($interval < 1 ) + { die("Interval must be minimum 1"); } print("\nCommon settings:\n"); print(" INTERVAL=$interval\n"); - my $fcvcount = 0; - foreach my $fcv (split(/\s+/, $fctemps)) + foreach my $pwm (sort(keys %afctemp)) { - ($afcpwm[$fcvcount], $afctemp[$fcvcount]) = split(/=/, $fcv); - - $fcfans =~ s/^\S*=(\S+)\s*//; $afcfan[$fcvcount] = $1; - $mintemp =~ s/^\S*=(\S+)\s*//; $afcmintemp[$fcvcount] = $1; - $maxtemp =~ s/^\S*=(\S+)\s*//; $afcmaxtemp[$fcvcount] = $1; - $minstart =~ s/^\S*=(\S+)\s*//; $afcminstart[$fcvcount] = $1; - $minstop =~ s/^\S*=(\S+)\s*//; $afcminstop[$fcvcount] = $1; - - print("\nSettings for $afcpwm[$fcvcount]:\n"); - print(" Depends on $afctemp[$fcvcount]\n"); - print(" Controls $afcfan[$fcvcount]\n"); - print(" MINTEMP = $afcmintemp[$fcvcount]\n"); - print(" MAXTEMP = $afcmaxtemp[$fcvcount]\n"); - print(" MINSTART = $afcminstart[$fcvcount]\n"); - print(" MINSTOP = $afcminstop[$fcvcount]\n"); - - $fcvcount++; + print("\nSettings for $pwm:\n"); + print(" Depends on $afctemp{$pwm}\n"); + print(" Controls $afcfans{$pwm}\n"); + print(" MINTEMP = $afcmintemp{$pwm}\n"); + print(" MAXTEMP = $afcmaxtemp{$pwm}\n"); + print(" MINSTART = $afcminstart{$pwm}\n"); + print(" MINSTOP = $afcminstop{$pwm}\n"); + + if ( lc($afcfans{$pwm}) eq "none" ) + { $afcfans{$pwm} = "none" } + if ($afcmintemp{$pwm} < 0 ) + { die("MINTEMP must be positive"); } + if ($afcmaxtemp{$pwm} < 0 ) + { die("MAXTEMP must be positive"); } + if ($afcmintemp{$pwm} > $afcmaxtemp{$pwm} ) + { die("MINTEMP must be less than MAXTEMP"); } + if ($afcminstart{$pwm} < 0 ) + { die("MINSTART must be positive"); } + if ($afcminstop{$pwm} < 0 ) + { die("MINSTOP must be positive"); } + if ($afcminstart{$pwm} > 255 ) + { die("MINSTART must be less than 255"); } + if ($afcminstop{$pwm} > 255 ) + { die("MINSTOP must be less than 255"); } } }
@@ -302,14 +380,12 @@ $SIG{QUIT} = 'IGNORE'; print("Aborting, restoring fans...\n"); - my $fcvcount = 0; - - while ( $fcvcount < $#afcpwm+1) + our %afctemp; + foreach my $pwmo (keys %afctemp) { - my $pwmo = $afcpwm[$fcvcount]; - &pwmdisable($afcpwm[$fcvcount]); - $fcvcount++; + &pwmdisable($pwmo); } + unlink PIDFILE; print("Verify fans have returned to full speed\n"); POSIX:_exit(-1); }
@@ -318,32 +394,61 @@ ############################################################ sub UpdateFanSpeeds() { - my $fcvcount = 0; - - while ($fcvcount < $#afcpwm+1) + our (%afctemp, %afcfans, %afcmintemp, %afcmaxtemp, %afcminstart, %afcminstop); + my %sensres; # cache for this run + foreach my $pwmo (keys %afctemp) { - my $pwmo = $afcpwm[$fcvcount]; - my $tsens = $afctemp[$fcvcount]; - my $fan = $afcfan[$fcvcount]; - my $mint = $afcmintemp[$fcvcount]; - my $maxt = $afcmaxtemp[$fcvcount]; - my $minsa = $afcminstart[$fcvcount]; - my $minso = $afcminstop[$fcvcount]; + my @tsens = split(/\+/, $afctemp{$pwmo}); + my $fan = $afcfans{$pwmo}; + my $mint = $afcmintemp{$pwmo}; + my $maxt = $afcmaxtemp{$pwmo}; + my $minsa = $afcminstart{$pwmo}; + my $minso = $afcminstop{$pwmo}; ### tval my $tval = 0; - if (open(F, "$dir/$tsens")) + my $tvals = 0; + foreach my $sens (@tsens) + { + my $val = 0; + if ( defined $sensres{$sens} ) { + $val = $sensres{$sens}; + } + else { - $tval = <F>; - close(F); + if ( $sens =~ /^script:\s*(.*)\s*$/ ) + { + my $prog = $1; + if (!(-x $prog)) { die("$prog must be executeable"); } + open(F, "$prog |") || die "can't fork $prog: $!"; + $val = <F>; + close F; + $val = int($val); + } + elsif ( $sens =~ /^file:\s*(.*)\s*$/ ) + { + if (! open(F, "$1")) { die("Error reading temperature from $sens"); } + $val = <F>; + close F; + $val = int($val); + } + else + { + if (! open(F, "$dir/$sens")) { die("Error reading temperature from $dir/$sens"); } + $val = <F>; + close(F); + $val =~ /([.\d]+)\s*$/; + $val = int($1); + if ($sysfs == 1) { $val /= 1000; } + } + $sensres{$sens} = $val; } - else - { die("Error reading temperature from $dir/$tsens"); } - $tval =~ /([.\d]+)\s*$/; - $tval = int($1); - if ($sysfs == 1) - { $tval /= 1000; } - + $tval += $val; + $tvals++; + if (DEBUG == 1) { print("$sens => $val. sum=$tval ($tvals)\n"); } + } + + $tval /= $tvals; ### pwmpval my $pwmpval = 0; if (open(F, "$dir/$pwmo"))
@@ -357,21 +462,27 @@ ### fanval my $fanval = 0; - if (open(F, "$dir/$fan")) - { - $fanval = <F>; - close(F); - } + if ($fan eq "none") + { + $fanval = 1; + } else - { die("Error reading Fan value from $dir/$fan"); } - $fanval =~ /(\d+)\s$/; - $fanval = $1; - + { + if (open(F, "$dir/$fan")) + { + $fanval = <F>; + close(F); + } + else + { die("Error reading Fan value from $dir/$fan"); } + $fanval =~ /(\d+)\s$/; + $fanval = $1; + } ### DEBUG if (DEBUG == 1) { print("pwmo=$pwmo\n"); - print("tsens=$tsens\n"); + print("tsens=$afctemp{$pwmo}\n"); print("fan=$fan\n"); print("mint=$mint\n"); print("maxt=$maxt\n");
@@ -390,8 +501,15 @@ { $pwmval = MAX; } else { - $pwmval = eval ( ($tval - $mint) / ($maxt - $mint) ); - $pwmval *= (255 - $minso); + if( ($maxt - $mint) == 0 ) { + $pwmval = 0; + } + elsif( ($tval - $mint) == 0 ) { + $pwmval = 255; + } else { + $pwmval = ($tval - $mint) / ($maxt - $mint); + $pwmval *= (255 - $minso); + } $pwmval += $minso; $pwmval = int($pwmval); if ( ($pwmval == 0) || ($fanval == 0) )
@@ -417,8 +535,6 @@ if (DEBUG == 1) { print("new pwmval = $pwmval\n"); } - - $fcvcount++; } }
#!/usr/bin/perl -wT
# $Id: fancontrol.pl 4588 2007-07-08 10:01:50Z khali $
#
# Perl script for temperature dependent fan speed control.
#
# Copyright 2009 Sonni Norlov <[email protected]>
#
# Modified to work on the average of multiple inputs. Which enables control
# of one fan using multiple temperatur sensors as input. Ex. as found on # Intels multicore CPUs which has a sensor per core and usually one fan. # CONFIGFILE Ex: # FCTEMPS=hwmon0/device/pwm1=hwmon1/device/temp1_input + hwmon2/device/temp1_input # # Changed how multiple fans are configured. Max one pwm per option (ex. FCTEMPS) # but multiple options of same type per file. # Added configuration file checks. # FCFANS are now optional, so fans with no rpm readout are not boosted to # MINSTART in each interval when they should be running. # Added removed of pid file on exit/stop. # Added creation of log file directory and redirected all logging to log file # in daemon mode. # # Added support for reading temperature from program. Program must output # an integer value on stdout. # CONFIGFILE Ex: # FCTEMPS=hwmon0/device/pwm1=script:/usr/sbin/nvidia_gpu0_coretemp + hwmon2/device/temp1_input # # Added support for reading temperature from a file. File must contain # an integer value on the first line. # CONFIGFILE Ex: # FCTEMPS=hwmon0/device/pwm1=file:/tmp/nvidia_gpu0_coretemp + hwmon2/device/temp1_input # # Copyright 2004 dean takemori <[email protected]> # # This is a reimplementation in perl of Marius Reiner's bash script for # fan speed control. It has advantages in that it can daemonize itself # and needn't spawn subprocesses for grep, sleep etc. Much of the structure # of the bash script is preserved to make mirroring changes easier, so # this is seriously non-idiomatic perl but at the same time it should not # be considered a a direct bash to perl translation. # # Usage: fancontrol [CONFIGFILE] # # For configuration instructions and warnings please see fancontrol.txt, # which can be found in the doc/ directory or at the website mentioned # elsewhere. # # This script is derived from Marius Reiner's bash version, so it is # hereby placed under the GPL. # # Copyright 2003 Marius Reiner <[email protected]> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. #
use warnings;
use strict;
use IO::Handle;
use Getopt::Std;
use POSIX;
use File::Basename;
$ENV{PATH} = "/bin:/usr/bin";
##### Configuration #####
use constant DEBUG => 0;
use constant MAX => 255;
use constant PIDFILE => '/var/run/fancontrol.pid'; use constant CONFFILE => '/etc/fancontrol';
use constant LOGFILE => '/var/log/fancontrol/fancontrol.log'; use constant ERRFILE => '/var/log/fancontrol/fancontrol.err'; ### End Configuration ###
our $interval;
our @afctemp;
our @afcfans;
our @afcmaxtemp;
our @afcmintemp;
our @afcminstart;
our @afcminstop;
sub loadconfig($);
sub pwmdisable($);
sub pwmenable($);
sub restorefans();
sub calc(@);
sub UpdateFanSpeeds();
END { restorefans(); }
our $sysfs = 0;
our $dir = '/proc/sys/dev/sensors';
### Best guees of the interface?
if (!(-d $dir))
{
my $predir = '/sys/class/hwmon';
if ((-d $predir))
{
$sysfs = 1;
}
else
{
$predir = '/sys/bus/i2c/devices';
if ((-d $predir))
{
$sysfs = 1;
} }
}
our $opt_d;
getopts('d');
### Daemonize
if ( defined($opt_d) && ($opt_d == 1) )
{
my $pid = fork;
POSIX::_exit(0) if $pid;
unless (defined($pid))
{ die("Couldn't fork: $!"); }
my $logdir = dirname(ERRFILE);
if (!(-d $logdir))
{ mkdir($logdir); }
$logdir = dirname(LOGFILE);
if (!(-d $logdir))
{ mkdir($logdir); }
open(*STDERR, '>', ERRFILE);
IO::Handle::autoflush(*STDERR);
open(*STDOUT, '>>', LOGFILE);
IO::Handle::autoflush(*STDOUT);
unless (POSIX::setsid())
{ die("Couldn't open new session: $!"); }}
my $config = shift;
if (defined($config))
{ loadconfig($config); }
else
{ loadconfig(CONFFILE); }
### Pidfile
if (open(FILE, ">" . PIDFILE))
{
print(FILE "$$\n");
close(FILE); }
else
{ print(PIDFILE . ": $!\n"); }
### What kind of interface?
our %afctemp;
if (!(-d $dir))
{
my @pwms = keys %afctemp;
if ($pwms[0] =~ m/^hwmon\d/)
{
$dir = '/sys/class/hwmon';
}
else
{
$dir = '/sys/bus/i2c/devices';
}
if (!(-d $dir))
{ die("No sensors found! (are the necessary modules loaded?) :
$!\n"); }
else
{
$sysfs = 1;
}}
### Trap signals
$SIG{TERM} = \&restorefans;
$SIG{HUP} = \&restorefans;
$SIG{INT} = \&restorefans;
$SIG{QUIT} = \&restorefans;
### Enable PWMprint("Enabling PWM on fans...\n");
foreach my $pwmo (sort(keys %afctemp))
{
unless (pwmenable($pwmo))
{ die("Error enabling PWM on $dir/$pwmo : $!\n"); }}
print("Starting automatic fan control...\n");
while(1)
{
UpdateFanSpeeds();
sleep($interval);}
1;
################################################################
sub loadconfig($)
{
my $file = shift;
print("Loading configuration from $file ...\n");
unless ( (-e $file) && (-r $file) )
{ die("Unable to read config file $file: $!"); } open(F, $file);
# config file format: key=pwm=value
our ($interval, %afctemp, %afcfans, %afcmintemp, %afcmaxtemp, %afcminstart, %afcminstop);
while($_ = <F>)
{
if ($_ =~ /^\s+$/) { next; }
elsif ($_ =~ /^INTERVAL=\s*(.*)$/) { $interval = $1; next; }
elsif ($_ =~ /^FCTEMPS=\s*(.*)\s*=\s*(.*)\s*$/) {
my $pwm = $1;
my $in = $2;
# strip whitespaces between temps
foreach my $tmp (split(/\+/, $in))
{
$tmp =~ s/^\s+//;
$tmp =~ s/\s+$//;
if( $tmp ne "" ) {
if (defined($afctemp{$pwm}) ) {
$afctemp{$pwm} .= "+";
}
$afctemp{$pwm} .= $tmp;
}
}
$afcfans{$pwm} = "none";
$afcmintemp{$pwm} = 0;
$afcmaxtemp{$pwm} = 0;
$afcminstart{$pwm} = 255;
$afcminstop{$pwm} = 255;
next;
}
elsif ($_ =~ /^FCFANS=\s*(.*)\s*=\s*(.*)\s*$/) { $afcfans{$1} = $2; next; }
elsif ($_ =~ /^MINTEMP=\s*(.*)\s*=\s*(.*)\s*$/) { $afcmintemp{$1} = $2; next; }
elsif ($_ =~ /^MAXTEMP=\s*(.*)\s*=\s*(.*)\s*$/) { $afcmaxtemp{$1} = $2; next; }
elsif ($_ =~ /^MINSTART=\s*(.*)\s*=\s*(.*)\s*$/) { $afcminstart{$1} = $2; next; }
elsif ($_ =~ /^MINSTOP=\s*(.*)\s*=\s*(.*)\s*$/) { $afcminstop{$1} = $2; next; }
}
close(F);unless (defined($interval))
{ die("Interval setting missing ..."); } if ($interval < 1 )
{ die("Interval must be minimum 1"); }
print("\nCommon settings:\n");
print(" INTERVAL=$interval\n");
foreach my $pwm (sort(keys %afctemp))
{
print("\nSettings for $pwm:\n");
print(" Depends on $afctemp{$pwm}\n");
print(" Controls $afcfans{$pwm}\n");
print(" MINTEMP = $afcmintemp{$pwm}\n");
print(" MAXTEMP = $afcmaxtemp{$pwm}\n");
print(" MINSTART = $afcminstart{$pwm}\n");
print(" MINSTOP = $afcminstop{$pwm}\n"); if ( lc($afcfans{$pwm}) eq "none" )
{ $afcfans{$pwm} = "none" }
if ($afcmintemp{$pwm} < 0 )
{ die("MINTEMP must be positive"); }
if ($afcmaxtemp{$pwm} < 0 )
{ die("MAXTEMP must be positive"); }
if ($afcmintemp{$pwm} > $afcmaxtemp{$pwm} )
{ die("MINTEMP must be less than MAXTEMP"); }
if ($afcminstart{$pwm} < 0 )
{ die("MINSTART must be positive"); }
if ($afcminstop{$pwm} < 0 )
{ die("MINSTOP must be positive"); }
if ($afcminstart{$pwm} > 255 )
{ die("MINSTART must be less than 255"); }
if ($afcminstop{$pwm} > 255 )
{ die("MINSTOP must be less than 255"); }
}
}################################################################
sub pwmdisable($)
{
my $p = shift;
if ($sysfs == 1)
{
if (open(F, ">$dir/$p"))
{
print(F MAX . '\n');
close(F);
}
else
{ die("$dir/$p : $!"); } my $enable = "$dir/${p}_enable";
if (-f $enable)
{
if (open(F, ">$enable"))
{
print(F '0');
close(F);
}
else
{ die("$enable : $!"); }
}
}
else
{
if (open(F, ">$dir/$p"))
{
print(F MAX . ' 0');
close(F);
}
else
{ die("$dir/$p : $!"); }
} return(1);
}
#################################################################
sub pwmenable($)
{
my $p = shift;
if ($sysfs == 1)
{
my $enable = "$dir/${p}_enable";
if (-f $enable)
{
if (open(F, ">$enable"))
{
print(F "1\n");
close(F);
}
else
{ die("$enable : $!\n"); }
}
}
else
{
if (open(F, ">$dir/$p"))
{
print(F MAX . " 1\n");
close(F);
}
else
{ die("$dir/$p : $!\n"); }
} return(1);
}
################################################################
sub restorefans()
{
$SIG{TERM} = 'IGNORE';
$SIG{HUP} = 'IGNORE';
$SIG{INT} = 'IGNORE';
$SIG{QUIT} = 'IGNORE';
print("Aborting, restoring fans...\n");
our %afctemp;
foreach my $pwmo (keys %afctemp)
{
&pwmdisable($pwmo);
} unlink PIDFILE;
print("Verify fans have returned to full speed\n"); POSIX:_exit(-1);
}
############################################################
sub UpdateFanSpeeds()
{
our (%afctemp, %afcfans, %afcmintemp, %afcmaxtemp, %afcminstart, %afcminstop); my %sensres; # cache for this run
foreach my $pwmo (keys %afctemp)
{
my @tsens = split(/\+/, $afctemp{$pwmo});
my $fan = $afcfans{$pwmo};
my $mint = $afcmintemp{$pwmo};
my $maxt = $afcmaxtemp{$pwmo};
my $minsa = $afcminstart{$pwmo};
my $minso = $afcminstop{$pwmo};
### tval
my $tval = 0;
my $tvals = 0;
foreach my $sens (@tsens)
{
my $val = 0;
if ( defined $sensres{$sens} ) {
$val = $sensres{$sens};
}
else
{
if ( $sens =~ /^script:\s*(.*)\s*$/ )
{
my $prog = $1;
if (!(-x $prog)) { die("$prog must be executeable"); }
open(F, "$prog |") || die "can't fork $prog: $!";
$val = <F>;
close F;
$val = int($val);
}
elsif ( $sens =~ /^file:\s*(.*)\s*$/ )
{
if (! open(F, "$1")) { die("Error reading temperature from $sens"); }
$val = <F>;
close F;
$val = int($val);
}
else
{
if (! open(F, "$dir/$sens")) { die("Error reading temperature from $dir/$sens"); }
$val = <F>;
close(F);
$val =~ /([.\d]+)\s*$/;
$val = int($1);
if ($sysfs == 1) { $val /= 1000; }
}
$sensres{$sens} = $val;
}
$tval += $val;
$tvals++;
if (DEBUG == 1) { print("$sens => $val. sum=$tval ($tvals)\n"); }
}
$tval /= $tvals;
### pwmpval
my $pwmpval = 0;
if (open(F, "$dir/$pwmo"))
{
$pwmpval = <F>;
close(F);
}
else
{ die("Error reading PWM value from $dir/$pwmo"); }
($pwmpval) = split(/\s/, $pwmpval);
### fanval
my $fanval = 0;
if ($fan eq "none")
{
$fanval = 1;
}
else
{
if (open(F, "$dir/$fan"))
{
$fanval = <F>;
close(F);
}
else
{ die("Error reading Fan value from $dir/$fan"); }
$fanval =~ /(\d+)\s$/;
$fanval = $1;
}
### DEBUG
if (DEBUG == 1)
{
print("pwmo=$pwmo\n");
print("tsens=$afctemp{$pwmo}\n");
print("fan=$fan\n");
print("mint=$mint\n");
print("maxt=$maxt\n");
print("minsa=$minsa\n");
print("minso=$minso\n");
print("tval=$tval\n");
print("pwmpval=$pwmpval\n");
print("fanval=$fanval\n");
print("\n");
}
my $pwmval;
if ($tval <= $mint)
{ $pwmval = 0; }
elsif ($tval >= $maxt)
{ $pwmval = MAX; }
else
{
if( ($maxt - $mint) == 0 ) {
$pwmval = 0;
}
elsif( ($tval - $mint) == 0 ) {
$pwmval = 255;
} else {
$pwmval = ($tval - $mint) / ($maxt - $mint);
$pwmval *= (255 - $minso);
}
$pwmval += $minso;
$pwmval = int($pwmval);
if ( ($pwmval == 0) || ($fanval == 0) )
{
if (open(F, ">$dir/$pwmo"))
{
print(F "$minsa\n");
close(F);
}
else
{ die("Error writing PWM value to $dir/$pwmo : $!\n"); }
sleep 1;
}
}
if (open(F, ">$dir/$pwmo"))
{
print(F "$pwmval\n");
close(F);
}
else
{ die("Error writing PWM value to $dir/$pwmo : $!\n"); }
if (DEBUG == 1)
{ print("new pwmval = $pwmval\n"); }
}}
1;
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors