first commit
This commit is contained in:
commit
1daa4f0e09
|
|
@ -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
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
.env
|
||||||
|
venv/
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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"
|
||||||
|
|
@ -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 = """
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>URL Status Report</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
background-color: #f4f4f4;
|
||||||
|
color: #333;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
background-color: #fff;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
font-size: 24px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
color: #4CAF50;
|
||||||
|
}
|
||||||
|
table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
th, td {
|
||||||
|
padding: 12px;
|
||||||
|
text-align: left;
|
||||||
|
border-bottom: 1px solid #ddd;
|
||||||
|
}
|
||||||
|
th {
|
||||||
|
background-color: #4CAF50;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
tr:nth-child(even) {
|
||||||
|
background-color: #f9f9f9;
|
||||||
|
}
|
||||||
|
.status-ok {
|
||||||
|
color: #4CAF50;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.status-error {
|
||||||
|
color: #F44336;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h1>URL Status Report</h1>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>URL</th>
|
||||||
|
<th>Status Code</th>
|
||||||
|
<th>Error</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
"""
|
||||||
|
|
||||||
|
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"""
|
||||||
|
<tr>
|
||||||
|
<td>{url}</td>
|
||||||
|
<td class="{status_class}">{status}</td>
|
||||||
|
<td class="{status_class}">{error}</td>
|
||||||
|
</tr>
|
||||||
|
"""
|
||||||
|
|
||||||
|
html_content += """
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
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()
|
||||||
|
|
@ -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/"
|
||||||
|
]
|
||||||
Loading…
Reference in New Issue