Skip to content

Instantly share code, notes, and snippets.

@dosaboy
Last active December 5, 2018 21:34
Show Gist options
  • Select an option

  • Save dosaboy/d7dde2223f065cc2a93a7635962ed82a to your computer and use it in GitHub Desktop.

Select an option

Save dosaboy/d7dde2223f065cc2a93a7635962ed82a to your computer and use it in GitHub Desktop.
#!/bin/bash -eu
#
# Origin: https://gist.github.com/dosaboy/d7dde2223f065cc2a93a7635962ed82a
#
# Authors:
# - [email protected]
# - [email protected]
#
# Description:
#
# Tool to display Swift ring management info from Swift Juju Charm relations.
#
# To run do:
#
# . ./swift-ring-analyser.sh
# show_proxy_settings
# show_storage_settings
#
CLUSTER_RID=
get_proxy_app_units ()
{
local app=$1
local _units=()
readarray -t _units<<<"`juju status --format=json| jq -r \".applications | to_entries | map(select(.key | match(\\"$app\\$\\";\\"i\\"))) | map(.value)[].units | keys[]\"`"
echo "${#_units[@]} $app unit(s): ${_units[@]}" 1>&2
echo ${_units[@]}
}
get_storage_app_units ()
{
local app=$1
local _units=()
readarray -t _units<<<"`juju status --format=json| jq -r \".applications | to_entries | map(select(.key | match(\\"$app\\$\\";\\"i\\"))) | map(.value)[].units | keys[]\"`"
echo "${#_units[@]} $app units: ${_units[@]}" 1>&2
echo ${_units[@]}
}
get_proxy_units ()
{
local _units=()
readarray -t apps<<<"`juju status --format=json| jq -r '.applications | to_entries | map(select(.key | match("swift-proxy$";"i"))) | map(.key)[]'`"
for app in ${apps[@]}; do
_units+=( `get_proxy_app_units $app` )
done
echo ${_units[@]}
}
get_storage_apps ()
{
juju status --format=json| jq -r '.applications | to_entries | map(select(.key | match("swift-storage.+";"i"))) | map(.key)[]'
}
get_storage_units ()
{
local _units=()
for app in `get_storage_apps`; do
_units+=( `get_storage_app_units $app` )
done
echo ${_units[@]}
}
check_data ()
{
datafile=$1
related_unit=$2
cat << EOF | python
import json
import re
from datetime import datetime
WHITELIST = ['.+-proxy-service.*',
'leader-changed-notification',
'resync-request.*',
'.*broker.*',
'peers-only',
'sync-only-builders',
'rings_url']
for line in open("$datafile").readlines():
root = json.loads(line)
for top_dict in root:
for k, v in top_dict.iteritems():
if k == "UnitId":
unit = v.strip()
if "$related_unit":
print "\n{} <- {}:".format(unit, "$related_unit")
else:
print "\n{}:".format(unit)
continue
elif k == "Stdout":
subdict = json.loads(v.strip())
data = False
for key, val in subdict.iteritems():
if not any([re.search(_key, key) for _key in WHITELIST]):
continue
if key in ['broker-timestamp', 'timestamp']:
val = datetime.fromtimestamp(float(val))
val = "\033[33m{}\033[0m".format(val)
print " + {} = {}".format(key, val)
data = True
if not data:
print " -"
EOF
}
show_proxy_settings ()
{
out=`mktemp`
echo "Fetching swift-proxy peer relation settings"
for unit in `get_proxy_units`; do
[ -n "$CLUSTER_RID" ] || CLUSTER_RID="`juju run --unit $unit 'relation-ids cluster'`"
juju run --format=json --application swift-proxy "relation-get --format=json -r $CLUSTER_RID - $unit" > $out
check_data $out $unit
echo -e ""
done
rm $out
}
show_storage_settings ()
{
out=`mktemp`
echo "Fetching swift-storage relation settings"
declare -a p_units=( `get_proxy_units` )
echo ""
for app in `get_storage_apps`; do
declare -a units=( `get_storage_app_units $app` )
readarray -t storage_rids<<<"`juju run --unit ${units[0]} 'relation-ids swift-storage'| sort -r`"
((${#storage_rids[@]}>1)) && \
{ echo "INFO: multiple storage rids found (${storage_rids[@]}) on unit ${units[0]} - using highest"; }
storage_rid=${storage_rids[0]}
[ -n "$storage_rid" ] || { echo "ERROR: unable to retreive storage rid"; continue; }
for p_unit in ${p_units[@]}; do
juju run --format=json --application $app "relation-get --format=json -r $storage_rid - $p_unit" > $out
check_data $out $p_unit || cat $out
done
echo ""
done
rm $out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment