Skip to content

Commit

Permalink
Create Automating_Log_Management.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
thrinathadevops authored Jun 21, 2024
1 parent 939270a commit f5c7475
Showing 1 changed file with 248 additions and 0 deletions.
248 changes: 248 additions & 0 deletions Log_Management/Automating_Log_Management.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
# -----------------------------------------------------------------------------------
# File Name : Automating_Log_Management
# Author : Thrinatha Reddy
# Description : This script efficiently handles the copying, archiving, transferring, and deleting of log files.
# Call Syntax : ./Automating_Log_Management.sh
# Last Modified: 21-JUN-2024
# -----------------------------------------------------------------------------------


#!/bin/bash

# Server IPs or hostnames
TARGET_SERVER="192.168.1.12"
# Directories containing the logs
LOG_DIRS="/opt/alert" # Modify as needed
# Directory where archives will be stored temporarily in the local server before transfer
ARCHIVE_DIR="/opt"
# Date format
DATE=$(date +"%Y%m%d")
# Temporary directory for intermediate operations
TEMP_DIR_ROOT="/opt/log_archives_$DATE"
mkdir -p "$TEMP_DIR_ROOT"

# Get the current IP address and extract the last segment
IP_LAST_SEGMENT=$(hostname -I | awk '{split($1, a, "."); print a[4]}')

# Check if TEMP_DIR_ROOT was created successfully
if [ ! -d "$TEMP_DIR_ROOT" ]; then
echo "Error: Failed to create temporary directory $TEMP_DIR_ROOT"
exit 1
fi

# Get current time in seconds since epoch
CURRENT_TIME=$(date +%s)

# Function to convert log filename to seconds since epoch
log_filename_to_epoch() {
local filename=$1
local datetime_part=$(echo "$filename" | grep -oP '\d{2}\.\d{2}\.\d{2}_\d{2}\.\d{2}\.\d{2}')
if [[ -n $datetime_part ]]; then
local log_time=$(date -d "${datetime_part:0:2}-${datetime_part:3:2}-${datetime_part:6:2} ${datetime_part:9:2}:${datetime_part:12:2}:${datetime_part:15:2}" +%s 2>/dev/null)
echo $log_time
else
echo "0"
fi
}

# Function to restore logs from a temporary directory
restore_logs() {
local temp_dir=$1
local log_dir=$2
local restored_files=""

# Move files back to their original directory
if [ -d "$temp_dir" ]; then
restored_files=$(ls "$temp_dir")
mv "$temp_dir"/* "$log_dir/"
if [ $? -ne 0 ]; then
echo "Error: Failed to restore logs from $temp_dir to $log_dir"
exit 1
else
echo "Successfully restored logs from $temp_dir to $log_dir:"
echo "$restored_files"
fi
else
echo "Warning: Temporary directory $temp_dir does not exist"
fi
}

# Function to handle failures
handle_failure() {
local message=$1
local reasons=$2
local restored_logs=""

echo "Error: $message"
echo "Reasons: $reasons"
echo "Restoring logs to original directories..."

for LOG_DIR in $LOG_DIRS; do
LOG_TYPE=$(basename "$LOG_DIR")
TEMP_DIR="$TEMP_DIR_ROOT/$LOG_TYPE"

if [ -d "$TEMP_DIR" ]; then
restore_logs "$TEMP_DIR" "$LOG_DIR"
restored_logs=$(ls "$TEMP_DIR")
fi
done

rm -rf "$TEMP_DIR_ROOT"

echo "Log archival and transfer process failed due to: $reasons"

if [ -n "$restored_logs" ]; then
echo "Restored logs:"
echo "$restored_logs"
fi

exit 1
}

# Function to check network issues
check_network_issues() {
local server=$1
local issues=""

# Ping check
if ping -c 3 "$server" > /dev/null 2>&1; then
issues+="Ping to $server: Success\n"
else
issues+="Ping to $server failed.\n"
fi

# Telnet check on port 22 (SSH)
if command -v telnet &> /dev/null; then
telnet_output=$(echo "exit" | telnet "$server" 22 2>&1)
if echo "$telnet_output" | grep -q "Connected to"; then
issues+="Telnet to $server on port 22: Success\n"
else
issues+="Telnet to $server on port 22 failed.\n"
fi
else
issues+="Telnet command not found.\n"
fi

echo "$issues"
}

# Function to delete logs older than 7 days
delete_old_logs() {
local log_dir=$1
local errors=""
local deleted_logs=""
local logs_deleted=false

while IFS= read -r -d '' LOG_FILE; do
LOG_FILENAME=$(basename "$LOG_FILE")
LOG_FILE_EPOCH=$(log_filename_to_epoch "$LOG_FILENAME")
if [ "$LOG_FILE_EPOCH" -ne "0" ]; then
TIME_DIFF=$((CURRENT_TIME - LOG_FILE_EPOCH))
if [ $TIME_DIFF -gt 604800 ]; then
if rm "$LOG_FILE"; then
deleted_logs+="$LOG_FILENAME\n"
logs_deleted=true
else
errors+="Failed to delete $LOG_FILE. Ensure you have the correct permissions. "
fi
fi
else
errors+="Failed to parse date from $LOG_FILENAME. "
fi
done < <(find "$log_dir" -type f \( -name "SystemOut_*.log" -o -name "SystemErr_*.log" \) -print0)

if [ "$logs_deleted" = true ]; then
echo -e "Deleted logs:\n$deleted_logs"
else
echo "No logs were deleted."
fi

if [ -n "$errors" ]; then
echo "Log deletion process completed with errors: $errors"
fi
}

# Main loop to archive and transfer logs
errors=""
for LOG_DIR in $LOG_DIRS; do
if [ ! -d "$LOG_DIR" ]; then
echo "Warning: Log directory $LOG_DIR does not exist. Skipping."
continue
fi
# Extract the log type from the directory path
LOG_TYPE=$(basename "$LOG_DIR")
# Define the archive name based on date and IP address
ARCHIVE_NAME="${LOG_TYPE}_logs_${DATE}_${IP_LAST_SEGMENT}.tar.gz"
# Create a temporary directory for this log type
TEMP_DIR="$TEMP_DIR_ROOT/$LOG_TYPE"
mkdir -p "$TEMP_DIR"

# Verify if the temporary directory was created successfully
if [ ! -d "$TEMP_DIR" ]; then
echo "Error: Failed to create temporary directory $TEMP_DIR"
continue
fi
# Find SystemOut and SystemErr logs within the last 24 hours based on filename timestamps
find "$LOG_DIR" -type f \( -name "SystemOut_*.log" -o -name "SystemErr_*.log" \) | while read -r LOG_FILE; do
LOG_FILENAME=$(basename "$LOG_FILE")
LOG_FILE_EPOCH=$(log_filename_to_epoch "$LOG_FILENAME")

if [ "$LOG_FILE_EPOCH" -ne "0" ]; then
# Calculate time difference in seconds
TIME_DIFF=$((CURRENT_TIME - LOG_FILE_EPOCH))

# Check if the log file is within the last 24 hours (86400 seconds)
if [ $TIME_DIFF -le 86400 ]; then
cp "$LOG_FILE" "$TEMP_DIR"
if [[ $? -ne 0 ]]; then
errors+="Failed to copy $LOG_FILE to $TEMP_DIR. "
fi
fi
fi
done

# Check if there are log files to archive
if [ "$(ls -A $TEMP_DIR)" ]; then
tar -czf "$ARCHIVE_DIR/$ARCHIVE_NAME" -C "$TEMP_DIR" .
if [[ $? -ne 0 ]]; then
handle_failure "Failed to create archive $ARCHIVE_NAME" "$errors"
fi

# Check network issues before attempting to SCP
network_issues=$(check_network_issues "$TARGET_SERVER")

if echo "$network_issues" | grep -q "Ping to $TARGET_SERVER: Success" && echo "$network_issues" | grep -q "Telnet to $TARGET_SERVER on port 22: Success"; then
echo "Network connectivity to $TARGET_SERVER is stable."
else
handle_failure "Network issues detected" "$network_issues"
fi
# Define a log file for SCP errors
scp_error_log="/opt/scp_error.log"
# Attempt to SCP the archive to the target server
scp -q "$ARCHIVE_DIR/$ARCHIVE_NAME" root@"$TARGET_SERVER":/opt/ > "$scp_error_log" 2>&1
if [[ $? -ne 0 ]]; then
scp_error=$(cat "$scp_error_log")
errors+="Failed to transfer $ARCHIVE_NAME to $TARGET_SERVER. SCP error: $scp_error "
continue # Skip deletion and final message if SCP failed
fi
else
echo "No log files to archive for $LOG_TYPE"
fi
# Remove the temporary directory after successful operations
rm -rf "$TEMP_DIR"
done

# Delete old logs after archiving and transferring
for LOG_DIR in $LOG_DIRS; do
delete_old_logs "$LOG_DIR"
done

# Remove the root temporary directory
rm -rf "$TEMP_DIR_ROOT"

# Final message
if [ -z "$errors" ]; then
echo "Log archival, transfer, and deletion process completed successfully."
else
echo "Log archival, transfer, and deletion process completed with errors: $errors"
fi

0 comments on commit f5c7475

Please sign in to comment.