Assignment- Menu-Based-Health-Check-System
Step 1- bash shell
Step 2- install msmtp
Step 3- Clone the repository- https://github.com/DevOpsInstituteMumbai-wq/Menu-Based-Health-
Check-System.git
Step 4- Make Script executable
Step 5- modify script. Replace email id
Step 6- run the script
Step 7 crontab -e
every four hours, add a cron job
Step 8 -> chmod 600 ~/.msmtprc
Test mail
Check mail inbox.
Step 8 – check log
Step 9- Check mail. Mail receiving on mention email Id as per cron.
NOTE: The given script is the corrected script. Using below script cron also work and also receiving mail. Also I am trying for the mailutils
but it’s not work. So I find another way and do it for the same.
Menu.sh
#!/bin/bash
LOG_FILE="/var/log/sys_health.log"
EMAIL="sangeetashinde.gaikwad@gmail.com"
check_disk_usage() {
echo "Checking disk usage..."
df -h
monitor_services() {
echo "Monitoring running services..."
systemctl list-units --type=service --state=running
check_memory_usage() {
echo "Checking memory usage..."
free -m
check_cpu_usage() {
echo "Checking CPU usage..."
top -bn1 | grep "Cpu"
}
send_report() {
REPORT="/tmp/sys_health_report.txt"
echo "Generating system health report..."
echo "=== System Health Report ==="
echo "Date: $(date)"
echo -e "\nDisk Usage:"
df -h
echo -e "\nRunning Services:"
systemctl list-units --type=service --state=running
echo -e "\nMemory Usage:"
free -m
echo -e "\nCPU Usage:"
top -bn1 | grep "Cpu"
} > "$REPORT"
echo -e "Subject: System Health Report\n\n" | cat - "$REPORT" | /usr/bin/msmtp "$EMAIL"
echo "Report sent successfully!"
# Check if first argument is --email
if [[ "$1" == "--email" ]]; then
# If --email is passed, send the report immediately
send_report
exit 0
fi
while true; do
echo "============================="
echo " System Health Check Menu"
echo "============================="
echo "1. Check Disk Usage"
echo "2. Monitor Running Services"
echo "3. Assess Memory Usage"
echo "4. Evaluate CPU Usage"
echo "5. Send Comprehensive Report"
echo "6. Exit"
read -p "Enter your choice: " choice
case $choice in
1) check_disk_usage ;;
2) monitor_services ;;
3) check_memory_usage ;;
4) check_cpu_usage ;;
5) send_report ;;
6) echo "Exiting..."; exit 0 ;;
*) echo "Invalid option. Please try again." ;;
esac
echo "Press Enter to continue..."
read
done