From 6e258b03bacf8aa6670e526613dc5b5a934bee75 Mon Sep 17 00:00:00 2001 From: "s.mostryukov" Date: Wed, 18 Jun 2025 16:06:28 +0300 Subject: [PATCH] refactor --- README.md | 1 + app/backup.py | 17 +++++++++-------- app/main.py | 14 ++++++++++---- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 03dc054..f843e97 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,4 @@ Python 3.13 Переменные окружения (см. app/config/.env-template) app/config/config.ini Пример конфиг файла см. app/config/config-template.ini +Положить файл со списком устройств в папку config \ No newline at end of file diff --git a/app/backup.py b/app/backup.py index 0e5937d..a4702f8 100644 --- a/app/backup.py +++ b/app/backup.py @@ -36,7 +36,7 @@ def read_device_list() -> dict[str, dict[str, str]]: return all_devices -def connect_to_device(vendor: str, host: str) -> ConnectHandler: +def connect_to_device(vendor: str, host: str, name: str="") -> ConnectHandler: conn_conf: dict = { "host": host, "username": cfg.net_dev.user, @@ -61,14 +61,14 @@ def connect_to_device(vendor: str, host: str) -> ConnectHandler: conn_conf["username"] = cfg.net_dev.domain + "\\" + cfg.net_dev.user try: connection: ConnectHandler = ConnectHandler(**conn_conf) - log.info("connect to %r", host) + log.info("connect to %s %s", name, host) return connection except exceptions.NetmikoAuthenticationException: - log.warning("Authentication error %r ", host) + log.warning("Authentication error %s %s ", name, host) except exceptions.NetmikoTimeoutException: - log.warning("Connection time out error %r ", host) + log.warning("Connection time out error %s %s ", name, host) except Exception as e: - log.warning("Connection error %r: %r", host, e) + log.warning("Connection error %s %s: %s", name, host, e) def send_command(connection: ConnectHandler, command: str) -> str: @@ -84,6 +84,7 @@ def save_mikrotik_bcp(host: str, name: str) -> None: connection = connect_to_device( vendor="mikrotik", host=host, + name=name, ) if connection is None: return @@ -112,7 +113,7 @@ def save_snr_bcp(host: str, name: str) -> None: connection.disconnect() log.info("disconnected from %r", name) if result == "NetmikoTimeoutException": - log.warning("Timeout read config from %r", name) + log.warning("Timeout read config from %s", name) return with open(os.path.join(cfg.bcp.dir, name), "w") as f: f.write(result) @@ -130,9 +131,9 @@ def save_cisco_sb_bcp(host: str, name: str) -> None: return result = send_command(connection, "show running-config") connection.disconnect() - log.info("disconnected from %r", name) + log.info("disconnected from %s", name) if result == "NetmikoTimeoutException": - log.warning("Timeout read config from %r", name) + log.warning("Timeout read config from %s", name) return with open(os.path.join(cfg.bcp.dir, name), "w") as f: f.write(result) diff --git a/app/main.py b/app/main.py index 2125e49..dd3a05f 100644 --- a/app/main.py +++ b/app/main.py @@ -16,6 +16,13 @@ import os def main(): check_bcp_repo() device_dict = read_device_list() + log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs") + log_file: str = os.path.join(log_dir, "logfile.log") + if os.path.isfile(log_file): + with open(log_file, 'w') as file: + file.write('') + log.info("log file clear") + for device in device_dict: current_device = device_dict[device] if current_device["vendor"].lower() == "mikrotik": @@ -42,14 +49,13 @@ def main(): if cfg.git.push: push_to_remote() if cfg.mail.send: - log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs") - file_path: str = os.path.join(log_dir, "logfile.log") - if os.path.isfile(file_path): - with open(file_path, 'r', encoding='utf-8') as file: + if os.path.isfile(log_file): + with open(log_file, 'r', encoding='utf-8') as file: text = file.read() send_mail(text) + if __name__ == "__main__": log.info("Starting app") schedule.every().day.at(cfg.bcp.start_at).do(main)