Categories We Write About

Build a URL monitor with alerts

Building a URL monitor with alerts involves creating a system that periodically checks the availability and status of a specified URL and notifies you if the website goes down or behaves unexpectedly. Below is a detailed guide on how to build such a monitor, including a simple implementation example using Python. This can be expanded or integrated into more complex setups as needed.


How to Build a URL Monitor with Alerts

1. Define Your Monitoring Requirements

  • URL(s) to monitor: One or multiple URLs.

  • Check frequency: How often to check the URL (e.g., every 5 minutes).

  • Conditions for alert: HTTP status codes (e.g., anything other than 200 OK), response time thresholds, or specific content checks.

  • Alert method: Email, SMS, Slack, webhook, or other notification systems.

  • Logging: Keep track of uptime and downtime history.

2. Choose Your Tools and Technologies

  • Programming Language: Python is popular for this type of task due to libraries like requests and smtplib.

  • Scheduler: Cron jobs, Python’s schedule library, or a cloud function.

  • Notification Service: SMTP email, Twilio for SMS, Slack API, or others.


Example: Simple URL Monitor with Email Alerts Using Python

Requirements

  • Python 3.x

  • requests library (pip install requests)

  • Email credentials for sending alerts (SMTP server, email account)

Code Overview

python
import requests import smtplib import time from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart # Configuration URL = "https://example.com" CHECK_INTERVAL = 300 # seconds (5 minutes) SMTP_SERVER = "smtp.gmail.com" SMTP_PORT = 587 EMAIL_ADDRESS = "your_email@gmail.com" EMAIL_PASSWORD = "your_app_password" # Use app-specific password if 2FA enabled ALERT_TO = "alert_recipient@example.com" def send_email(subject, body): msg = MIMEMultipart() msg['From'] = EMAIL_ADDRESS msg['To'] = ALERT_TO msg['Subject'] = subject msg.attach(MIMEText(body, 'plain')) try: server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) server.starttls() server.login(EMAIL_ADDRESS, EMAIL_PASSWORD) server.sendmail(EMAIL_ADDRESS, ALERT_TO, msg.as_string()) server.quit() print("Alert email sent.") except Exception as e: print(f"Failed to send email: {e}") def check_url(): try: response = requests.get(URL, timeout=10) if response.status_code == 200: print(f"{URL} is up. Status code: {response.status_code}") else: print(f"{URL} returned status code {response.status_code}") send_email( f"URL Monitor Alert: {URL} Status {response.status_code}", f"The URL {URL} returned status code {response.status_code}." ) except requests.exceptions.RequestException as e: print(f"Error accessing {URL}: {e}") send_email( f"URL Monitor Alert: {URL} is down", f"Failed to reach {URL}. Error: {e}" ) def main(): while True: check_url() time.sleep(CHECK_INTERVAL) if __name__ == "__main__": main()

How It Works

  • The script checks the specified URL every 5 minutes.

  • If the URL returns anything other than HTTP 200, it sends an alert email.

  • If the URL is unreachable or a timeout occurs, it sends an alert email.

  • The send_email function uses SMTP to send notifications.


Enhancements to Consider

  • Multiple URLs: Monitor a list of URLs.

  • Custom alert conditions: Response time, content validation.

  • Retry logic: Confirm downtime by retrying before alerting.

  • Logging: Save monitoring results to a file or database.

  • Better alert channels: SMS via Twilio, Slack messages, push notifications.

  • Web dashboard: Visualize uptime history.

  • Dockerize: Containerize for easy deployment.


Summary

This approach gives you a straightforward, customizable URL monitoring solution with alerting capabilities that can be adapted to many use cases. For production use, consider adding security for email credentials and handling rate limits on notifications.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About