Skip to content

rohitrsrohit/day-05-disk-alert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

💾 Day 05 — Disk Usage Alerting with Email Notification

30 Days of DevOps | Day 5/30 — Linux & Shell Scripting

A production-grade disk monitoring script that checks multiple mount points against configurable warn/critical thresholds, finds top large files/directories, tracks usage trends over time, sends email + Slack alerts, and generates a dark-themed HTML dashboard report — fully cron-ready.


📸 Sample Terminal Output

╔══════════════════════════════════════════════════════╗
║        DISK USAGE ALERTING SYSTEM                    ║
╚══════════════════════════════════════════════════════╝
  Host    : my-server
  Time    : 2026-06-01 10:30:00

Checking mount points...
──────────────────────────────────────────────────────

  Mount: /
  Usage  : [████████████████░░░░░░░░░░░░░░] 53%  ← green
  Space  : Used 21G of 40G (19G free)
  Inodes : 12%
  Trend  : +2% since last run
  Status : ✔ OK

  Mount: /var
  Usage  : [████████████████████████░░░░░░] 78%  ← yellow
  Space  : Used 7.8G of 10G (2.2G free)
  Trend  : +5% since last run
  Status : ⚡ WARNING

══════════════════════════════════════════════════════
  SUMMARY
  Critical : 0
  Warning  : 1
  Healthy  : 1
══════════════════════════════════════════════════════

Active Alerts:
  🟡 WARNING: /var is at 78% (7.8G/10G) — 2.2G remaining

📁 Project Structure

day-05-disk-alert/
├── scripts/
│   └── disk_alert.sh          # Main monitoring + alerting script
├── config/
│   └── disk_alert.conf        # Mount points, thresholds, email/Slack config
├── tests/
│   └── test_disk_alert.sh     # 15 automated tests
├── docs/
│   └── architecture.md
├── logs/                      # Runtime logs + reports (gitignored)
├── setup_cron.sh              # One-command cron installer
├── .gitignore
└── README.md

⚙️ Features

Feature Description
Multi-mount monitoring Configure any number of mount points
Warn + Critical thresholds Separate % levels per alert severity
ASCII progress bar Visual usage bar in terminal
Inode monitoring Alerts when inodes run out (often missed!)
Top large directories Shows biggest dirs when threshold breached
Trend tracking Tracks usage over time in CSV, shows delta since last run
Email alerts HTML email via mailutils on threshold breach
Slack alerts Webhook notification to Slack channel
HTML dashboard Dark-themed report with progress bars and history table
Exit codes 0=OK, 1=Warning, 2=Critical — integrates with monitoring systems
Cron-ready setup_cron.sh installs it in one command
--show-all mode Full df summary of all filesystems
--find-large mode Find top 20 largest files in any path
--trend mode View usage history from trend CSV

🚀 Quick Start

1. Clone

git clone https://github.com/YOUR_USERNAME/day-05-disk-alert.git
cd day-05-disk-alert
chmod +x scripts/disk_alert.sh setup_cron.sh

2. Configure

nano config/disk_alert.conf
# Mount points to monitor
MOUNT_POINTS=("/" "/home" "/var")

# Thresholds
WARN_THRESHOLD=70
CRIT_THRESHOLD=90

# Email (requires mailutils)
ENABLE_EMAIL=true
ALERT_EMAIL="you@example.com"

# Slack webhook
ENABLE_SLACK=true
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/..."

3. Run

bash scripts/disk_alert.sh

4. Useful modes

# Show all mounted filesystems
bash scripts/disk_alert.sh --show-all

# Find the 20 largest files under /var
bash scripts/disk_alert.sh --find-large /var

# View disk usage history
bash scripts/disk_alert.sh --trend

5. Set up cron (every 30 min)

bash setup_cron.sh

6. Run tests

bash tests/test_disk_alert.sh

📧 Email Setup

# Install mailutils (Ubuntu/Debian)
sudo apt install mailutils -y

# Then in config:
ENABLE_EMAIL=true
ALERT_EMAIL="you@example.com"

For Gmail SMTP, configure /etc/ssmtp/ssmtp.conf or use msmtp.


💬 Slack Setup

  1. Go to api.slack.com/messaging/webhooks
  2. Create a new webhook for your channel
  3. Copy the webhook URL into disk_alert.conf:
ENABLE_SLACK=true
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/T000/B000/xxxxx"

🔁 Exit Codes

Code Meaning Use case
0 All disks healthy No action needed
1 Warning threshold hit Soft alert
2 Critical threshold hit Page on-call

This makes it easy to integrate with monitoring systems like Nagios, Zabbix, or custom wrappers:

bash scripts/disk_alert.sh || echo "Disk alert triggered!"

📊 Trend Tracking

Every run appends to logs/disk_trend.csv:

2026-06-01 08:00:00,/,53
2026-06-01 09:00:00,/,54
2026-06-01 10:00:00,/,55

View it with:

bash scripts/disk_alert.sh --trend

🛠️ Requirements

Requirement Notes
OS Linux / macOS
Shell Bash 4.0+
Commands df, du, find, awk — all standard
Optional mail for email, curl for Slack, mailutils package

🤝 Part of 30 Days of DevOps

Day Topic
Day 1 ✅ System Health Monitor
Day 2 ✅ Log Analyzer & Report Generator
Day 3 ✅ Automated Backup with Rotation
Day 4 ✅ User Management Automation
Day 5 ✅ Disk Usage Alerting (this project)
Day 6 Multi-Stage Dockerfile for Node.js App

📄 License

MIT — free to use, modify, and distribute.

About

day-05-disk-alert

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages