165 lines
5.0 KiB
Python
165 lines
5.0 KiB
Python
import json
|
|
import requests
|
|
import os
|
|
import smtplib
|
|
import pathlib
|
|
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.connect(server, os.getenv("MAIL_PORT"))
|
|
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
|
|
cwd = pathlib.Path(__file__).parent.resolve()
|
|
print("%s/services.json"%cwd)
|
|
services = load_services("%s/services.json"%cwd)
|
|
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()
|