#!/bin/bash
# Define the location of the log files
LOG_DIR=/var/log
# Define the recipient email address
EMAIL_ADDRESS=user@example.com
# Search for error, warning, and failed messages in log files
ERRORS=$(grep -rniE 'error|warning|failed' $LOG_DIR | grep -v 'grep' | awk -F ":" '{print $1 " - Line " $2 ": " $3}')
if [ -n "$ERRORS" ]; then
# Send an email notification with the error messages
subject="Log file scan - $(date +%F)"
message="The following errors, warnings, or failures were found in log files:
$ERRORS"
echo "$message" | mail -s "$subject" $EMAIL_ADDRESS
else
# Send an email notification indicating no errors were found
subject="Log file scan - $(date +%F)"
message="No errors, warnings, or failures were found in log files."
echo "$message" | mail -s "$subject" $EMAIL_ADDRESS
fi
No comments:
Post a Comment