Files
misc/bin/st-power
2026-01-11 00:22:26 -08:00

37 lines
1.2 KiB
Bash
Executable File

#!/bin/sh
# for UPS monitoring with NUT
#
DATA_CMD="upsc ups@localhost"
# on debian and freebsd, ups.realpower is not available.
# calculate load in watts with load percent and max output instead.
MAX_OUTPUT=$($DATA_CMD | grep ups.realpower.nominal: | awk '{print $2}')
LOAD_VALUE=$($DATA_CMD | grep ups.load: | awk '{print $2}')
LOAD_PERCENT=$(echo "scale=2; $LOAD_VALUE/100" | bc)
POWER=$(echo "scale=0; $LOAD_PERCENT*$MAX_OUTPUT" | bc)
# "round" to integer. just removes decimal and whatevers after.
POWER="${POWER%.*} W"
CHARGE="$($DATA_CMD | grep battery.charge: | awk '{print $2}')%"
if [ "$CHARGE" = "100%" ]; then
echo $POWER
else
echo "$CHARGE $POWER"
fi
# for battery monitoring everywhere else
#
# if power_now is not present, calculate it:
#VOLTAGE=$(cat /sys/class/power_supply/BAT*/voltage_now)
#CURRENT=$(cat /sys/class/power_supply/BAT*/current_now)
#POWER=$(echo "scale=2; $VOLTAGE*$CURRENT/1000000000000" | bc)
# if it is present, just divide it to watts (from micro watts on thinkpads, perhaps something else on other platforms)
#POWER=$(cat /sys/class/power_supply/BAT*/power_now)
#POWER="$(echo "scale=2; $POWER/1000000" | bc)W"
#CHARGE="$(cat /sys/class/power_supply/BAT*/capacity)%"
#echo "$CHARGE $POWER"