From 23d0e30ef364841791aced6461409487d14a8624 Mon Sep 17 00:00:00 2001 From: rhpidfyre Date: Sat, 7 Jun 2025 00:05:42 -0400 Subject: [PATCH] Implament `sys.argv` with argparse --- src/cli.py | 24 +++++++++--------------- src/cpu.py | 17 ++++++++++++----- src/main.py | 54 +++++++++++++++++++++++++++++------------------------ src/ntfy.py | 5 +++++ 4 files changed, 56 insertions(+), 44 deletions(-) diff --git a/src/cli.py b/src/cli.py index 48a4508..a2b69e6 100644 --- a/src/cli.py +++ b/src/cli.py @@ -10,20 +10,14 @@ python3 main.py ntfy.domain.com\033[0m Use \033[32m-h\033[0m or \033[32m--help\033[0m for a full list of options.""" -class Interface: - def __init__(self): - self.parser = argparse.ArgumentParser() - self.parser.add_argument("--cpu-temp-disabled", action="store_true", help="Disable notifications for CPU tempature.") - self.parser.add_argument("--cpu-temp-critical", type=int, default=80, help="CPU tempature for the crtitical alert. default = 80") - self.parser.add_argument("--cpu-temp-warning", type=int, default=70, help="CPU tempature for the warning alert. default = 70") - self.parser.add_argument("--update-rate", type=int, default=1, help="How often updates happen in seconds. default = 1") +def Interface(): + parser = argparse.ArgumentParser() + parser.add_argument("server_address", help="The ntfy server address.") - def parsed_args(self): - return self.parser.parse_args() + parser.add_argument("--disable-uptime-notifys", action="store_true", help="Disable uptime notifications.") + parser.add_argument("--disable-cpu-temp", action="store_true", help="Disable notifications for CPU tempature.") + parser.add_argument("--cpu-temp-critical", type=int, default=80, help="CPU tempature for the crtitical alert. default = 80") + parser.add_argument("--cpu-temp-warning", type=int, default=70, help="CPU tempature for the warning alert. default = 70") + parser.add_argument("--update-rate", type=int, default=1, help="How often updates happen in seconds. default = 1") - def argv_1(self) -> Optional[str]: - if len(sys.argv) > 1: - return sys.argv[1] - else: - print(_ntfy_configure_prompt) - return None \ No newline at end of file + return parser.parse_args() \ No newline at end of file diff --git a/src/cpu.py b/src/cpu.py index 982b1d2..fde8ed8 100644 --- a/src/cpu.py +++ b/src/cpu.py @@ -5,8 +5,11 @@ import re from typing import Optional from ntfy import Ntfy -last_cpu_check_unix: float = time.time() -last_cpu_check: int = 60 # Seconds +_time_now = time.time() +last_cpu_check_critical: float = _time_now +last_cpu_check_warning: float = _time_now + +last_cpu_check: int = 60 # Seconds class Tempature: cpu_temp_crtitical_message: str = "🔥 CPU tempature is at critical tempatures!" @@ -26,10 +29,14 @@ class Tempature: return float(match.group(1)) return None + def __check_time(self, last_check: float) -> bool: + return (time.time() - last_check) > last_cpu_check * 1000 + def ntfy_check(self): cpu_temp = self.get() - if cpu_temp and (time.time() - last_cpu_check_unix) > last_cpu_check * 1000: - if cpu_temp >= self.cpu_critical_temp: + if cpu_temp: + print(f"{cpu_temp}") + if cpu_temp >= self.cpu_critical_temp and self.__check_time(last_cpu_check_critical): self.ntfy.send(f"{Tempature.cpu_temp_crtitical_message} {cpu_temp}") - elif cpu_temp >= self.cpu_warning_temp: + if cpu_temp >= self.cpu_warning_temp and self.__check_time(last_cpu_check_warning): self.ntfy.send(f"{Tempature.cpu_temp_warning_message} {cpu_temp}") diff --git a/src/main.py b/src/main.py index 2b470f3..c7129dc 100644 --- a/src/main.py +++ b/src/main.py @@ -4,36 +4,42 @@ import package import cli import cpu +from datetime import datetime +from typing import TypedDict from ntfy import Ntfy -def start( - cpu_temp_checking: bool, - cpu_critical_temp: int, - cpu_warning_temp: int, - ntfy_server: str, - interval: int -): - ntfy = Ntfy(ntfy_server) - ntfy_cpu_temp_monitor = cpu.Tempature(ntfy, cpu_critical_temp, cpu_warning_temp) +_pretty_date_time = datetime.fromtimestamp(time.time()) +_monitoring_prompt = f"""{_pretty_date_time} +Ntfy monitoring software is now listening. - print(f"Started. {time.time()}") - print("Ntfy monitoring software is now listening.") +Source code available at: + +""" +class Config(TypedDict): + cpu_temp_check_disabled: bool + cpu_critical_temp: int + cpu_warning_temp: int + update_interval: int + ntfy_server_url: str + +def start(config: Config): + ntfy = Ntfy(config["ntfy_server_url"]) + ntfy_cpu_temp_monitor = cpu.Tempature(ntfy, config["cpu_critical_temp"], config["cpu_warning_temp"]) + + print(_monitoring_prompt) while True: - if not cpu_temp_checking: + if not config["cpu_temp_check_disabled"]: ntfy_cpu_temp_monitor.ntfy_check() - time.sleep(interval) + time.sleep(config["update_interval"]) if __name__ == "__main__": if package.installed("lm-sensors"): - cli_args = cli.Interface() - ntfy_server = cli_args.argv_1() - parsed = cli_args.parsed_args() - if ntfy_server: - start( - parsed.cpu_temp_checking, - parsed.cpu_temp_critical, - parsed.cpu_temp_warning, - ntfy_server, - parsed.update_rate, - ) + cli_args = cli.Interface() + start({ + "cpu_temp_check_disabled": cli_args.disable_cpu_temp, + "cpu_critical_temp": cli_args.cpu_temp_critical, + "cpu_warning_temp": cli_args.cpu_temp_warning, + "update_interval": cli_args.update_rate, + "ntfy_server_url": cli_args.server_address, + }) \ No newline at end of file diff --git a/src/ntfy.py b/src/ntfy.py index 2db09ac..3b08f21 100644 --- a/src/ntfy.py +++ b/src/ntfy.py @@ -1,4 +1,7 @@ import requests +import time + +from datetime import datetime class Ntfy: def __init__(self, server: str): @@ -6,6 +9,8 @@ class Ntfy: def send(self, message: str): try: + pretty_date_time = datetime.fromtimestamp(time.time()) + print(f"[{pretty_date_time}]: {message}") requests.post(self.server, data=message) except Exception as err: print(f"Ntfy failed. {err}")