Skip to content

Instantly share code, notes, and snippets.

@mateiidavid
Last active December 15, 2022 11:35
Show Gist options
  • Save mateiidavid/04e3e686e1d929a0fd2a3a7c792df09a to your computer and use it in GitHub Desktop.
Save mateiidavid/04e3e686e1d929a0fd2a3a7c792df09a to your computer and use it in GitHub Desktop.
Script to fetch linkerd-proxy logs and metrics for a set of Kubernetes pods with a common label value
#!/usr/bin/env bash
print_summary() {
local name="${0##*/}"
printf "Collect proxy-logs and diagnostics\n\nUsage:\n %2s ${name} --namespace name [--selector \"key=value\"] [--o /output/path]\n\n"
printf "Examples:\n"
printf "%2s#Collect logs and diagnostics for all pods in 'test'\n%2s${name} --namespace test\n\n"
printf "%2s#Collect logs and diagnostics for pods with 'app=test' label\n%2s${name} --namespace test --selector \"app=test\"\n\n"
printf "%2s#Output files in a different path\n%2s${name} --namespace 'test' --o /path/to/files\n\n"
printf "NOTE:\n%2s*%1sFiles will be named based on the pod and will include either a '.dg' or a '.log' extension\n%2s*%1sSelector supports only one k=v pair\n\n"
}
mk_opts() {
export namespace=''
export selector=''
export path=''
if [ $# -eq 0 ]; then
print_summary "$0"
exit 0
fi
while [ $# -ne 0 ]; do
case $1 in
-h|--help)
print_summary "$0"
exit 0
;;
-n|--namespace)
namespace="$2"
if [ -z "$namespace" ]; then
echo "Error: argument for --namespace not specified" >&2
exit 64
fi
shift
shift
;;
-s|--selector)
selector="$2"
shift
shift
;;
-o|--output)
path="$2"
shift
shift
;;
*)
esac
done
if [ -z "$namespace" ]; then
echo 'Error: namespace argument required' >&2
exit 64
fi
if [ -z "$path" ]; then
path="$(dirname "$0")"
fi
}
_kubectl_get_pods() {
local ns=$1
local select=$2
echo $(kubectl get po -n $ns --selector $select -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}')
}
run() {
d="$1"
ns="$2"
select="$3"
echo "Fetching logs and dg for pods with label '$select' in namespace '$ns'"
local count=0
for name in $(_kubectl_get_pods "$ns" "$select"); do
echo "Extracting logs for $name..."
$(kubectl logs -n "$ns" "$name" linkerd-proxy > "$path/$name-$d.log")
echo "Extracting diagnostics for $name..."
$(linkerd dg proxy-metrics -n "$ns" "po/$name" > "$path/$name-$d.dg")
((count+=2))
done
echo "Saved $count files in $path"
echo "Done"
}
mk_opts "$@"
date="$(date +'%H:%M:%S')"
run "$date" "$namespace" "$selector"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment