mirror of
https://github.com/unixtensor/proxmox-ntfy.git
synced 2025-06-28 08:48:04 +00:00
The CPU tempature notifications are less eager and a trend message is sent within 5 minutes if a rise in high temps are detected
This commit is contained in:
32
src/cpu.py
32
src/cpu.py
@ -7,11 +7,18 @@ from print_t import print_t
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
from ntfy import Ntfy
|
from ntfy import Ntfy
|
||||||
|
|
||||||
last_cpu_check_warning: float = time.time()
|
_time_now = time.time()
|
||||||
|
|
||||||
|
last_cpu_check_warning: float = _time_now
|
||||||
|
last_cpu_warning_set: float = _time_now
|
||||||
|
last_warning_temp: int = 0
|
||||||
|
|
||||||
|
def timeout_expired(check: float, timeout: int) -> bool:
|
||||||
|
return (time.time() - check) > timeout * 1000
|
||||||
|
|
||||||
class Tempature:
|
class Tempature:
|
||||||
warning_message: str = "🌡️ CPU is at a high tempature."
|
warning_message: str = "🌡️ CPU is at a high tempature."
|
||||||
timeout_check: int = 180 # Seconds
|
timeout_check: int = 60 # Seconds
|
||||||
|
|
||||||
def __init__(self, ntfy_instance: Ntfy, warning_temp: int):
|
def __init__(self, ntfy_instance: Ntfy, warning_temp: int):
|
||||||
self.warning_temp = warning_temp
|
self.warning_temp = warning_temp
|
||||||
@ -26,14 +33,27 @@ class Tempature:
|
|||||||
return float(match.group(1))
|
return float(match.group(1))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def __timeout_expired(self, last_check: float) -> bool:
|
def __warn(self, cpu_temp: int):
|
||||||
return (time.time() - last_check) > Tempature.timeout_check * 1000
|
pad = 0
|
||||||
|
if timeout_expired(last_cpu_warning_set, Tempature.timeout_check * 5):
|
||||||
|
#If the timeout expired, send a notify immediately
|
||||||
|
global last_warning_temp
|
||||||
|
last_warning_temp = 0
|
||||||
|
else:
|
||||||
|
#If the timeout is still active, apply padding to the current CPU tempature so its less spammy
|
||||||
|
pad = 5
|
||||||
|
if cpu_temp + pad > last_warning_temp:
|
||||||
|
message = f"{cpu_temp} C."
|
||||||
|
if last_warning_temp != 0:
|
||||||
|
message += f" The tempature has risen by: {cpu_temp - last_warning_temp} C. 📈"
|
||||||
|
last_warning_temp = cpu_temp
|
||||||
|
self.ntfy.send(message=message, title=Tempature.warning_message)
|
||||||
|
|
||||||
def ntfy_check(self):
|
def ntfy_check(self):
|
||||||
cpu_temp = self.get()
|
cpu_temp = self.get()
|
||||||
if cpu_temp:
|
if cpu_temp:
|
||||||
cpu_temp = math.floor(cpu_temp)
|
cpu_temp = math.floor(cpu_temp)
|
||||||
if cpu_temp >= self.warning_temp and self.__timeout_expired(last_cpu_check_warning):
|
if cpu_temp >= self.warning_temp and timeout_expired(last_cpu_check_warning, Tempature.timeout_check):
|
||||||
self.ntfy.send(f"{Tempature.warning_message} {cpu_temp} C")
|
self.__warn(cpu_temp)
|
||||||
else:
|
else:
|
||||||
print_t("\033[31mCannot get a feasible tempature value for the CPU. (lm-sensors)\033[0m")
|
print_t("\033[31mCannot get a feasible tempature value for the CPU. (lm-sensors)\033[0m")
|
||||||
|
Reference in New Issue
Block a user