Thursday, August 3, 2023

New File

 #!/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"

    sed 's/<[^>]*>//g' "$OUTPUT_FILE" | awk -v OFS='\t' '{print $1, $2, $3}' | while IFS=$'\t' read -r error line message; do

        echo "<tr><td>$error</td><td>$line</td><td>$message</td></tr>" >> "$OUTPUT_FILE.tmp"

    done

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

    mv "$OUTPUT_FILE.tmp" "$OUTPUT_FILE"

}


# Send email function

send_email() {

    local email_subject="Patch Log Errors - $LOG_FILE_NAME"

    local 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

    /usr/lib/sendmail -t < "$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