commit 1daa4f0e09aeb952c7a6b0d5817d8ab904a32ff2 Author: Dominik Date: Mon Sep 2 22:26:37 2024 +0200 first commit diff --git a/.env copy b/.env copy new file mode 100644 index 0000000..2ce5cdb --- /dev/null +++ b/.env copy @@ -0,0 +1,5 @@ +MAIL_SERVER=mail.mailone24.de +MAIL_USER=management@oceanwave018.de +MAIL_PASSWORD= +MAIL_PORT=587 +EMAIL_RECEIVER=sr.strangas@gmail.com \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..66bca12 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +venv/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d6b1b71 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +# Use an official Python runtime as a parent image +FROM python:3.9-slim + +# Set the working directory in the container +WORKDIR /usr/src/app + +# Copy the current directory contents into the container at /usr/src/app +COPY . . + +# Install any needed packages specified in requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +# Install cron +RUN apt-get update && apt-get install -y cron + +# Add the cron job to run the Python script weekly (every Sunday at 3 AM) +RUN echo "0 3 * * 0 python /usr/src/app/main.py >> /var/log/cron.log 2>&1" > /etc/cron.d/weekly_job + +# Give execution rights on the cron job +RUN chmod 0644 /etc/cron.d/weekly_job + +# Apply cron job +RUN crontab /etc/cron.d/weekly_job + +# Create the log file to be able to run tail +RUN touch /var/log/cron.log + +# Run the cron job +CMD cron && tail -f /var/log/cron.log diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..efb1994 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +version: '3.8' + +services: + python-cron-job: + build: . + container_name: python-cron-job + restart: unless-stopped + volumes: + - ./app:/usr/src/app # Mount your local app directory to the container + environment: + - TZ=UTC # Set the timezone (you can adjust this as needed) + logging: + options: + max-size: "10m" + max-file: "3" diff --git a/main.py b/main.py new file mode 100644 index 0000000..c48171e --- /dev/null +++ b/main.py @@ -0,0 +1,160 @@ +import json +import requests +import os +import smtplib +from dotenv import load_dotenv +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + +def load_services(file_path): + """Load the list of URLs from a JSON file.""" + with open(file_path, "r") as file: + return json.load(file) + +def check_service_status(url): + """Check the status of a service by sending a GET request.""" + try: + response = requests.get(url) + print(f'Url {url} responded with code {response.status_code}') + return {"status": response.status_code, "error": response.status_code >= 400} + except Exception as e: + print(f"{url} was not fetchable: {str(e)}") + return {"status": None, "error": True} + +def generate_html_report(status_dict): + """Generate an HTML report from the status dictionary.""" + html_content = """ + + + + + + URL Status Report + + + +
+

URL Status Report

+ + + + + + + + + + """ + + for url, info in status_dict.items(): + status = info['status'] if info['status'] is not None else "N/A" + error = "Yes" if info['error'] else "No" + status_class = "status-error" if info['error'] else "status-ok" + + html_content += f""" + + + + + + """ + + html_content += """ + +
URLStatus CodeError
{url}{status}{error}
+
+ + + """ + return html_content + + +def send_email(status_dict, server, user, password, receiver): + """Send an email with the service status report.""" + message = MIMEMultipart("alternative") + message["From"] = f"Management - Oceanwave018 <{user}>" + message["To"] = f"Dominik <{receiver}>" + message["Subject"] = "Server Status" + + text = "Status deiner Services:\n" + html = generate_html_report(status_dict) + print(html) + + message.attach(MIMEText(text, 'plain')) + message.attach(MIMEText(html, 'html')) + + try: + with smtplib.SMTP(server, port=os.getenv("MAIL_PORT")) as smtp: + smtp.starttls() + smtp.login(user, password) + smtp.sendmail(user, receiver, message.as_string()) + print("Email sent successfully.") + except smtplib.SMTPException as e: + print(f"Failed to send email: {str(e)}") + +def main(): + print("In Up-Service") + + load_dotenv() + + # Load environment variables + server = os.getenv("MAIL_SERVER") + user = os.getenv("MAIL_USER") + password = os.getenv("MAIL_PASSWORD") + receiver = os.getenv("EMAIL_RECEIVER") + + # Load services and check their status + services = load_services("./services.json") + status_dict = {url: check_service_status(url) for url in services} + + print(status_dict) + + # Send the email report + send_email(status_dict, server, user, password, receiver) + +if __name__ == "__main__": + main() diff --git a/services.json b/services.json new file mode 100644 index 0000000..68cb929 --- /dev/null +++ b/services.json @@ -0,0 +1,7 @@ +[ + "https://brunnergarten-muc.de/", + "https://cms.brunnergarten-muc.de/", + "https://nextcloud.oceanwave018.de/", + "https://hilfe.oceanwave018.de/", + "https://traefik.oceanwave018.de/" +] \ No newline at end of file