UPS service and timer


wurmfood
 Share

0

Recommended Posts

While I like having the helios-ups.timer in case of power failure,  I don't like that my logs get three lines written to them every 20 seconds.

Apr 09 07:43:26 helios64 systemd[1]: Starting Helios64 UPS Action...
Apr 09 07:43:26 helios64 systemd[1]: helios64-ups.service: Succeeded.
Apr 09 07:43:26 helios64 systemd[1]: Finished Helios64 UPS Action.

Does anyone know if there is a way to keep the timer on but not fill the logs this way?

 

Everything I've found about silencing systemd messages is about the output of the command, not the systemd activity itself.

Link to post
Share on other sites

Armbian is a community driven open source project. Do you like to contribute your code?

@wurmfood

On my system the helios64-ups.timer is disabled by default.

Is this a timer that has to be manually enabled ?? I thought that the Helios would check if it had the battery and activate this automagically.

It might be better to have a daemon in the background that checked the battery value every 20 seconds and only printed out output when there was a change.

Link to post
Share on other sites

@SIGSEGV

It is disabled by default, but since I had some time the last few days I've been digging into things and came across this. There's a corresponding service, but all it does is check to see the state of the battery and then exit. If something else is monitoring the battery, I don't know what it is.

 

The service could be rewritten as a script that has an loop that checks the state of the battery every 20 seconds and then sleeps.

Link to post
Share on other sites

Can maybe disable the service and put the script it is running, /usr/bin/helios64-ups.sh to root cron for every minute. You could make it run every 20 seconds by adding a while loop that goes 3 turns and sleeps 20 seconds in each turn.

 

However, I don't see where this script checks to see if mains power is active. What I see is a script that will continually shutdown the device if the battery voltage is low even when the mains returns. Is that function provided by the service timer, as in the timer stops when there is mains?

If so absolutely do NOT cron helios64-ups.sh as it is. Because you won't be able to boot the device until the battery charges over 7 volts.

 

#!/bin/bash

#7.0V   916     Recommended threshold to force shutdown system
TH=916

val=$(cat '/sys/bus/iio/devices/iio:device0/in_voltage2_raw')
sca=$(cat '/sys/bus/iio/devices/iio:device0/in_voltage_scale')
adc=$(echo "$val * $sca / 1" | bc)

if [ "$adc" -le $TH ]; then
    /usr/sbin/poweroff
fi

 

There is a whole device with lots of data in there, is there any documentation on iio:device0?

 

edit- apparently there is :D https://wiki.kobol.io/helios64/ups/#ups-status-under-linux

 

Edited by clostro
Link to post
Share on other sites

Posted (edited)

While something that sleeps for 20 seconds might be better, I've set up the following script in cron to go off every minute.

 

<removed>

My crontab looks like:

<removed>

I've tested this and it seems to work. The pipe to systemd-cat lets this log to the system journal at a set warn level.

 

-- Edit: Removed extra stuff here to clean things up a little. This didn't work the way I wanted.

Edited by wurmfood
Didn't actually solve the problem.
Link to post
Share on other sites

Sigh. Except that doesn't solve the problem. Now it's just cron filling up the log.

 

New solution, using the sleep option. Modified helio64-ups.service:

[Unit]
Description=Helios64 UPS Action

[Install]
WantedBy=multi-user.target

[Service]
#Type=oneshot
#ExecStart=/usr/bin/helios64-ups.sh
Type=simple
ExecStart=/usr/local/sbin/powermon.sh

 

Modified powermon.sh:

#!/bin/bash

#7.0V   916     Recommended threshold to force shutdown system
TH=916

# Values can be info, warning, emerg
warnlevel="emerg"

while [ : ]
do
        main_power=$(cat '/sys/class/power_supply/gpio-charger/online')
        # Only use for testing:
        # main_power=0

        if [ "$main_power" == 0 ]; then
                val=$(cat '/sys/bus/iio/devices/iio:device0/in_voltage2_raw')
                sca=$(cat '/sys/bus/iio/devices/iio:device0/in_voltage_scale')
                # The division is required to make the test later work.
                adc=$(echo "$val * $sca /1" | bc)

                echo "Main power lost. Current charge: $adc" | systemd-cat -p $warnlevel
                echo "Shutdown at $TH" | systemd-cat -p $warnlevel

                # Uncomment for testing
                # echo "Current values:"
                # echo -e "\tMain Power = $main_power"
                # echo -e "\tRaw Voltage = $val"
                # echo -e "\tVoltage Scale = $sca"
                # echo -e "\tVoltage = $adc"

                if [ "$adc" -le $TH ]; then
                    echo "Critical power level reached. Powering off." | systemd-cat -p $warnlevel
                    /usr/sbin/poweroff
                fi
        fi
        sleep 20
done

 

Link to post
Share on other sites

On 4/10/2021 at 1:44 PM, clostro said:

However, I don't see where this script checks to see if mains power is active. What I see is a script that will continually shutdown the device if the battery voltage is low even when the mains returns. Is that function provided by the service timer, as in the timer stops when there is mains?

@SIGSEGV @clostro the service is triggered by udev rules.

 

@wurmfood, I didn't realize the timer fill the the log every 20s. Initial idea was one time timer, poweroff after 10m of power loss event. Then it was improved to add polling of the battery level and poweroff when threshold reached. Your script look good, we are considering to adapt it to official release. Thank you

 

 

Link to post
Share on other sites

 Share

0