Skip to content

Instantly share code, notes, and snippets.

@IMMORTALxJO
Created October 23, 2020 12:26
Show Gist options
  • Save IMMORTALxJO/22784991ad3011f6ac0fc0eb687faeb0 to your computer and use it in GitHub Desktop.
Save IMMORTALxJO/22784991ad3011f6ac0fc0eb687faeb0 to your computer and use it in GitHub Desktop.
Swarm bitseq visualization
#!/bin/bash
PRINT_INTERVAL="${PRINT_INTERVAL:-10}"
NETWORK_NAME="${1}"
if [[ -z "${NETWORK_NAME}" ]]; then
echo "usage: $0 <network_name>"
exit 0
fi;
set -eu
NETWORK_SUBNET=$(docker network inspect "${NETWORK_NAME}" --format '{{json .IPAM.Config}}' | jq -r '.[0].Subnet')
### collector
# convert Uint32 in binary string
toBinaryPOSIX(){
n="$1"
bit=""
while [ "$n" -gt 0 ]; do
bit="$(( n&1 ))$bit";
: $(( n >>= 1 ))
done;
printf "00000000000000000000000000000000%s" "$bit" | rev | cut -c1-32 | rev
}
# read journalctl stream and print graph
collector() {
echo "Collector process has been started ..."
local subnet="$1"
local subnet_prefix=$(echo "${subnet}" | cut -d'.' -f1,2,3)
local prev_binseq=""
local actions_log=()
local binseq=""
local block_len=3
local releasedAddress=""
local recordTimestamp=""
local result=""
local drawRecSize=16
[[ $(echo "${subnet}" | cut -d'/' -f2) == "16" ]] && block_len=4 && drawRecSize=256
stdbuf -i0 -o0 -e0 journalctl --since '1 hour ago' -f -u docker | stdbuf -i0 -o0 -e0 grep "${subnet}.*(0x.*)" | while read logentry; do
recordTimestamp=$(echo "${logentry}" | cut -d'"' -f2)
result=""
binseq=$(
echo "${logentry}" | grep -o '(0x.*)' | tr '-' '\n' | sed 's/[>(,)]//g' | while read l; do
z1=$(echo "${l}" | cut -d' ' -f1)
z2=$(echo "${l}" | cut -d' ' -f2)
echo -n $(toBinaryPOSIX $(($z1)))
z2=$((z2-1))
while [[ "${z2}" -gt 0 ]]; do echo -n 00000000000000000000000000000000; z2=$((z2-1));done;
done;
)
binseqsize="${#binseq}"
if [[ $(echo "${logentry}" | cut -d' ' -f8 | cut -d'"' -f2) == "Released" ]]; then
releasedAddress=$(echo "${logentry}" | cut -d' ' -f11 | cut -d'.' -f4)
binseq=$(echo "${binseq}" | sed "s/./0/$((releasedAddress+1))")
fi;
if [[ "${prev_binseq}" == "" ]]; then
prev_binseq="${binseq}"
fi;
for i in $(seq 0 "$((binseqsize-1))");do
ni=$(printf "%0${block_len}d" "${i}" )
ni=" ${ni} "
if [[ "${prev_binseq:i:1}" == "${binseq:i:1}" ]]; then
if [[ "${binseq:i:1}" == "0" ]]; then
result="${result}\e[94m${ni}"
else
result="${result}\e[32m${ni}"
fi;
elif [[ "${binseq:i:1}" == "1" ]]; then
actions_log=("${recordTimestamp}: ${subnet_prefix}.${i} added" "${actions_log[@]}")
result="${result}\e[92m${ni}"
elif [[ "${binseq:i:1}" == "0" ]]; then
actions_log=("${recordTimestamp}: ${subnet_prefix}.${i} removed" "${actions_log[@]}")
result="${result}\e[91m${ni}"
fi;
if [[ $(((i+1)%drawRecSize)) -eq 0 ]]; then
result="${result}\n"
fi;
done;
actions_log=("${actions_log[@]:0:10}")
clear
echo
echo -e "\e[39mLast action:\n${logentry}\nBinary view:\n${binseq}\n${result}\e[39m\nActions log:\n"
printf "%s\n" "${actions_log[@]}"
prev_binseq="${binseq}"
done;
}
collector "${NETWORK_SUBNET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment