Test and Debug the Monitoring System
With the script and cron job set up, it’s important to test and debug your monitoring system to ensure it functions correctly. This part of the tutorial focuses on testing various scenarios, simulating failures, and verifying that alerts are sent as expected. Additionally, you’ll learn how to debug common issues to ensure your system is reliable and effective.
Step 1: Test the Script Manually
Before testing the automated cron job, verify that the script works as intended when run manually.
Run the script from the terminal:
/path/to/website_monitor.sh
If the website is reachable:
The script should output a success message (if you added logging or debugging).
No email should be sent.
If the website is down:
An email alert should be sent to the address specified in the EMAIL variable.
Confirm that the email is received.
Step 2: Simulate Website Downtime
To ensure the alerting mechanism works, simulate a scenario where the website is unreachable.
Method 1: Use an Invalid URL
Modify the WEBSITE variable in the script to an invalid or non-existent URL:
WEBSITE="http://invalid-url.com"
Run the script manually:
/path/to/website_monitor.sh
Check your email inbox for an alert. This confirms that the script can detect downtime and send notifications.
Method 2: Use iptables to Block Access (Advanced)
You can use iptables to temporarily block access to the target website:
sudo iptables -A OUTPUT -d example.com -j REJECT
Run the script:
/path/to/website_monitor.sh
After testing, remove the iptables rule to restore access:
sudo iptables -D OUTPUT -d example.com -j REJECT
Step 3: Verify Cron Job Execution
To confirm that the cron job is running the script as scheduled:
Wait for the Next Scheduled Interval: Let the cron job execute the script at its next scheduled time. For example, if you
scheduled it to run every 5 minutes, wait for 5 minutes.
Check Cron Logs: View the cron logs to ensure the job is being executed:
On Ubuntu/Debian:
grep CRON /var/log/syslog
On CentOS/RHEL:
grep CRON /var/log/cron
Look for an entry similar to this:
Dec 7 12:05:01 server CRON[12345]: (your-username) CMD (/path/to/website_monitor.sh)
Check the Script Log (if configured): If you redirected the script output to a log file, review it to confirm the script ran successfully:
cat /var/log/website_monitor.log
Step 4: Debugging Issues
If the system isn’t working as expected, follow these steps to identify and fix the problem:
Issue 1: Cron Job Doesn’t Run
Check the Crontab: Ensure the cron job is listed correctly:
crontab -l
Verify the Script Path: Ensure the full path to the script is correct. Use realpath to confirm:
realpath website_monitor.sh
Check Permissions: Ensure the script is executable:
chmod +x /path/to/website_monitor.sh
Issue 2: Email Alerts Aren’t Sent
Check Email Configuration: Verify that your email utility is correctly configured. Test sending an email manually:
echo "Test email" | mail -s "Test Email" your-email@example.com
Check Mail Logs: Review the logs for errors:
On Ubuntu/Debian:
tail -f /var/log/mail.log
On CentOS/RHEL:
tail -f /var/log/maillog
Issue 3: Script Doesn’t Work as Expected
Run the Script Manually: Execute the script directly to check for errors:
bash -x /path/to/website_monitor.sh
The -x flag provides a step-by-step trace of the script’s execution.
Check Dependencies: Ensure curl and mail are installed and functioning correctly:
curl --version
mail --version
Issue 4: Debugging Output
Enable Debugging Messages in the Script: Add debug messages to the script for troubleshooting:
echo "Debug: Checking website status for $WEBSITE" >> /var/log/website_monitor_debug.log
Step 5: Test Real-World Scenarios
To ensure your monitoring system is robust, test it under various real-world conditions:
Website Intermittent Downtime: Temporarily stop the web server hosting the target website (if you control it) and verify that the script detects the downtime.
Network Connectivity Issues: Disconnect your system from the internet and run the script to confirm it handles network issues gracefully.
Monitoring Multiple Websites: Modify the script to monitor multiple websites and test each URL to ensure accurate monitoring.
Step 6: Final Validation
Once you’ve tested all scenarios and resolved any issues, let the cron job run for a full day. Monitor the logs and email alerts to ensure the system behaves as expected over time.
You have a tested and debugged monitoring system. You’ll be confident that the script detects website downtime, sends email alerts, and runs automatically at the scheduled intervals. In the next part, you’ll explore ways to enhance the monitoring system for greater functionality and reliability.