Building a Network Monitoring Tool with Python and Linode

Have you ever wanted to monitor your application, get notified when it goes down, and automatically restart it—without breaking a sweat? Well, that’s exactly what we’re doing today! In this guide, we’ll set up a Linode server, deploy NGINX as a Docker container, and use a Python script to monitor the application endpoint. If something goes wrong, our script will send an email alert and attempt to restart the container or even the server!

Step 1: Setting Up a Linode Server ️

We’ll start by spinning up a Linode instance with Debian 11. Here’s how:

  1. Create a Linode Account – Head over to Linode and log in.
  2. Create a New Linode:

    • Select Debian 11 as the operating system.
    • Choose a plan (a Nanode works fine for testing).
    • Set a root password (you’ll need this later).
  3. Access Your Server – Use SSH to connect:
    To access the Linode server using SSH, the server needs to be configured to accept SSH request.

    • Copy your public ssh key and add to the linode server
cat ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa.pub

Enter fullscreen mode Exit fullscreen mode

  • Connect to your linode server
ssh root@your-linode-ip
ssh root@your-linode-ip
ssh root@your-linode-ip

Enter fullscreen mode Exit fullscreen mode

Woohoo! Your first cloud server is live! Time to give it some love, deploy cool stuff, and rule the cloud like a pro. The sky (or should we say, the cloud) is the limit! ️

Step 2: Installing Docker

Docker simplifies app deployment using lightweight containers. Since our server runs Debian, we’ll follow the official Docker installation guide for Debian, found here

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
# Add Docker's official GPG key: sudo apt-get update sudo apt-get install ca-certificates curl sudo install -m 0755 -d /etc/apt/keyrings sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc sudo chmod a+r /etc/apt/keyrings/docker.asc # Add the repository to Apt sources: echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update

Enter fullscreen mode Exit fullscreen mode

To install the latest version run

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Enter fullscreen mode Exit fullscreen mode

Check that Docker is running:

docker --version
docker --version
docker --version

Enter fullscreen mode Exit fullscreen mode

Step 3: Deploying NGINX in a Docker Container

We’ll now set up an NGINX web server inside a Docker container.

docker run -d --name nginx-container -p 8080:80 nginx
docker run -d --name nginx-container -p 8080:80 nginx
docker run -d --name nginx-container -p 8080:80 nginx

Enter fullscreen mode Exit fullscreen mode

Verify it’s running:

docker ps
docker ps
docker ps

Enter fullscreen mode Exit fullscreen mode

Now, visiting http://your-linode-ip:8080 should display the default NGINX welcome page.

Step 4: Writing a Python Monitoring Script

Now for the fun part! Our Python script will:

  • Monitor the application endpoint where NGINX is running by making an HTTP request and checking the status code. This determines whether the application is up or experiencing issues—such as inaccessibility, errors, or a crashed container.
  • Trigger an email alert if the application is down.
  • Attempt to restart the Docker container to restore service.
  • If the issue persists, reboot the entire Linode server to bring everything back online.

Code repository

Skills Gained from This Project

By working through this project, you have developed and strengthened several key skills, including:

  • Cloud Server Management – Setting up and configuring a Linode server.
  • Docker & Containerization – Deploying applications inside Docker containers.
  • Python Scripting – Writing automation scripts to monitor and manage applications.
  • Networking & Troubleshooting – Diagnosing and resolving connectivity issues.
  • Linux System Administration – Installing and managing software on Debian.
  • Email Notifications & Alerting – Using SMTP to send alerts on application failures.
  • Server Automation & Recovery – Automating container and server restarts for high availability.
  • API & Remote Server Management – Interacting with Linode’s API for automated server management.

Final Thoughts

With this setup, you have a self-healing infrastructure that minimizes downtime. Whether you’re running a personal project or a production service, this approach keeps your app online and responsive.

Have questions or ideas to improve the setup? Let’s discuss!

原文链接:Building a Network Monitoring Tool with Python and Linode

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
The worst sort of indolence is being given a choice, yet taking no initiative to change.
我们人生中最大的懒惰,就是当我们明知自己拥有作出选择的能力,却不去主动改变而是放任它的生活态度
评论 抢沙发

请登录后发表评论

    暂无评论内容