diff --git a/app/config.py b/app/config.py index 3f12d1f..32960c4 100644 --- a/app/config.py +++ b/app/config.py @@ -80,6 +80,19 @@ class ConfigGit: username: str = os.getenv("GIT_USERNAME") token: str = os.getenv("GIT_TOKEN") +@dataclass +class ConfigMail: + send: bool = config["mail"].getboolean("send") + if send: + user: str = os.getenv('MAIL_USER') + pwd: str = os.getenv('MAIL_PWD') + server: str = config['mail'].get('server') + port: int = config['mail'].getint('port') + from_address : str = config['mail'].get('from_address') + to_address : str = config['mail'].get('to_address') + subject : str = config['mail'].get('subject') + + @dataclass() class ConfigAll: @@ -88,6 +101,7 @@ class ConfigAll: bcp: ConfigBcp = ConfigBcp net_dev: ConfigNetDev = ConfigNetDev git: ConfigGit = ConfigGit + mail: ConfigMail = ConfigMail cfg = ConfigAll diff --git a/app/config/.env-template b/app/config/.env-template index 0f6963c..441161d 100644 --- a/app/config/.env-template +++ b/app/config/.env-template @@ -8,4 +8,10 @@ NET_DEV_PWD=password # название токена от для подключения к репозиторию GIT_USERNAME=git_user # токен для подключения к репозиторию -GIT_TOKEN=bla-bla-lba \ No newline at end of file +GIT_TOKEN=bla-bla-lba + + +# пользователь и пароль для отправки почты +MAIL_USER=mail_user +# токен для подключения к репозиторию +MAIL_PWD=bla-bla-lba \ No newline at end of file diff --git a/app/config/config-template.ini b/app/config/config-template.ini index 0fb05e8..a404ddb 100644 --- a/app/config/config-template.ini +++ b/app/config/config-template.ini @@ -3,6 +3,16 @@ # False \ True - включение\отключение отправки оповещения в телеграм send: False + +[mail] +# False \ True - включение\отключение отправки оповещения на почту +send: False +server: smtp.example.com +port: 25 +from_address: net-bcp@example.com +to_address: engineer@example.com, network_manager@example.com +subject: Warning in backup network device + # Конфиг гит репозитория [git] # False \ True - включение\отключение отправки файлов в удалённый репозиторий diff --git a/app/main.py b/app/main.py index c8712d2..2125e49 100644 --- a/app/main.py +++ b/app/main.py @@ -7,10 +7,11 @@ from backup import ( save_cisco_sb_bcp, save_cisco_bcp, ) +from messages import send_mail from repo import check_bcp_repo, push_to_remote import schedule import time - +import os def main(): check_bcp_repo() @@ -40,6 +41,13 @@ def main(): save_cisco_bcp(host=current_device["ip"], name=current_device["name"]) 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: + text = file.read() + send_mail(text) if __name__ == "__main__": diff --git a/app/messages.py b/app/messages.py new file mode 100644 index 0000000..77ed710 --- /dev/null +++ b/app/messages.py @@ -0,0 +1,23 @@ +import smtplib +from email.message import EmailMessage +import logging as log +from config import cfg + +def send_mail(text: str) -> None: + log.info('sending a message') + msg = EmailMessage() + msg['Subject'] = cfg.mail.subject + msg['From'] = cfg.mail.from_address + msg['To'] = cfg.mail.to_address + msg.set_content(text) + try: + smtp = smtplib.SMTP(cfg.mail.server, cfg.mail.port) + smtp.starttls() + smtp.ehlo() + smtp.login(cfg.mail.user, cfg.mail.pwd) + + smtp.send_message(msg) + smtp.quit() + log.info('message sent successfully') + except smtplib.SMTPException as err: + log.warning(err)