Skip to content

0xUjwal/SysMon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SysMon — Linux System Monitoring & Alerting Tool

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.


🛠 Tech Stack

  • Language: Python 3
  • Scripting: Bash
  • Libraries: psutil (System utilization), smtplib (Email)
  • Scheduling: Cron

✅ Features

  • 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.

📂 Project Structure

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

🚀 Setup & Installation

1. Clone the Repository

git clone https://github.com/your-username/sysmon.git
cd sysmon

2. Set up Virtual Environment (Recommended)

Isolate dependencies using a virtual environment.

python3 -m venv venv
source venv/bin/activate

3. Install Dependencies

Install the required Python packages (primarily psutil).

pip install -r requirements.txt

⚙️ Configuration

1. Create the Config File

Copy the example template to a usable JSON file.

cp config.example.json config.json

2. Edit Settings

Open 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
    }
}

3. Set Environment Variable (Security)

🔐 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.


🏃 Usage

Manual Execution

You can run the Python script directly to test configurations.

python3 sysmon.py

What happens:

  1. Reads config.json.
  2. Collects metrics.
  3. Appends to sysmon.log.
  4. If thresholds are breached, appends to alerts.log and sends an email.

Bash Wrapper

The run_sysmon.sh script ensures the correct Python interpreter and absolute paths are used (crucial for Cron).

  1. Make the script executable:
    chmod +x run_sysmon.sh
  2. Run it:
    ./run_sysmon.sh

⏰ Scheduling with Cron

To run SysMon automatically (e.g., every 5 minutes), use crontab.

  1. Open the crontab editor:

    crontab -e
  2. 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 the SYSMON_EMAIL_PASSWORD variable. You should define the export inside run_sysmon.sh before the python command, or source your environment file.


📝 Logs

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.

🔮 Future Enhancements

  • 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).

About

Linux System Monitoring & Alerting Tool

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published