Last active
January 27, 2023 19:44
-
-
Save inthuriel/45a98368b8431280c56a27538876deb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# This script get all logs from all containers in the namespace | |
# Usage | |
# ./all-logs.sh -n [namespace] -s [hours] | |
# parse args | |
while getopts ":n:s:" opt; do | |
case $opt in | |
n) namespace="$OPTARG" | |
;; | |
s) since="$OPTARG" | |
;; | |
\?) echo "Invalid option -$OPTARG" >&2 | |
;; | |
esac | |
done | |
# set namespace we want search in | |
ENV="${namespace:-default}" | |
# set how far in past we want to search logs | |
LOGS_SINCE="${since:-24}h" | |
# set tmp folder for logs | |
LOGS_TMP="$(mktemp -d -t namespace-logs-XXXXX)" | |
# print initial message | |
echo "Searching logs since ${LOGS_SINCE} in namespace ${ENV}" | |
# get all pods | |
for pod in $(kubectl get pods -n "${ENV}" --no-headers -o=custom-columns=NAME:.metadata.name); do | |
# get all containers in pods | |
for container in $(kubectl get pods -n "${ENV}" -o jsonpath='{.spec.containers[*].name}' "$pod"); do | |
echo "Obtaining logs from: ${pod}/${container}. Saving to: ${LOGS_TMP}/${pod}_${container}.log"; | |
kubectl logs -n "${ENV}" --since="${LOGS_SINCE}" -c "${container}" "${pod}" > "${LOGS_TMP}/${pod}_${container}.log" | |
done; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment