Understanding a Python Website Monitor with Slack Alerts

Intro

Today, we’ll dive into the Python script we posted yesterday that monitors website availability and sends alerts through Slack.

Core Components and Setup

The script is built around a WebsiteMonitor class that handles all the monitoring functionality. Let’s break down its key components:

Initial Setup and Dependencies

The script uses several important Python libraries:

  • requests for making HTTP requests
  • socks for SOCKS proxy support (used with Tor)
  • datetime for time-based operations
  • json for configuration handling

The WebsiteMonitor Class

The class initializes with two separate request sessions:

This dual-session approach is clever – it maintains one session for regular websites and another for .onion addresses through Tor, allowing the monitor to check both regular websites and Tor hidden services.

How the Monitoring Works

Site Management

The script maintains a dictionary of sites with their status:

Each site entry tracks:

  • Number of consecutive failures
  • Timestamp of the last check
  • Whether an alert has been sent

Site Checking Logic

The check_site method handles the actual verification:

  1. It determines which session to use based on the URL (.onion or regular)
  2. Makes a request with a realistic user agent
  3. Sets a 30-second timeout to avoid hanging
  4. Considers a site “up” if it returns a 200 status code

Alert System

The monitoring system has two types of alerts:

  1. Failure Alerts: Triggered every three consecutive failures
  1. Daily Status Reports: Sent every 24 hours, showing the status of all monitored sites

Slack Integration

The Slack alerts are formatted with helpful emojis and site information:

  • 🚨 for failure alerts
  • 📊 for daily reports
  • ✅ for up status
  • ❌ for down status

Configuration and Usage

The script uses a JSON configuration file for flexibility:

This approach allows easy modification of monitored sites without changing the code.

Error Handling and Robustness

The script includes several robust error handling features:

  1. SSL verification is disabled to handle sites with invalid certificates
  2. Comprehensive exception handling for network issues
  3. Configuration validation at startup
  4. Proper handling of missing or invalid configuration files

The Monitoring Loop

The main monitoring loop runs continuously:

  1. Checks each site in the configuration
  2. Updates failure counts and sends alerts if needed
  3. Resets failure counts when sites recover
  4. Waits for an hour before the next check cycle

Beyond the Basics

Some advanced features worth noting:

  1. Tor Support: The ability to monitor .onion sites makes this tool valuable for organizations running hidden services
  2. Flexible Timing: The hour-long wait between checks and three-failure alert threshold can be easily modified
  3. Detailed Logging: Console output helps track the monitor’s operation
  4. Clean Exit Handling: Proper system exit codes for different error conditions

Conclusion

This website monitor is a practical example of how to build a robust monitoring system with Python. It demonstrates several important concepts:

  • Session management for different types of requests
  • Proper error handling and logging
  • Configuration-driven design
  • Integration with external services (Slack)
  • Support for both regular and Tor hidden services

The script’s modular design makes it easy to extend with additional features like different notification methods or more detailed status reporting.