github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/tests/load-tests/collect-pod-logs/snapshot-collect-pod-logs.sh (about)

     1  #!/bin/bash
     2  
     3  set +e
     4  
     5   # Log directory configuration
     6   # Format:  ./snapshot-collect-pod-logs.sh logs-${USER}-$(date +%Y-%m-%d)  
     7  
     8   # if no parameter given, a default will be used for the log_dir
     9   if [ -z "$1" ]; then
    10     # Set default log directory
    11     timestamp=$(date +%Y-%m-%d-%H-%M-%S)
    12     log_dir="logs-${USER}-$timestamp"
    13   else
    14     # Use log directory provided as script argument
    15     log_dir="$1"
    16   fi
    17  
    18  interval=$(( 5 * 60 ))  # 5 minutes
    19  
    20  # Flag tosignal if a signal has been received 
    21  signal_received=0
    22  
    23  # Function to handle signals
    24  function handle_signal() {
    25      echo "Script terminated by user"
    26      signal_received=1
    27  }
    28  
    29  # Trap signals
    30  trap 'handle_signal' SIGINT SIGTERM
    31  
    32  while true; do
    33      # check if a signal has been received
    34      if [[ signal_received -eq 1 ]]; then
    35        echo "Exiting safely"
    36        exit 0
    37      fi
    38  
    39      echo "Wait for the interval in seconds before processing the logs gathering loop.."
    40      sleep $interval
    41  
    42      # check if there are any namespaces available
    43      namespaces=$(kubectl get ns -o jsonpath='{.items[*].metadata.name}')
    44      if [[ -z $namespaces ]]; then
    45        echo "No namespaces available, Skipping this iteration"
    46        echo "Wait for the interval in seconds before processing the logs gathering loop.."
    47        sleep $interval
    48  
    49        continue
    50      fi
    51  
    52      # Iterate through namespaces
    53      echo "$namespaces" | tr ' ' '\n' | while read -r namespace; do
    54          echo "Processing namespace: $namespace"
    55  
    56          # Create namespace directory if it doesn't exist
    57          mkdir -p "$log_dir/$namespace"
    58  
    59          # get pods in namespace
    60          pods=$(kubectl get pods -n $namespace -o jsonpath='{.items[*].metadata.name}')
    61          if [[ -z $pods ]]; then
    62            echo "No pods in namespace: $namespace, iterate with next namespace if exists"
    63            continue
    64          fi
    65  
    66          # Iterate through pods
    67          echo "$pods" | tr ' ' '\n' | while read -r pod; do
    68              echo "  Processing pod: $pod"
    69  
    70              # Create pod directory if it doesn't exist
    71              mkdir -p "$log_dir/$namespace/$pod"
    72  
    73              # get containers in the pod
    74              containers=$(kubectl get pods -n $namespace $pod -o jsonpath='{.spec.containers[*].name}')
    75              if [[ -z $containers ]]; then
    76                echo "No containers exist in pod: $namespace/$pod, iterate with next pod if exists"
    77                continue
    78              fi
    79  
    80              # Iterate through containers
    81              for container in $containers; do
    82                  echo "    Processing container: $container"
    83                  container_dir="$log_dir/$namespace/$pod/$container"
    84  
    85                  # Create container directory if it doesn't exist
    86                  mkdir -p "$container_dir"
    87                  
    88                  # Generate a new timestamp
    89                  timestamp=$(date +%Y%m%d%H%M%S)
    90  
    91                  # Fetch container logs
    92                  log=$(kubectl logs -n $namespace $pod $container)
    93                  
    94                  # If log is not empty, delete existing logs and write the new log to a file
    95                  if [[ ! -z "$log" ]]; then
    96                      rm -f "$container_dir"/*.log
    97                      echo "$log" > "$container_dir/$container-$timestamp.log"
    98                  fi
    99              done
   100          done
   101      done
   102  done