Thursday, August 3, 2023

mewss

#!/usr/bin/ksh


# Check if both log file location and name are provided as arguments

if [ $# -ne 2 ]; then

    echo "Usage: $0 <log_file_location> <log_file_name>"

    exit 1

fi


# Set the input parameters to variables

LOG_FILE_LOCATION="$1"

LOG_FILE_NAME="$2"


# Define other variables

OUTPUT_FILE="patch_errors.html"

RECIPIENT="user@example.com"


# Function to scan log files for errors, warnings, and failures

scan_log_files() {

    egrep -i "error|warning|failed" "$LOG_FILE_LOCATION/$LOG_FILE_NAME" > "$OUTPUT_FILE"

    # Add a meaningful description about the log file being scanned

    echo "<p>Scanned log file: $LOG_FILE_LOCATION/$LOG_FILE_NAME</p>" > "$OUTPUT_FILE.tmp"

    # Convert the log errors to an HTML table

    echo "<table border='1'>" >> "$OUTPUT_FILE.tmp"

    echo "<tr><th>Error Type</th><th>Line</th><th>Error Message</th></tr>" >> "$OUTPUT_FILE.tmp"

    awk -F':' '{print "<tr><td>" $2 "</td><td>" $1 "</td><td>" $3 "</td></tr>"}' "$OUTPUT_FILE" >> "$OUTPUT_FILE.tmp"

    echo "</table>" >> "$OUTPUT_FILE.tmp"

    mv "$OUTPUT_FILE.tmp" "$OUTPUT_FILE"

}


# Function to send email using the MIME file

send_email() {

    email_subject="Patch Log Errors - $LOG_FILE_NAME"

    mime_file="email_content.mime"

    

    # Create the MIME file

    {

        echo "To: $RECIPIENT"

        echo "Subject: $email_subject"

        echo "Content-Type: text/html"

        echo "MIME-Version: 1.0"

        echo

        cat "$OUTPUT_FILE"

    } > "$mime_file"


    # Send the email using the created MIME file

    mailx -s "$email_subject" "$RECIPIENT" < "$mime_file"


    # Clean up the MIME file

    rm "$mime_file"

}


# Main script execution

scan_log_files

if [ -s "$OUTPUT_FILE" ]; then

    send_email

    echo "Email sent with the error details from the log file: $LOG_FILE_NAME."

else

    echo "No errors, warnings, or failures found in the log file: $LOG_FILE_NAME."

fi


No comments:

Post a Comment