Skip to content

Instantly share code, notes, and snippets.

@freyes
Forked from brettmilford/query-etcd.sh
Last active November 30, 2020 20:58
Show Gist options
  • Save freyes/c81106843bc757910d1e4bb161e958fc to your computer and use it in GitHub Desktop.
Save freyes/c81106843bc757910d1e4bb161e958fc to your computer and use it in GitHub Desktop.
Query etcd keys
#!/bin/bash -eux
# brettmilford: added etcd /metrics gathering
# This file is based on https://code.launchpad.net/~freyes/+git/experiment
# Modified to use xargs instead of parallel
# 'etcd.etcdctl' is provided by the etcd snap
which etcd.etcdctl
PARALLEL_REQS=20
SLEEP_SECS=0.1
KEY="/registry/services/endpoints/kube-system/kube-controller-manager"
TEMPLOG=$(mktemp).log
TEMPLOGMETRICS=$(mktemp)-metrics.log
[[ $(cat /var/snap/kube-apiserver/current/args) =~ --etcd-servers=(.[^ ]+) ]]
ETCD_ENDPOINTS=${BASH_REMATCH[1]}
echo "logging to $TEMPLOG"
echo "etcd endpoints: $ETCD_ENDPOINTS"
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
function ctrl_c() {
local DEST=query-etcd.$(date '+%Y%m%d_%H%M').log
local DESTMETRICS=query-etcd-metrics.$(date '+%Y%m%d_%H%M').log
mv $TEMPLOG $DEST
mv $TEMPLOGMETRICS $DESTMETRICS
echo -e "\noutput at $DEST \nmetrics at $DESTMETRICS"
wait
exit 0
}
set +x
(
ts_and_log() {
local polled_endpoint=$1
while read line; do
# filter comments
[[ "$line" =~ ^\# ]] || echo "$(date +%d-%m-%YT%H:%m:%S) $polled_endpoint $line" 2>&1 >> $TEMPLOGMETRICS
done
}
while true; do
(for ENDPOINT in $(echo $ETCD_ENDPOINTS | sed 's/,/ /g' | tr -d '"'); do
curl -s -X GET "${ENDPOINT}/metrics" --cacert /root/cdk/etcd/client-ca.pem --cert /root/cdk/etcd/client-cert.pem --key /root/cdk/etcd/client-key.pem | ts_and_log $ENDPOINT
done)
/bin/sleep 5
done
) &
ETCD_ENDPOINT=$(echo $ETCD_ENDPOINTS | cut -d',' -f1 | tr -d '"')
while true; do
seq $PARALLEL_REQS | xargs -I {} -P$PARALLEL_REQS sudo etcd.etcdctl --cacert /root/cdk/etcd/client-ca.pem --cert /root/cdk/etcd/client-cert.pem --key /root/cdk/etcd/client-key.pem --endpoints=$ETCD_ENDPOINT get -w json $KEY 2>&1 >> $TEMPLOG
/bin/sleep $SLEEP_SECS
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment