Skip to content

Setup — System Health Monitor

Create the project structure and verify your environment before writing any code.

Directory Structure

mkdir -p ~/projects/health-monitor/logs
cd ~/projects/health-monitor
touch health_check.sh health_check.conf
chmod +x health_check.sh

Your directory should look like this:

~/projects/health-monitor/ ├── health_check.sh ← main script (you will write this) ├── health_check.conf ← configuration (you will write this) └── logs/ └── health_check.log ← created automatically on first run

Create the Config File

Create health_check.conf with these defaults:

# health_check.conf — thresholds and alert settings

# Disk usage alert threshold (percentage)
THRESHOLD_DISK=85

# Memory usage alert threshold (percentage)
THRESHOLD_MEM=80

# CPU load alert threshold (1-minute load average)
THRESHOLD_CPU=4.0

# Log file path
LOGFILE="${HOME}/projects/health-monitor/logs/health_check.log"

# Optional: Slack webhook URL for alerts
# SLACK_WEBHOOK_URL="https://hooks.slack.com/services/..."

# Number of log lines to keep (rotation)
LOG_MAX_LINES=1000

Verify Metric Commands Work

Run each metric command manually to confirm it works on your system:

# Disk usage — should print a percentage like "45%"
df / | awk 'NR==2 {print $5}'
45%

# Memory usage — should print a number like "62"
free | awk '/^Mem:/ { printf "%.0f", $3/$2*100 }'
62

# CPU load average (1 minute)
uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | tr -d ','
0.42

# Hostname
hostname
my-server

macOS differences

free does not exist on macOS. Use vm_stat instead. uptime format differs slightly. The course targets Linux; macOS users may need to adapt the memory metric command.

Create a Test Log Entry

Before writing the full script, confirm you can write to the log:

LOGFILE=~/projects/health-monitor/logs/health_check.log
echo "[$(date '+%Y-%m-%d %H:%M:%S')] test entry" >> "$LOGFILE"
cat "$LOGFILE"
[2024-01-15 09:30:00] test entry

If you see the entry, your environment is ready.


requirements | implementation