A Python-based network monitoring tool that continuously pings multiple IP addresses and logs the results with automatic log rotation.
- Monitor up to 6 IP addresses simultaneously (DNS, Gateway, and 4 custom IPs)
- Automatic log rotation (keeps 7 days of hourly logs)
- Configurable via environment variables
- Runs as a systemd service for continuous monitoring
- Detailed logging with timestamps and response times
- Linux operating system (Ubuntu/Debian recommended)
- Python 3.10 or higher
- Root/sudo access (required for ICMP ping operations)
ping3- For sending ICMP ping requestspython-dotenv- For loading environment variables from .env file
# Update package list
sudo apt update
# Install Python 3 and venv support
sudo apt install python3 python3-venv python3-pip -y# Create virtual environment
python3 -m venv venv
# Activate virtual environment
source venv/bin/activate
# Install Python dependencies
pip install ping3 python-dotenv
# Deactivate virtual environment
deactivateClone this repo under /home/YOUR_USERNAME/ping/ with the monitoring script.
open file named ping.env in the same directory:
DNS=1.1.1.1
GATEWAY=192.168.10.1
EXTRA=8.8.8.8
EXTRA2=192.168.1.10
EXTRA3=192.168.1.20
EXTRA4=192.168.1.30Configuration Options:
DNS- Primary DNS server to monitor (default: 1.1.1.1)GATEWAY- Your network gateway/router (default: 192.168.10.1)EXTRA- Optional additional IP addressEXTRA2- Optional additional IP addressEXTRA3- Optional additional IP addressEXTRA4- Optional additional IP address
# Navigate to project directory
cd ~/ping
# Run the script manually (requires sudo for ICMP)
sudo ./venv/bin/python ping.pyPress Ctrl+C to stop. Check that ping.log is created in the same directory.
sudo nano /etc/systemd/system/ping-monitor.serviceAdd the following content (replace YOUR_USERNAME with your actual username):
[Unit]
Description=Network Ping Monitor
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/home/YOUR_USERNAME/ping
ExecStart=/home/YOUR_USERNAME/ping/venv/bin/python /home/YOUR_USERNAME/ping/ping.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target# Reload systemd to recognize the new service
sudo systemctl daemon-reload
# Enable service to start on boot
sudo systemctl enable ping-monitor
# Start the service
sudo systemctl start ping-monitor
# Check service status
sudo systemctl status ping-monitor# Start the service
sudo systemctl start ping-monitor
# Stop the service
sudo systemctl stop ping-monitor
# Restart the service
sudo systemctl restart ping-monitor
# Check service status
sudo systemctl status ping-monitor
# Enable auto-start on boot
sudo systemctl enable ping-monitor
# Disable auto-start on boot
sudo systemctl disable ping-monitor# View systemd service logs (live)
sudo journalctl -u ping-monitor -f
# View last 50 lines of service logs
sudo journalctl -u ping-monitor -n 50
# View the actual ping log file
tail -f /home/YOUR_USERNAME/ping/ping.log
# View ping log with less
less /home/YOUR_USERNAME/ping/ping.log- Main log file:
/home/YOUR_USERNAME/ping/ping.log - Rotated logs:
ping.log.YYYY-MM-DD_HH-MM-SS(automatically created)
- Logs rotate every hour
- Keeps 168 hours (7 days) of history
- Old logs are automatically deleted
YYYY-MM-DD HH:MM:SS - LEVEL - Message
Example log entries:
2026-02-02 11:15:30 - INFO - Ping to 1.1.1.1 successful. Response time: 12.45 ms
2026-02-02 11:15:31 - WARNING - Ping to 192.168.10.1 failed.
2026-02-02 11:15:32 - ERROR - Error pinging 8.8.8.8: timeout
- Check service status:
sudo systemctl status ping-monitor- View detailed logs:
sudo journalctl -u ping-monitor -n 100- Verify Python path in service file matches your installation:
ls -l /home/YOUR_USERNAME/ping/venv/bin/pythonEnsure packages are installed in the virtual environment:
cd ~/ping
source venv/bin/activate
pip list | grep -E "ping3|python-dotenv"
deactivateIf missing:
source venv/bin/activate
pip install ping3 python-dotenv
deactivate
sudo systemctl restart ping-monitor- Check working directory permissions:
ls -ld ~/ping- Manually create log file:
sudo touch ~/ping/ping.log
sudo chown root:root ~/ping/ping.log- Run script manually to see errors:
cd ~/ping
sudo ./venv/bin/python ping.py# Stop and disable the service
sudo systemctl stop ping-monitor
sudo systemctl disable ping-monitor
# Remove service file
sudo rm /etc/systemd/system/ping-monitor.service
# Reload systemd
sudo systemctl daemon-reload
# Remove project directory
rm -rf ~/ping~/ping/
├── ping.py # Main Python script
├── ping.env # Environment configuration
├── ping.log # Current log file
├── ping.log.2026-02-01_* # Rotated log files
└── venv/ # Virtual environment
├── bin/
│ └── python # Python interpreter
└── lib/
└── python3.*/
└── site-packages/ # Installed packages
For issues or questions:
- Check the troubleshooting section above
- Review systemd logs:
sudo journalctl -u ping-monitor -n 100 - Test the script manually:
sudo ~/ping/venv/bin/python ~/ping/ping.py
This script is provided as-is for network monitoring purposes.