From 2cc340820ed7a0c3ba4e4d6d8d72edb7cf7aff08 Mon Sep 17 00:00:00 2001 From: gru Date: Fri, 29 May 2026 23:13:38 +0200 Subject: [PATCH] Add check_unifi_manager.py --- check_unifi_manager.py | 107 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 check_unifi_manager.py diff --git a/check_unifi_manager.py b/check_unifi_manager.py new file mode 100644 index 0000000..f1a8384 --- /dev/null +++ b/check_unifi_manager.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 + +import subprocess +import requests +import re +import argparse +import os +import sys + +PUSHOVER_USER_KEY = "" +PUSHOVER_API_TOKEN = "" + + +def get_installed_version(): + try: + result = subprocess.run(["dpkg", "-l"], capture_output=True, text=True, check=True) + for line in result.stdout.splitlines(): + if "unifi" in line and line.startswith("ii"): + match = re.search(r"\bunifi\s+([\d\.]+)", line) + if match: + return match.group(1) + except Exception as e: + print(f"Błąd przy sprawdzaniu wersji dpkg: {e}") + return None + +def get_latest_version(): + url = "https://raw.githubusercontent.com/trexx/docker-unifi-controller/refs/heads/main/Dockerfile" + try: + response = requests.get(url) + if response.status_code == 200: + match = re.search(r'UNIFI_CONTROLLER_VERSION="([\d\.]+)', response.text) + if match: + return match.group(1) + except Exception as e: + print(f"Błąd przy pobieraniu Dockerfile: {e}") + return None + +def send_pushover(message, title="Aktualizacja UniFi"): + try: + r = requests.post("https://api.pushover.net/1/messages.json", data={ + "token": PUSHOVER_API_TOKEN, + "user": PUSHOVER_USER_KEY, + "message": message, + "title": title, + "priority": 0 + }) + return r.status_code == 200 + except Exception as e: + print(f"Błąd przy wysyłaniu powiadomienia: {e}") + return False + +def download_and_install(version): + deb_url = f"https://dl.ui.com/unifi/{version}/unifi_sysvinit_all.deb" + local_file = f"/tmp/unifi_{version}.deb" + print(f"Pobieranie: {deb_url}") + try: + r = requests.get(deb_url, stream=True) + if r.status_code != 200: + print(f"Nie udało się pobrać pakietu. Kod: {r.status_code}") + return False + with open(local_file, 'wb') as f: + for chunk in r.iter_content(chunk_size=8192): + f.write(chunk) + print("Instalowanie pakietu...") + subprocess.run(["sudo", "dpkg", "-i", local_file], check=True) + print("Instalacja zakończona.") + return True + except Exception as e: + print(f"Błąd przy instalacji: {e}") + return False + +def main(): + parser = argparse.ArgumentParser(description="Sprawdza i aktualizuje UniFi Controller") + parser.add_argument('--install', nargs='?', const='latest', help="Zainstaluj konkretną wersję lub najnowszą") + args = parser.parse_args() + + if args.install: + version = args.install + if version == 'latest': + version = get_latest_version() + if not version: + print("Nie można pobrać najnowszej wersji.") + sys.exit(1) + print(f"Instalacja wersji: {version}") + success = download_and_install(version) + sys.exit(0 if success else 1) + + # Tryb sprawdzania i ewentualnego alertowania + installed = get_installed_version() + latest = get_latest_version() + + if not installed or not latest: + print("Nie udało się pobrać wersji.") + return + + print(f"Zainstalowana wersja: {installed}") + print(f"Najnowsza wersja : {latest}") + + if installed != latest: + msg = f"Dostępna nowa wersja UniFi Controller: {latest} (obecna: {installed})" + print(msg) + send_pushover(msg) + else: + print("UniFi Controller jest aktualny.") + +if __name__ == "__main__": + main()