Automation Scripts
Automation Lab 2: Bash System Health Check
This lab is a Bash script that checks CPU, memory, disk, and key services on a Linux machine and prints a status report with the time.
Overview
You need to see quickly whether a machine is under pressure or a service has stopped. This script puts those checks into one command that can be repeated.
Real world relevance: Spotting a full disk or a stopped service early makes it easier to fix before it causes an outage.
Objective
- Check CPU usage and flag if it exceeds 80 percent
- Check memory usage and flag if used memory exceeds 85 percent
- Check disk usage on the root partition and flag if it exceeds 90 percent
- Verify that critical services such as SSH and cron are running
- Output a clean, timestamped health report to the terminal and an optional log file
Script Walkthrough
- I set limits for CPU, memory, and disk at the top so they are easy to change.
- I used top and awk to get the CPU usage.
- I used free to work out the memory percentage.
- I used df to get the disk usage on the root partition.
- I checked each key service with systemctl is-active and marked it Running or NOT Running.
- I printed a colored summary with the date and time.
- The script is set up to write to a log file, but right now only the closing line reaches it. Sending the whole report to the log is my next fix.
- The service names in the script, ssh and cron, match Debian based systems such as Ubuntu, Kali, and Parrot. On other Linux systems the names can be different.
Script Preview
#!/bin/bash
# Automation Lab 2 - Bash System Health Check
# Checks CPU, memory, disk, and service status with threshold alerting
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
LOG_FILE="/var/log/system_health.log"
CPU_THRESHOLD=80
MEM_THRESHOLD=85
DISK_THRESHOLD=90
SERVICES=("ssh" "cron")
GREEN='\033[0;32m'
RED='\033[0;31m'
RESET='\033[0m'
echo "====================================="
echo " System Health Check - $TIMESTAMP"
echo "====================================="
# CPU Usage
CPU_IDLE=$(top -bn1 | grep "Cpu(s)" | awk '{print $8}' | tr -d '%')
CPU_USED=$(echo "100 - $CPU_IDLE" | bc)
if (( $(echo "$CPU_USED > $CPU_THRESHOLD" | bc -l) )); then
echo -e "CPU Usage: ${RED}${CPU_USED}% - ALERT${RESET}"
else
echo -e "CPU Usage: ${GREEN}${CPU_USED}% - OK${RESET}"
fi
# Memory Usage
MEM_TOTAL=$(free | awk '/Mem:/ {print $2}')
MEM_USED=$(free | awk '/Mem:/ {print $3}')
MEM_PCT=$(echo "scale=1; $MEM_USED / $MEM_TOTAL * 100" | bc)
if (( $(echo "$MEM_PCT > $MEM_THRESHOLD" | bc -l) )); then
echo -e "Memory Usage: ${RED}${MEM_PCT}% - ALERT${RESET}"
else
echo -e "Memory Usage: ${GREEN}${MEM_PCT}% - OK${RESET}"
fi
# Disk Usage
DISK_USED=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$DISK_USED" -gt "$DISK_THRESHOLD" ]; then
echo -e "Disk Usage: ${RED}${DISK_USED}% - ALERT${RESET}"
else
echo -e "Disk Usage: ${GREEN}${DISK_USED}% - OK${RESET}"
fi
# Service Checks
echo ""
echo "Service Status:"
for SERVICE in "${SERVICES[@]}"; do
STATUS=$(systemctl is-active "$SERVICE" 2>/dev/null)
if [ "$STATUS" = "active" ]; then
echo -e " $SERVICE: ${GREEN}Running${RESET}"
else
echo -e " $SERVICE: ${RED}NOT Running - ALERT${RESET}"
fi
done
echo "=====================================" | tee -a "$LOG_FILE"
echo "Check complete. See $LOG_FILE for history."
Key Skills Demonstrated
Tools & Technologies
- Bash shell scripting
- GNU utilities including
top,free,df,awk, andbc - systemd with
systemctl - Cron scheduling
Results & Outcomes
- Built a repeatable host health check that runs quickly on standard Linux systems.
- Created clear output that helps prioritize immediate response actions.
- Improved maintainability by separating thresholds from core logic.
- Set up to keep a log for tracking trends once the full report is sent to it.