Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagebash
#!/bin/bash

# Function to display usage information
usage() {
    echo "Usage: $0 -d swarm_ip -p admin:password -i new_log_level [-t duration_in_seconds]"
    echo "  -d, --swarm_ip           IP address of the Swarm API endpoint"
    echo "  -p, --credentials        Credentials in the format admin:password"
    echo "  -i, --log.level          New log level to set"
    echo "  -t, --time               Duration in seconds to keep the new log level (optional)"
    exit 1
}

# Parse input arguments
while [[ "$#" -gt 0 ]]; do
Function to format file size
format_size() {
    local size=$1
   case $1if in(( size >= 1073741824 )); then
   -d|--swarm_ip) swarm_ip="$2"; shift ;;  echo "$(awk "BEGIN {printf    -p|--credentials) credentials="$2"; shift ;;
        -i|--log.level) new_log_level="$2"; shift ;;\"%.1fGB\", $size/1073741824}")"
    elif (( size >= 1048576 )); then
         -t|--time)
   echo "$(awk "BEGIN {printf \"%.1fMB\", $size/1048576}")"
    elif (( size >= 1024 if [[ -n "$2" && "$2" != -* ]]; then
    )); then
        echo "$(awk "BEGIN {printf \"%.1fKB\", $size/1024}")"
    else
      duration="$2  echo "${size}B"
    fi
}

# Function to format duration
format_duration() {
  shift  local duration=$1
    local hours=$((duration / 3600))
 else   local minutes=$(( (duration % 3600) / 60 ))
    local  read -p "Enter duration in seconds: " duration
            fi
 seconds=$((duration % 60))
    printf "%02d:%02d:%02d" $hours $minutes $seconds
}

# Parse input arguments
while [[ "$#" -gt 0 ]]; do
    case $1 in
   ;;         *) usage-d|--swarm_ip) swarm_ip="$2"; shift ;;
    esac    -p|--credentials) credentials="$2"; shift done

# Check if required arguments are provided
if [[ -z "$swarm_ip" || -z "$credentials" || -z "$new;;
        -i|--log.level) new_log_level=" ]]; then$2"; shift ;;
      usage fi -t|--time)
# Retrieve the cluster name clusterName=$(curl -u admin:caringo -sS "<http://$swarm_ip:91/api/storage/clusters>" | grep -oP '"name":\s*"\K[^"]+')
if [[ -zn "$clusterName$2" ]]; && "$2" != -* ]]; then
    echo   "Failed to retrieve the cluster name. Please check your inputs. duration="$2"
    exit  1 fi  # Convert duration to an integer if itshift
is set if [[ -n "$duration" ]]; then       else
 if ! [[ "$duration" =~ ^[0-9]+$ ]]; then        read echo-p "Error:Enter Durationduration mustin beseconds: a" positiveduration
integer value in seconds."         exitfi
1     fi fi  # Display input parameters echo;;
"Swarm IP: $swarm_ip" echo "Credentials: [hidden for security]" echo*) "Newusage log;;
level: $new_log_level" echo "Cluster Name:esac
$clusterName"  # Get theshift
currentdone
log level
echo "Retrieving the current log level..."
current_log_level=$(curl -u "$credentials" -sS "<http://$swarm_ip:91/api/storage/clusters/$clusterName/settings/log.level>" | jq '.value')

# Check if the current log level was retrieved successfully
if [[ -z "$current
# Check if required arguments are provided
if [[ -z "$swarm_ip" || -z "$credentials" || -z "$new_log_level" ]]; then
    usage
fi

# Retrieve the cluster name
clusterName=$(curl -u admin:caringo -sS "http://$swarm_ip:91/api/storage/clusters" | grep -oP '"name":\s*"\K[^"]+')
if [[ -z "$clusterName" ]]; then
    echo "Failed to retrieve the cluster name. Please check your inputs."
    exit 1
fi

# Convert duration to an integer if it is set
if [[ -n "$duration" ]]; then
    if ! [[ "$duration" =~ ^[0-9]+$ ]]; then
        echo "Error: Duration must be a positive integer value in seconds."
        exit 1
    fi
fi

# Display input parameters
echo "Swarm IP: $swarm_ip"
echo "Credentials: [hidden for security]"
echo "Cluster Name: $clusterName"

# Identify the log file location
log_file=""
if [[ -f "/var/log/caringo/castor.log" ]]; then
    log_file="/var/log/caringo/castor.log"
elif [[ -f "/var/log/datacore/castor.log" ]]; then
    log_file="/var/log/datacore/castor.log"
fi

# Display log file information and truncate
if [[ -n "$log_file" ]]; then
    echo "Log file located at: $log_file"

    # Capture initial file size
    initial_size=$(stat -c%s "$log_file")
    initial_size_formatted=$(format_size "$initial_size")
    echo "Initial log file size: $initial_size_formatted"

else
    echo "Warning: Log file not found in expected directories."
fi

# Get the current log level
echo ""
echo "Retrieving the current log level..."
current_log_level=$(curl -u "$credentials" -sS "http://$swarm_ip:91/api/storage/clusters/$clusterName/settings/log.level" | grep -oP '"value":\s*\K[0-9]+')

# Check if the current log level was retrieved successfully
if [[ -z "$current_log_level" ]]; then
    echo "Failed to retrieve the current log level. Please check your inputs."
    exit 1
fi
echo "New log level: $new_log_level"
echo "Current log level is $current_log_level."

# Check if the new log level is the same as the current log level
if [[ "$current_log_level" -eq "$new_log_level" ]]; then
    echo ""
    echo "Log level is already set to $new_log_level. No changes made."
    exit 0
fi

# Update the log level using PUT
echo "Updating log level to $new_log_level..."
response=$(curl -u "$credentials" -sS -X PUT -H "Content-Type: application/json" \
    "http://$swarm_ip:91/api/storage/clusters/$clusterName/settings/log.level" \
    -d "{\"value\": $new_log_level}")

# Verify if the log level was updated
updated_log_level=$(echo "$response" | grep -oP '"value":\s*\K[0-9]+')
if [[ "$updated_log_level" -eq "$new_log_level" ]]; then
    echo "Failed to retrieve the current log level. Please check your inputs."
    exit 1
fi
echo "Current log level is $currentLog level changed successfully from $current_log_level → $new_log_level."
else
# Check if the newecho log"Failed levelto is the same as the current update log level
if [[ "$current_log_level" -eq "$new_log_level" ]]; then
    echo "Log level is already set to $new_log_level. No changes made."
    exit 0
fi

# Update the log level using PUT
echo "Updating log level to. Response: $response"
    exit 1
fi

# If duration is specified, wait and revert after the specified time
if [[ -n "$duration" && "$duration" -gt 0 ]]; then
    echo "Keeping log level at $new_log_level for $duration second(s)..."
response=$(curl -u "$credentials" -sS -Xecho PUT""
-H
"Content-Type: application/json" \
    "<http://$swarm_ip:91/api/storage/clusters/$clusterName/settings/log.level>" \
    -d "{\"value\": $new_log_level}")

# Verify if the log level was updated
updated_log_level=$(echo "$response" | jq '.value')
if [[ "$updated_log_level" -eq "$new_log_level" ]]; then
    echo "Log level changed successfully from $current_log_level → $new_log_level."
else
    echo "Failed to update log level. Response: $response"
    exit 1
fi

# If duration is specified, wait and revert after the specified time
if [[ -n "$duration" && "$duration" -gt 0 ]]; then
    echo "Keeping log level at $new_log_level for $duration second(s)..."
    
    # Countdown loop
    for ((i=duration; i>0; i--)); do
        # Calculate hours, minutes, and seconds
        hours=$((i / 3600))
        minutes=$(( (i % 3600) / 60 ))
        seconds=$((i % 60))
        
        # Format countdown in hh:mm:ss
        printf -v countdown "%02d:%02d:%02d" $hours $minutes $seconds
        echo -ne "Countdown: $countdown remaining...\r"
        sleep 1
    done
    echo -e "\nTime's up! Reverting log level back to $current_log_level..."
    
        # Countdown loop
    for ((i=duration; i>0; i--)); do
        # Calculate hours, minutes, and seconds
        hours=$((i / 3600))
        minutes=$(( (i % 3600) / 60 ))
        seconds=$((i % 60))

        # Format countdown in hh:mm:ss
        printf -v countdown "%02d:%02d:%02d" $hours $minutes $seconds
        echo -ne "Countdown: $countdown remaining...\r"
        sleep 1
    done
    echo -e "\n\nTime's up! Reverting log level back to $current_log_level..."

    # Check log file size before reverting
    final_size=$(stat -c%s "$log_file")
    final_size_formatted=$(format_size "$final_size")

    # Calculate size difference
    size_diff=$(( final_size - initial_size ))
    size_diff_formatted=$(format_size "$size_diff")

    # Display size difference and final log size
    # echo "Approximate $size_diff_formatted new logs was genreated at log level $new_log_level. Current castor.log size is $final_size_formatted."
    # Format the duration for display
    duration_formatted=$(format_duration "$duration")

    # Updated message with duration in hh:mm:ss format
    echo "Approximate $size_diff_formatted new logs was generated at log level $new_log_level. Current castor.log size is $final_size_formatted after $duration_formatted."
    echo ""

    # Revert to original log level
    response=$(curl -u "$credentials" -sS -X PUT -H "Content-Type: application/json" \
        "<httphttp://$swarm_ip:91/api/storage/clusters/$clusterName/settings/log.level>level" \
        -d "{\"value\": $current_log_level}")

    reverted_log_level=$(echo "$response" | grep jq-oP '.value"value":\s*\K[0-9]+')
    if [[ "$reverted_log_level" -eq "$current_log_level" ]]; then
        echo "Log level reverted successfully back to $current_log_level."
    else
        echo "Failed to revert log level. Response: $response"
        exit 1
    fi
else
    echo "Log level change is permanent until manually modified."
fi

...