- This is our final, beginner-friendly project π― — where we'll combine everything you've learned: variables, if-else, loops, functions, logging, and cron automation.
- Let's create a simple Middleware Health Monitoring script that checks Tomcat, Jenkins, Docker, MySQL, and SSL expiry.
π Table of Contents
1️⃣ Overview
This script uses:
- Variables – store names, logs, and domains.
- Functions – reusable checks for each service.
- If-Else – to decide if a service needs restart.
- For loop – to check multiple Docker containers.
- While loop – to retry connection.
- Logs – write output to a file.
2️⃣ Full Middleware Health Monitoring Script
#!/bin/bash
# MiddlewareBox - Simple Health Monitor
LOG="/var/log/middleware_health.log"
domain="middlewarebox.com"
echo "===== Middleware Health Check =====" >> $LOG
date >> $LOG
# --- Function to check Tomcat ---
check_tomcat() {
echo "Checking Tomcat..."
if pgrep -f "org.apache.catalina.startup.Bootstrap" > /dev/null; then
echo "✅ Tomcat is running" | tee -a $LOG
else
echo "π¨ Tomcat is down, restarting..." | tee -a $LOG
/opt/tomcat/bin/startup.sh
echo "✅ Tomcat restarted" | tee -a $LOG
fi
}
# --- Function to check Jenkins ---
check_jenkins() {
echo "Checking Jenkins..."
if systemctl is-active --quiet jenkins; then
echo "✅ Jenkins is active" | tee -a $LOG
else
echo "π¨ Jenkins not running, restarting..." | tee -a $LOG
systemctl restart jenkins
echo "✅ Jenkins restarted" | tee -a $LOG
fi
}
# --- Function to check MySQL ---
check_db() {
echo "Checking MySQL..."
if mysql -u root -pSecret -e "show databases;" > /dev/null 2>&1; then
echo "✅ MySQL connection successful" | tee -a $LOG
else
echo "π¨ MySQL connection failed" | tee -a $LOG
echo "Retrying in 5 seconds..."
sleep 5
mysql -u root -pSecret -e "show databases;" && echo "✅ MySQL reconnected" | tee -a $LOG
fi
}
# --- Check Docker Containers using FOR Loop ---
check_docker() {
echo "Checking Docker containers..."
for c in nginx webapp db
do
status=$(docker inspect -f '{{.State.Status}}' $c 2>/dev/null)
if [ "$status" != "running" ]; then
echo "π¨ Container $c is $status, restarting..." | tee -a $LOG
docker start $c
else
echo "✅ Container $c is running" | tee -a $LOG
fi
done
}
# --- Check SSL certificate expiry ---
check_ssl() {
echo "Checking SSL expiry..."
expiry=$(echo | openssl s_client -servername $domain -connect $domain:443 2>/dev/null | openssl x509 -noout -dates | grep notAfter | cut -d= -f2)
days_left=$(( ($(date -d "$expiry" +%s) - $(date +%s)) / 86400 ))
if [ $days_left -le 15 ]; then
echo "⚠️ SSL expires in $days_left days!" | tee -a $LOG
else
echo "✅ SSL valid for $days_left days" | tee -a $LOG
fi
}
# --- MAIN EXECUTION FLOW ---
check_tomcat
check_jenkins
check_db
check_docker
check_ssl
echo "✅ Health check completed successfully!" | tee -a $LOG
echo "========================================" >> $LOG
π¬ Example Output:
✅ Tomcat is running✅ Jenkins is active
✅ MySQL connection successful
✅ Container nginx is running
✅ SSL valid for 70 days
✅ Health check completed successfully!
3️⃣ Automate the Script
# Run every 30 minutes
*/30 * * * * /opt/scripts/middleware_health.sh >> /var/log/health_cron.log 2>&1
# Run automatically on system reboot
@reboot /opt/scripts/middleware_health.sh >> /var/log/boot_health.log 2>&1
π¬ Output (Cron log):
[BOOT] Health check executed✅ All services healthy
π Summary
- ✅ Combined all major shell scripting concepts.
- ✅ Checks Tomcat, Jenkins, Docker, MySQL, and SSL in one file.
- ✅ Uses
if,for,functions, and basicwhileretry logic. - ✅ Can run manually or via cron / reboot automation.
No comments:
Post a Comment