diff --git a/config.py b/config.py new file mode 100644 index 0000000..7a32215 --- /dev/null +++ b/config.py @@ -0,0 +1,20 @@ + +import os +from dotenv import load_dotenv + +load_dotenv() + +class Config: + WEBMIN_SERVERS = os.getenv('WEBMIN_SERVERS').split(',') + WEBMIN_API_KEYS = os.getenv('WEBMIN_API_KEYS').split(',') + EMAIL_HOST = os.getenv('EMAIL_HOST') + EMAIL_PORT = int(os.getenv('EMAIL_PORT')) + EMAIL_USER = os.getenv('EMAIL_USER') + EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD') + EMAIL_RECIPIENTS = os.getenv('EMAIL_RECIPIENTS').split(',') + SSL_ALERT_DAYS = int(os.getenv('SSL_ALERT_DAYS', 15)) + DOMAIN_EXPIRATION_ALERT_DAYS = int(os.getenv('DOMAIN_EXPIRATION_ALERT_DAYS', 45)) + DOMAIN_FILE = os.getenv('DOMAIN_FILE', 'domains.txt') + + # New variable for additional domains + ADDITIONAL_DOMAINS = os.getenv('ADDITIONAL_DOMAINS', '').split(',') diff --git a/domain_monitor.py b/domain_monitor.py new file mode 100644 index 0000000..f5ad6b0 --- /dev/null +++ b/domain_monitor.py @@ -0,0 +1,28 @@ + +from domain_operations import gather_all_domains +from notifications import send_email, render_email_template +from logger import setup_logger +from config import Config +import time + +logger = setup_logger() + +def main(): + all_domains = gather_all_domains() + + for domain in all_domains: + # Placeholder for checking expiration or any other processing + pass + +def continuous_loop(): + while True: + main() + logger.info(f"Sleeping for {Config.CHECK_INTERVAL} seconds before the next run.") + time.sleep(Config.CHECK_INTERVAL) + +if __name__ == "__main__": + # Uncomment the next line to enable continuous loop mode + # continuous_loop() + + # Comment the following line if enabling continuous loop mode + main() diff --git a/domain_operations.py b/domain_operations.py new file mode 100644 index 0000000..1176174 --- /dev/null +++ b/domain_operations.py @@ -0,0 +1,31 @@ + +import requests +from config import Config + +def get_domains(webmin_url, api_key): + headers = {'Authorization': f"Bearer {api_key}", 'Accept': 'application/json'} + try: + response = requests.get( + f"{webmin_url}/virtual-server/remote.cgi?program=list-domains&name-only", + headers=headers, timeout=10, verify=False + ) + response.raise_for_status() + return response.text.splitlines() + except requests.RequestException as e: + return [] + +def gather_all_domains(): + all_domains = [] + + # Fetch domains from Webmin servers + for i, webmin_url in enumerate(Config.WEBMIN_SERVERS): + domains = get_domains(webmin_url, Config.WEBMIN_API_KEYS[i]) + all_domains.extend(domains) + + # Include additional domains from the environment variable + if Config.ADDITIONAL_DOMAINS: + all_domains.extend([domain.strip() for domain in Config.ADDITIONAL_DOMAINS if domain.strip()]) + + # Ensure the domain list is unique + all_domains = list(set(all_domains)) + return all_domains diff --git a/logger.py b/logger.py new file mode 100644 index 0000000..179c0bc --- /dev/null +++ b/logger.py @@ -0,0 +1,10 @@ + +import logging + +def setup_logger(log_file='app.log'): + logger = logging.getLogger(__name__) + handler = logging.FileHandler(log_file) + formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') + handler.setFormatter(formatter) + logger.addHandler(handler) + return logger diff --git a/notifications.py b/notifications.py new file mode 100644 index 0000000..7f82530 --- /dev/null +++ b/notifications.py @@ -0,0 +1,26 @@ + +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from jinja2 import Environment, FileSystemLoader + +from config import Config + +def send_email(subject, html_content, plain_content): + msg = MIMEMultipart("alternative") + msg['Subject'] = subject + msg['From'] = Config.EMAIL_USER + msg['To'] = ", ".join(Config.EMAIL_RECIPIENTS) + + msg.attach(MIMEText(plain_content, "plain")) + msg.attach(MIMEText(html_content, "html")) + + with smtplib.SMTP(Config.EMAIL_HOST, Config.EMAIL_PORT) as smtp: + smtp.starttls() + smtp.login(Config.EMAIL_USER, Config.EMAIL_PASSWORD) + smtp.sendmail(Config.EMAIL_USER, Config.EMAIL_RECIPIENTS, msg.as_string()) + +def render_email_template(template_name, context): + env = Environment(loader=FileSystemLoader('./templates')) + template = env.get_template(template_name) + return template.render(context)