diff --git a/src/cli.py b/src/cli.py index a2b69e6..7023cca 100644 --- a/src/cli.py +++ b/src/cli.py @@ -1,7 +1,4 @@ import argparse -import sys - -from typing import Optional _ntfy_configure_prompt = """\033[4mPlease configure an ntfy url before starting.\033[0m Examples: @@ -16,8 +13,7 @@ def Interface(): 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") + 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") return parser.parse_args() \ No newline at end of file diff --git a/src/cpu.py b/src/cpu.py index fde8ed8..722d348 100644 --- a/src/cpu.py +++ b/src/cpu.py @@ -1,22 +1,20 @@ import subprocess import time +import math import re from typing import Optional from ntfy import Ntfy _time_now = time.time() -last_cpu_check_critical: float = _time_now last_cpu_check_warning: float = _time_now -last_cpu_check: int = 60 # Seconds +last_check_debounce: int = 120 # Seconds class Tempature: - cpu_temp_crtitical_message: str = "🔥 CPU tempature is at critical tempatures!" - cpu_temp_warning_message: str = "🌡️ CPU tempature is at a high tempature." + cpu_temp_warning_message: str = "🌡️ CPU is at a high tempature." - def __init__(self, ntfy_instance: Ntfy, cpu_critical_temp: int, cpu_warning_temp: int): - self.cpu_critical_temp = cpu_critical_temp + def __init__(self, ntfy_instance: Ntfy, cpu_warning_temp: int): self.cpu_warning_temp = cpu_warning_temp self.ntfy = ntfy_instance @@ -30,13 +28,11 @@ class Tempature: return None def __check_time(self, last_check: float) -> bool: - return (time.time() - last_check) > last_cpu_check * 1000 + return (time.time() - last_check) > last_check_debounce * 1000 def ntfy_check(self): cpu_temp = self.get() 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}") + cpu_temp = math.floor(cpu_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}") + self.ntfy.send(f"{Tempature.cpu_temp_warning_message} {cpu_temp} C") diff --git a/src/main.py b/src/main.py index c7129dc..b9e42dc 100644 --- a/src/main.py +++ b/src/main.py @@ -8,8 +8,7 @@ from datetime import datetime from typing import TypedDict from ntfy import Ntfy -_pretty_date_time = datetime.fromtimestamp(time.time()) -_monitoring_prompt = f"""{_pretty_date_time} +_monitoring_prompt = f"""{datetime.fromtimestamp(time.time())} Ntfy monitoring software is now listening. Source code available at: @@ -18,14 +17,13 @@ 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"]) + ntfy_cpu_temp_monitor = cpu.Tempature(ntfy, config["cpu_warning_temp"]) print(_monitoring_prompt) while True: @@ -38,7 +36,6 @@ if __name__ == "__main__": 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, diff --git a/src/ntfy.py b/src/ntfy.py index 3b08f21..fb84731 100644 --- a/src/ntfy.py +++ b/src/ntfy.py @@ -8,9 +8,8 @@ class Ntfy: self.server = server def send(self, message: str): + print(f"[{datetime.fromtimestamp(time.time())}]: {message}") 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}")