mirror of
https://github.com/unixtensor/proxmox-ntfy.git
synced 2025-06-27 23:58:06 +00:00
Implament sys.argv
with argparse
This commit is contained in:
24
src/cli.py
24
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
|
||||
return parser.parse_args()
|
17
src/cpu.py
17
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}")
|
||||
|
54
src/main.py
54
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:
|
||||
<https://github.com/unixtensor/proxmox-ntfy>
|
||||
<https://git.rhpidfyre.io/rhpidfyre/proxmox-ntfy>"""
|
||||
|
||||
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,
|
||||
})
|
@ -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}")
|
||||
|
Reference in New Issue
Block a user