Compare commits

...

1 Commits

Author SHA1 Message Date
2f13e87774 up website monitor 2025-10-01 17:51:12 +02:00
2 changed files with 80 additions and 20 deletions

View File

@@ -0,0 +1,18 @@
{
"website": {
"url": "http://petrovv.com",
"check_interval_seconds": 600,
"timeout_seconds": 5
},
"email": {
"smtp_server": "mail.petrovv.com",
"smtp_port": 465,
"from_email": "monitor@petrovv.com",
"to_email": "nikola@petrovv.com",
"credentials_file": "credentials.txt"
},
"database": {
"path": "website_status.db",
"log_retention_days": 10
}
}

View File

@@ -4,6 +4,8 @@ import smtplib
from email.mime.text import MIMEText from email.mime.text import MIMEText
import sqlite3 import sqlite3
from datetime import datetime, timedelta from datetime import datetime, timedelta
import json
import os
def read_credentials(file_path): def read_credentials(file_path):
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
@@ -19,13 +21,13 @@ def check_website(url, timeout=5):
except requests.RequestException: except requests.RequestException:
return False return False
def send_email(subject, body, to_email, from_email, from_password, smtp_server): def send_email(subject, body, to_email, from_email, from_password, smtp_server, smtp_port):
msg = MIMEText(body) msg = MIMEText(body)
msg['Subject'] = subject msg['Subject'] = subject
msg['From'] = from_email msg['From'] = from_email
msg['To'] = to_email msg['To'] = to_email
with smtplib.SMTP_SSL(smtp_server, 465) as server: with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
server.login(from_email, from_password) server.login(from_email, from_password)
server.sendmail(from_email, to_email, msg.as_string()) server.sendmail(from_email, to_email, msg.as_string())
@@ -36,20 +38,21 @@ def create_database(db_path):
CREATE TABLE IF NOT EXISTS website_status ( CREATE TABLE IF NOT EXISTS website_status (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME NOT NULL, timestamp DATETIME NOT NULL,
status INTEGER NOT NULL current_status INTEGER NOT NULL,
previous_status INTEGER
) )
''') ''')
conn.commit() conn.commit()
conn.close() conn.close()
def log_website_status(db_path, status): def log_website_status(db_path, current_status, previous_status):
conn = sqlite3.connect(db_path) conn = sqlite3.connect(db_path)
cursor = conn.cursor() cursor = conn.cursor()
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cursor.execute(''' cursor.execute('''
INSERT INTO website_status (timestamp, status) INSERT INTO website_status (timestamp, current_status, previous_status)
VALUES (?, ?) VALUES (?, ?, ?)
''', (timestamp, 1 if status else 0)) ''', (timestamp, 1 if current_status else 0, previous_status))
conn.commit() conn.commit()
conn.close() conn.close()
@@ -57,7 +60,7 @@ def get_last_status(db_path):
conn = sqlite3.connect(db_path) conn = sqlite3.connect(db_path)
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(''' cursor.execute('''
SELECT status FROM website_status SELECT current_status FROM website_status
ORDER BY id DESC LIMIT 1 ORDER BY id DESC LIMIT 1
''') ''')
result = cursor.fetchone() result = cursor.fetchone()
@@ -71,50 +74,89 @@ def cleanup_old_logs(db_path, days_to_keep=10):
cutoff_date = (datetime.now() - timedelta(days=days_to_keep)).strftime('%Y-%m-%d %H:%M:%S') cutoff_date = (datetime.now() - timedelta(days=days_to_keep)).strftime('%Y-%m-%d %H:%M:%S')
conn = sqlite3.connect(db_path) conn = sqlite3.connect(db_path)
cursor = conn.cursor() cursor = conn.cursor()
# Delete rows where current_status equals previous_status and timestamp is older than 10 days
cursor.execute(''' cursor.execute('''
DELETE FROM website_status DELETE FROM website_status
WHERE timestamp < ? WHERE timestamp < ? AND current_status = previous_status
''', (cutoff_date,)) ''', (cutoff_date,))
deleted_rows = cursor.rowcount deleted_rows = cursor.rowcount
conn.commit() conn.commit()
conn.close() conn.close()
return deleted_rows return deleted_rows
def print_config_example():
example_config = {
"website": {
"url": "http://petrovv.com",
"check_interval_seconds": 600,
"timeout_seconds": 5
},
"email": {
"smtp_server": "mail.petrovv.com",
"smtp_port": 465,
"from_email": "monitor@petrovv.com",
"to_email": "nikola@petrovv.com",
"credentials_file": "credentials.txt"
},
"database": {
"path": "website_status.db",
"log_retention_days": 10
}
}
print("Example config.json:")
print(json.dumps(example_config, indent=2))
def main(): def main():
url = "http://petrovv.com" # Check if config.json exists
to_email = "nikola@petrovv.com" if not os.path.exists("config.json"):
check_interval = 600 # 10 minutes in seconds print("Error: config.json not found.")
credentials_file = "credentials.txt" print_config_example()
db_path = "website_status.db" return
smtp_server = "mail.petrovv.com"
# Load configuration
with open("config.json", "r") as f:
config = json.load(f)
url = config["website"]["url"]
to_email = config["email"]["to_email"]
check_interval = config["website"]["check_interval_seconds"]
credentials_file = config["email"]["credentials_file"]
db_path = config["database"]["path"]
smtp_server = config["email"]["smtp_server"]
smtp_port = config["email"]["smtp_port"]
log_retention_days = config["database"]["log_retention_days"]
create_database(db_path) create_database(db_path)
from_email, from_password = read_credentials(credentials_file) from_email, from_password = read_credentials(credentials_file)
last_status = None last_status = get_last_status(db_path)
last_cleanup = datetime.now() last_cleanup = datetime.now()
while True: while True:
current_time = datetime.now() current_time = datetime.now()
current_status = check_website(url) current_status = check_website(url)
log_website_status(db_path, current_status) log_website_status(db_path, current_status, last_status)
if last_status is None or current_status != (last_status == 1): if last_status is None or current_status != (last_status == 1):
status_str = "up" if current_status else "down" status_str = "up" if current_status else "down"
subject = f"Website Status Change: {status_str}" subject = f"Website Status Change: {status_str}"
body = f"The website {url} is now {status_str} at {current_time.strftime('%Y-%m-%d %H:%M:%S')}." body = f"The website {url} is now {status_str} at {current_time.strftime('%Y-%m-%d %H:%M:%S')}."
send_email(subject, body, to_email, from_email, from_password, smtp_server) send_email(subject, body, to_email, from_email, from_password, smtp_server, smtp_port)
print_email_sent(status_str, current_time.strftime('%Y-%m-%d %H:%M:%S')) print_email_sent(status_str, current_time.strftime('%Y-%m-%d %H:%M:%S'))
last_status = 1 if current_status else 0 last_status = 1 if current_status else 0
# Perform cleanup once per day # Perform cleanup once per day
if (current_time - last_cleanup).days >= 1: if (current_time - last_cleanup).days >= 1:
deleted = cleanup_old_logs(db_path, 10) deleted = cleanup_old_logs(db_path, log_retention_days)
if deleted > 0: if deleted > 0:
print(f"[{current_time.strftime('%Y-%m-%d %H:%M:%S')}] Cleanup: Deleted {deleted} logs older than 10 days") print(f"[{current_time.strftime('%Y-%m-%d %H:%M:%S')}] Cleanup: Deleted {deleted} logs older than {log_retention_days} days (keeping status changes)")
last_cleanup = current_time last_cleanup = current_time
time.sleep(check_interval) time.sleep(check_interval)
if __name__ == "__main__": if __name__ == "__main__":
main() main()