49 lines
1.5 KiB
Perl
49 lines
1.5 KiB
Perl
|
#!/usr/bin/env perl
|
||
|
$| = 1;
|
||
|
|
||
|
my $CRITICAL = undef;
|
||
|
my $LOW = undef;
|
||
|
my $CHARGING = undef;
|
||
|
my $DEAD_LEVEL = 2;
|
||
|
my $CRITICAL_LEVEL = 5;
|
||
|
my $LOW_LEVEL = 10;
|
||
|
|
||
|
while (1) {
|
||
|
my @acpi = split " ", `acpi | grep "Discharging" | grep -v "rate information"`;
|
||
|
my $battery_level = $acpi[3];
|
||
|
my $status = $acpi[2];
|
||
|
my $sent = undef;
|
||
|
$battery_level =~ s/%,//g;
|
||
|
$status =~ s/,//g;
|
||
|
|
||
|
if ($status =~ "Discharging") {
|
||
|
$CHARGING = undef;
|
||
|
if (!$CRITICAL) {
|
||
|
if (int($battery_level) <= int($CRITICAL_LEVEL)) {
|
||
|
$CRITICAL = 'def';
|
||
|
$sent = 'def';
|
||
|
system "notify-send -i \"battery-empty\" -t 0 -u critical \"BATTERY CRITICAL\" \"Battery level is ${battery_level}%\n\nCharge the system NOW.\""
|
||
|
}
|
||
|
} if (!$LOW && !$sent) {
|
||
|
if (int($battery_level) <= int($LOW_LEVEL)) {
|
||
|
$LOW = 'def';
|
||
|
$sent = 'def';
|
||
|
system "notify-send -i \"battery-caution\" -t 0 -u normal \"BATTERY LOW\" \"Battery level is ${battery_level}%\n\nCharge the system soon.\""
|
||
|
}
|
||
|
} if (int($battery_level) <= int($DEAD_LEVEL)) {
|
||
|
system "notify-send -t 0 -u critical \"SHUTTING DOWN\" \"Battery level is too low. The system will shutdown in 2 minutes to prevent corruption.\n\nCharge the system NOW to cancel the shutdown.\"";
|
||
|
system "doas shutdown -Ph 2 &";
|
||
|
}
|
||
|
} else {
|
||
|
if (!$CHARGING) {
|
||
|
$CHARGING = 'def';
|
||
|
$CRITICAL = undef;
|
||
|
$LOW = undef;
|
||
|
system "doas shutdown -c";
|
||
|
system "notify-send -t 3000 -i \"battery-good-charging\" \"System is now charging\"";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
sleep 30;
|
||
|
}
|