Operations 6 min read

How to Build a Threaded Python Site‑Monitoring Script with Email Alerts

This article explains how to create a Python script that monitors multiple websites, detects downtime, sends email alerts, and uses threading for concurrent checks, illustrating the implementation of site_up and site_down functions and how to manage dynamic monitoring lists in an operations context.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build a Threaded Python Site‑Monitoring Script with Email Alerts

In small companies with a single product line, one or two operations engineers are usually enough, but when the number of sites exceeds a few hundred, dedicated monitoring per site becomes necessary. To avoid alert flooding, the author proposes sending failure notifications only to the responsible "site‑owner".

The following Python script demonstrates this approach. It imports the required modules, defines a dictionary of sites and their alert email addresses, and implements two functions: one to continuously check if sites are up and another to re‑check sites that were previously marked as down.

from threading import Thread
import requests
import time
import smtplib

clients = {
    "http://www.mindg.cn": "[email protected]",
    "http://www.google.com": "[email protected]",
    "http://www.baidu.com": "[email protected]"
}

temp_dic = {}

def site_up():
    while True:
        for client, email in clients.items():
            try:
                r = requests.get(client)
                if r.status_code == 200:
                    print client, 'Site ok'
                    time.sleep(60)
                else:
                    print client, 'Site first registered as down - added to the "site down" monitoring'
                    temp_dic[client] = email
                    del clients[client]
            except requests.ConnectionError:
                print client, 'Site first registered as down - added to the "site down" monitoring'
                temp_dic[client] = email
                del clients[client]

def site_down():
    while True:
        time.sleep(900)
        for client, email in temp_dic.items():
            try:
                r = requests.get(client)
                if r.status_code == 200:
                    print client, 'Site is back up!!'
                    email_sender('Site back up!! ', email, client)
                    clients[client] = email
                    del temp_dic[client]
                else:
                    email_sender('Site down!! ', email, client)
                    print client, 'Site Currently down - email sent'
            except requests.ConnectionError:
                email_sender('Site down!! ', email, client)
                print client, 'Site Currently down - email sent'

# Start concurrent monitoring
t1 = Thread(target=site_up)
t2 = Thread(target=site_down)
t1.start()
t2.start()

The site_up function checks each URL; a 200 response means the site is healthy, otherwise the site is moved to a temporary dictionary ( temp_dic) for further observation. After a fixed interval (15 minutes), site_down re‑checks the entries in temp_dic. If a site recovers, an email is sent and the site is moved back to the main monitoring list; if it remains down, an alert email is sent.

The script runs the two functions concurrently using Python's threading.Thread. The author intentionally omits the actual email_sender implementation, encouraging readers to adapt the notification method to their own needs and treat the missing part as a practical exercise.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

threadingemail alertssite monitoring
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.