Dear Maintainer,

The workaround I previously proposed does not really solves the
problem, because of course CPU temperature is not necessarily
related to that of hard disks.

I now reconfigure /etc/thinkfan.conf automatically each time. To do
that I added an ExecStartPre entry to thinkfan.service to load the
script here attached for completeness. It seems to work fine, but
please note that i) the script may be fine tuned to my machine, ii) I
am guessing that the thinkfan service will always start after that the
hwmon devices have been determined, ii) I have no expertise with init
scripts.

Best,
Francesco
#!/usr/bin/perl

# thinkfan-hwmon - automatic /etc/thinkfan.conf configuration
#
# Copyright (C) 2017 Francesco Montanari <francesco.montan...@openmailbox.org>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
#
# Description
# -----------
# Automatically read hwmon sensors devices. Remove any previous
# 'hwmon /sys/devices/...'  entry from /etc/thinkfan.conf, and replace
# them by the newly detected ones.
#
# The script can be placed, e.g., under /usr/sbin/ and be called by
# thinkfan systemd service by adding the line
#   ExecStartPre=/usr/sbin/thinkfan-hwmon
# to the file /lib/systemd/system/thinkfan.service.
#
# Depending on the machine, the excluded folder name 'pci0000:00' may
# need to be adapted in this script.


use strict;
use warnings;
use File::Find::Rule;


# Where to look for the sensors
my $searchdir = "/sys/devices/";

# Exclude from the search those paths containing the following
# expression. This may change for different machines.
my $exclude = "pci0000:00";

my @cpu = read_sensors($searchdir,$exclude);

write_conf(@cpu);


sub read_sensors {
    # Read the location of hwmon devices.

    my @cpu = File::Find::Rule->file()
        ->name("temp*_input")
        ->in($searchdir);

    my @del_indexes = grep { $cpu[$_] =~ m/\Q$exclude/ } 0..$#cpu;
    for (@del_indexes){
        splice(@cpu,$_,1);
    }

    for (@cpu)
    {
        $_ = "hwmon " . $_ . "\n";
    }

    return @cpu;
}


sub write_conf {
    # Replace the sensors in the configuration file.

    my $filename = '/etc/thinkfan.conf';

    open my $in, '<', $filename or die "Could not open file '$filename' $!";
    my @contents = <$in>;
    close $in;

    @contents = grep {!/hwmon \/sys\/device/} @contents;

    open my $out, '>', $filename or die "Could not open file '$filename' $!";
    print $out @contents;
    print $out @cpu;
    close $out;
}

Reply via email to