This Bash script is designed to monitor a log file in real-time, analyze log entries, and send email alerts based on predefined conditions.
- Bash shell environment
mail
command to send email alerts (usually available by default on Unix-like systems)- Proper configuration of email settings to enable email alerts
- Clone or download the script file (
log_monitor.sh
) to your local system. - Make the script executable using the following command:
chmod +x log_monitor.sh
- Run the script with optional command-line arguments to customize log file path and search keyword:
./log_monitor.sh -f /path/to/log/file.log -s "error"
- -f or --file: Specify the path to the log file (default: "/path/to/log/file.log").
- -s or --search: Specify the keyword to search for in log entries (default: empty, searches all entries).
- Once the script is running, it will continuously monitor the specified log file.
- Press Ctrl+C to stop monitoring and display log statistics.
- To test the script:
- Prepare a sample log file with various log entries (errors, warnings, info).
- Run the script with the sample log file path.
- Generate log entries in the sample log file to observe the script's behavior:
- Add new log entries to simulate errors, warnings, and informational messages.
- Verify that the script properly analyzes and categorizes log entries.
- Verify that email alerts are sent when the error count exceeds the predefined threshold.
./log_monitor.sh -f sample.log -s "error
- Customize the script according to your specific log file format and analysis requirements.
- Ensure proper permissions and configurations for email alerts to function correctly.