#!/bin/bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
source ${SCRIPT_DIR}/common

# Expect base collection path as an argument
BASE_COLLECTION_PATH=$1

# Use PWD as base path if no argument is passed
if [ "${BASE_COLLECTION_PATH}" = "" ]; then
    BASE_COLLECTION_PATH=$(pwd)
fi

NAMESPACE=${2:-openshift-logging}


CLO_COLLECTION_PATH="$BASE_COLLECTION_PATH/cluster-logging"
collector_folder="$CLO_COLLECTION_PATH/collector"

log "BEGIN gather_collection_resources..."

check_collector_connectivity() {
  local pod=$1
  echo "--Connectivity between $pod and elasticsearch" >> $collector_folder/$pod

  local es_host=$(oc -n $NAMESPACE  get pod $pod  -o jsonpath='{.spec.containers[0].env[?(@.name=="ES_HOST")].value}')
  local es_port=$(oc -n $NAMESPACE  get pod $pod  -o jsonpath='{.spec.containers[0].env[?(@.name=="ES_PORT")].value}')

  local collector=fluent
  local container=$2
  echo "  with ca" >> $collector_folder/$pod
  oc -n $NAMESPACE exec $pod -c $container --cache-dir=${KUBECACHEDIR} -- curl -ILvs --key /etc/$collector/keys/key --cert /etc/$collector/keys/cert --cacert /etc/$collector/keys/ca -XGET https://$es_host:$es_port & >> $collector_folder/$pod

  echo "  without ca" >> $collector_folder/$pod
  oc -n $NAMESPACE exec $pod -c $container --cache-dir=${KUBECACHEDIR} -- curl -ILkvs --key /etc/$collector/keys/key --cert /etc/$collector/keys/cert -XGET https://$es_host:$es_port & >> $collector_folder/$pod
}

check_collector_persistence() {
  local pod=$1
  echo "--Persistence stats for pod $pod" >> $collector_folder/$pod

  local collector=$2
  local fbstoragePath=$(oc -n $NAMESPACE get daemonset $collector -o jsonpath='{.spec.template.spec.containers[0].volumeMounts[?(@.name=="filebufferstorage")].mountPath}')

  if [ -z "$fbstoragePath" ] ; then
    echo "No filebuffer storage defined" >>  $collector_folder/$pod
  else
    oc -n $NAMESPACE exec $pod -c $container --cache-dir=${KUBECACHEDIR} -- df -h $fbstoragePath >> $collector_folder/$pod
    oc -n $NAMESPACE exec $pod -c $container --cache-dir=${KUBECACHEDIR} -- ls -lr $fbstoragePath >> $collector_folder/$pod
  fi
}

log "if the collector 'fluentd' or 'collector' is missing thats OK"
log "this must gather is trying to cover all cases for name migration"
for collector in "fluentd" "collector"; do
  yaml="$(oc -n $NAMESPACE get ds/$collector --ignore-not-found -o yaml)"
  if [ "$yaml" == "" ] ; then
    continue
  fi

  is_fluent=$(echo $yaml|grep fluent)

  log "Gathering data for collection component: $collector"
  mkdir -p $collector_folder

  log "-- Checking Collector health: $collector"
  pods="$(oc -n $NAMESPACE get pods -l component=collector -o jsonpath='{.items[*].metadata.name}')"
  for pod in $pods
  do
      log "---- Collector pod: $pod"
      oc -n $NAMESPACE describe pod/$pod > $collector_folder/$pod.describe --cache-dir=${KUBECACHEDIR} 2>&1
      if [ "$is_fluent" != "" ] ; then
        check_collector_connectivity $pod $collector
        check_collector_persistence $pod $collector
        oc -n $NAMESPACE --cache-dir=${KUBECACHEDIR} exec $pod -c $collector -- ls -l /var/lib/fluentd/default  > $collector_folder/$pod.es-buffers.txt
        oc -n $NAMESPACE --cache-dir=${KUBECACHEDIR} exec $pod -c $collector -- ls -l /var/lib/fluentd/retry_default > $collector_folder/$pod.buffers.es-retry.txt
        echo "$pod" >> $collector_folder/output-buffer-size.txt
        echo "---------------" >> $collector_folder/output-buffer-size.txt
        oc -n $NAMESPACE --cache-dir=${KUBECACHEDIR} exec $pod -c $collector -- bash -c ' du -sh /var/lib/fluentd/*' >> $collector_folder/output-buffer-size.txt
      fi
  done
done
log "END gather_collection_resources..."
