Fix email, shutdown, and option bugs in bmspy-ups

The recipient check used a regular expression that never matched, so a full address got the host name appended. The main loop passed the device name with a parameter name that read_data does not have. A canceled shutdown did not clear its flag, so a second incident did not schedule a shutdown. The recorded shutdown time used the wrong unit. The --port option stayed text and never matched the STARTTLS ports.
This commit is contained in:
tim
2026-10-10 09:03:34 +02:00
parent 953f8a6482
commit b569bcf27b
2 changed files with 55 additions and 4 deletions
+6 -4
View File
@@ -1,7 +1,6 @@
import argparse
import atexit
import os
import re
import time
import smtplib
import ssl
@@ -65,11 +64,13 @@ def handle_shutdown(action: str = "cancel", delay: int = 0, debug: int = 0) -> N
if action == "shutdown":
if scheduled_shutdown is False:
scheduled_shutdown = time.time() + delay * 60 * 1000
scheduled_shutdown = time.time() + delay * 60
os.system("/sbin/shutdown {}".format(delay))
elif action == "cancel":
os.system("/sbin/shutdown -c")
# Allow the next incident to schedule a shutdown again.
scheduled_shutdown = False
return
@@ -88,7 +89,7 @@ def handle_email(
isSSL = False
hostname = socket.gethostname()
if re.match("/@/", recipient) is None:
if "@" not in recipient:
recipient = "{}@{}".format(recipient, hostname)
sender = "bmspy on {} <nobody@{}>".format(hostname, hostname)
@@ -169,6 +170,7 @@ def main() -> None:
"-p",
dest="port",
action="store",
type=int,
default=25,
help="Mail server port (default: 25)",
)
@@ -229,7 +231,7 @@ def main() -> None:
history = deque()
while True:
data = client.read_data(args.socket, "ups", device=device_name)
data = client.read_data(args.socket, "ups", ups=device_name)
device_obj = data.get(device_name)
if device_obj is None:
time.sleep(DAEMON_UPDATE_PERIOD)