SysMon is a lightweight system monitoring script written in Python. It tracks essential system metrics (CPU, Memory, Disk, Network) and logs them to a rotating directory. If specific usage thresholds are breached, it sends real-time email alerts.
Designed to be scheduled via Cron, SysMon ensures you are the first to know if your server needs attention.
- Language: Python 3
- Scripting: Bash
- Libraries:
psutil(System utilization),smtplib(Email) - Scheduling: Cron
- Comprehensive Monitoring: Tracks CPU, RAM, Disk usage, and Network throughput (kbps).
- Persistent Logging: Appends metrics with precise timestamps to a log file.
- Email Alerts: Sends immediate notifications via SMTP when defined thresholds are exceeded.
- Secure Configuration: Uses environment variables for sensitive credentials (email passwords).
- Cron-Ready: Includes a Bash wrapper for seamless integration with system schedulers.
sysmon/
├── sysmon.py # Main Python logic
├── run_sysmon.sh # Bash wrapper for Cron
├── config.json # Configuration file (Git ignored typically)
├── config.example.json # Template for configuration
├── requirements.txt # Python dependencies
└── logs/ # Directory where logs are stored
git clone https://github.com/your-username/sysmon.git
cd sysmonIsolate dependencies using a virtual environment.
python3 -m venv venv
source venv/bin/activateInstall the required Python packages (primarily psutil).
pip install -r requirements.txtCopy the example template to a usable JSON file.
cp config.example.json config.jsonOpen config.json and adjust the paths and thresholds:
{
"log_dir": "/path/to/your/sysmon/logs",
"smtp": {
"sender_email": "server-alerts@example.com",
"receiver_email": "admin@example.com",
"smtp_server": "smtp.gmail.com",
"smtp_port": 587
},
"thresholds": {
"cpu_usage": 85,
"memory_usage": 90,
"disk_usage": 90
}
}🔐 Security Note: Never hardcode your email password in
config.json.
Export your email password (or App Password) as an environment variable.
export SYSMON_EMAIL_PASSWORD="your_app_password_here"To make this persistent, add the line above to your ~/.bashrc or ~/.zshrc file.
You can run the Python script directly to test configurations.
python3 sysmon.pyWhat happens:
- Reads
config.json. - Collects metrics.
- Appends to
sysmon.log. - If thresholds are breached, appends to
alerts.logand sends an email.
The run_sysmon.sh script ensures the correct Python interpreter and absolute paths are used (crucial for Cron).
- Make the script executable:
chmod +x run_sysmon.sh
- Run it:
./run_sysmon.sh
To run SysMon automatically (e.g., every 5 minutes), use crontab.
-
Open the crontab editor:
crontab -e
-
Add the schedule line:
*/5 * * * * /bin/bash /full/path/to/sysmon/run_sysmon.sh >> /full/path/to/sysmon/cron.log 2>&1
⚠️ Important for Cron: Cron jobs often run with a minimal environment and may not see theSYSMON_EMAIL_PASSWORDvariable. You should define the export insiderun_sysmon.shbefore the python command, or source your environment file.
SysMon generates the following log files in the directory defined in your config.json:
| Log File | Description |
|---|---|
sysmon.log |
Contains standard metric entries for every run. |
alerts.log |
Records specific instances where thresholds were breached. |
cron.log |
(Optional) Captures standard output/errors from the Cron job itself. |
- Add desktop notifications (e.g.,
notify-send) alongside email. - Create a simple web dashboard (Flask/Django) to view log history.
- Integrate visualization tools like Matplotlib or Grafana.
- Granular network monitoring (per-interface stats).