diff --git a/app/Command/Daemon.php b/app/Command/Daemon.php index 06fc36715f5fee73228c6c0789f49547b042b757..06af7bcae56a42868fb75a5e9eb46bc36f821690 100644 --- a/app/Command/Daemon.php +++ b/app/Command/Daemon.php @@ -8,6 +8,8 @@ use Hugga\Console; class Daemon extends AbstractCommand { + const PID_PATH = '/run/fan-ctrl.pid'; + protected $name = 'daemon'; protected $description = 'Run the monitoring and control the fans'; @@ -23,6 +25,13 @@ class Daemon extends AbstractCommand $this->warn('This programm has to run as root'); return 1; } + + if (file_exists(self::PID_PATH) && posix_getpgid((int)file_get_contents(self::PID_PATH))) { + $this->warn('Fan Control is already running'); + return 1; + } + file_put_contents(self::PID_PATH, posix_getpid()); + $fanConfig = $this->app->fanConfig; try { $fanConfig->readConfig(); diff --git a/composer.json b/composer.json index d98f179a80341798e01b6ea812d4f47f889ff9dc..7ccc20cb29e7fe9446ab955b719d97fdb3186111 100644 --- a/composer.json +++ b/composer.json @@ -7,9 +7,10 @@ "ulrichsg/getopt-php": "^4.0", "filp/whoops": "^2.14", "monolog/monolog": "^1.9", + "mossadal/math-parser": "^1.3", "ext-pcntl": "*", - "php": ">=7.4", - "mossadal/math-parser": "^1.3" + "ext-posix": "*", + "php": ">=7.4" }, "autoload": { "psr-4": { diff --git a/example-config.php b/example-config.php new file mode 100644 index 0000000000000000000000000000000000000000..8c521ef735fd2623e7429a0657d7dfe58a4f5475 --- /dev/null +++ b/example-config.php @@ -0,0 +1,114 @@ +<?php + +return [ + 'fans' => [ + [ + 'name' => 'exhaust fans', + 'type' => 'App\\Model\\Fan\\HwmonFan', + 'options' => [ + 'hwmon' => 'nct6798', + 'fan' => 'fan1', + 'pwm' => 'pwm1', + 'start' => 70, + 'max' => 200, + ], + ], + [ + 'name' => 'cpu fan', + 'type' => 'App\\Model\\Fan\\HwmonFan', + 'options' => [ + 'hwmon' => 'nct6798', + 'fan' => 'fan2', + 'pwm' => 'pwm2', + 'start' => 40, + 'max' => 255, + ], + ], + [ + 'name' => 'radiator fans', + 'type' => 'App\\Model\\Fan\\HwmonFan', + 'options' => [ + 'hwmon' => 'nct6798', + 'fan' => 'fan3', + 'pwm' => 'pwm3', + 'start' => 60, + 'max' => 255, + ], + ], + [ + 'name' => 'bottom fans', + 'type' => 'App\\Model\\Fan\\HwmonFan', + 'options' => [ + 'hwmon' => 'nct6798', + 'fan' => 'fan4', + 'pwm' => 'pwm4', + 'start' => 65, + 'max' => 160, + ], + ], + [ + 'name' => 'water pump', + 'type' => 'App\\Model\\Fan\\HwmonFan', + 'options' => [ + 'hwmon' => 'nct6798', + 'fan' => 'fan6', + 'pwm' => 'pwm6', + 'start' => 20, + 'max' => 128, + ], + ], + ], + 'sensors' => [ + [ + 'name' => 'cpu', + 'type' => 'App\\Model\\Sensor\\HwmonSensor', + 'options' => [ + 'hwmon' => 'k10temp', + 'temp' => 'temp1', + ], + ], + [ + 'name' => 'gpu', + 'type' => 'App\\Model\\Sensor\\CommandSensor', + 'options' => [ + 'command' => 'nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader', + 'conversion' => NULL, + ], + ], + ], + 'rules' => [ + [ + 'fans' => [ + 'exhaust fans', + 'cpu fan', + 'bottom fans', + ], + 'sensor' => 'cpu', + 'type' => 'App\\Model\\Rule\\CurveRule', + 'options' => [ + 'alwaysOn' => true, + 'points' => [ + 0 => 55, + 60 => 80, + 100 => 90, + ], + ], + ], + [ + 'fans' => [ + 'radiator fans', + 'water pump', + ], + 'sensor' => 'gpu', + 'type' => 'App\\Model\\Rule\\CurveRule', + 'options' => [ + 'alwaysOn' => true, + 'points' => [ + 0 => 35, + 60 => 55, + 100 => 90, + ], + ], + ], + ], +]; diff --git a/install.sh b/install.sh new file mode 100755 index 0000000000000000000000000000000000000000..63b5f2b1ec17c97dcdeda4728a0af5b3df168873 --- /dev/null +++ b/install.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +set -e + +installTarget=/usr/share/fan-ctrl-php + +# check for php and composer +if ! which php > /dev/null 2>&1 || ! which composer > /dev/null 2>&1; then + echo 'php and composer have to be installed in order to run fan-ctrl-php' + exit 1; +fi + +# copy app, bin, composer.json, composer.lock to /usr/share/fan-ctrl-php +mkdir -p $installTarget +cp -r app bin composer.json composer.lock $installTarget + +# install composer dependencies +pushd $installTarget +composer install -no +popd + +# create symlink to bin/fan-ctrl in /usr/bin/ +ln -s $installTarget/bin/fan-ctrl /usr/bin/fan-ctrl + +# install fan-ctrl.service in /etc/systemd/system/ +{ + echo '[Unit]' + echo 'Description=A fan control service written in php' + echo '' + echo '[Service]' + echo 'User=root' + echo 'WorkingDirectory='$installTarget + echo 'ExecStart=/usr/bin/fan-ctrl daemon' + echo 'Restart=always' + echo '' + echo '[Install]' + echo 'WantedBy=multi-user.target' +} > /etc/systemd/system/fan-ctrl.service + +echo Fan Control PHP successfully installed. Enable the service with \$ systemctl enable fan-ctrl.service +if ! test -f /etc/fan-ctrl.php; then + echo To create a configuration file and test the fans use \$ fan-ctrl config +fi