From 4bb0fb0a4f48104311cfd379b456a5f0de4c0ffb Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Tue, 13 Aug 2024 05:11:02 +0300 Subject: [PATCH] add return to normal mode --- shutdown_ups.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/shutdown_ups.py b/shutdown_ups.py index 850c9f3..6c647d7 100644 --- a/shutdown_ups.py +++ b/shutdown_ups.py @@ -6,8 +6,8 @@ import socket import os ups_ip = "10.31.41.46" -time_threshold = 4 -interval = 20 +time_threshold = 6 +interval = 60 def parse_time(time_str): try: @@ -51,13 +51,15 @@ def main(): print(f"Time on battery: {time_on_battery} minutes, State: {state}", flush=True) - if time_on_battery != False and time_on_battery < time_threshold: - if state == "on-battery": - shutdown_status = run_command('shutdown --show 2>&1') - if "No scheduled shutdown" in shutdown_status: - print("UPS is failing, shutting down in 60 seconds...", flush=True) - send_email(f"{socket.gethostname()}@vvzvlad.xyz", "UPS is failing, shutting down in 60 seconds...", f"Proxmox node '{socket.gethostname()}': UPS is failing, shutting down in 60 seconds...", f"{socket.gethostname()}@vvzvlad.xyz") - run_command('sudo shutdown -h 1 "Shutting down due to UPS failure"') + shutdown_status = run_command('shutdown --show 2>&1') + + if (state == "on-battery") and (time_on_battery > time_threshold) and ("No scheduled shutdown" in shutdown_status): + print("UPS is failing, shutting down in 60 seconds...", flush=True) + send_email(f"{socket.gethostname()}@vvzvlad.xyz", "UPS is failing, shutting down in 60 seconds...", f"Proxmox node '{socket.gethostname()}': UPS is failing, shutting down in 60 seconds...", f"{socket.gethostname()}@vvzvlad.xyz") + run_command('sudo shutdown -h 3 "Shutting down due to UPS failure"') + if (state == "on-line") and ("Shutdown scheduled" in shutdown_status): + print("UPS is back online, cancelling shutdown...", flush=True) + run_command('sudo shutdown -c') if state == "on-battery": time.sleep(5)