diff --git a/avail.sh b/avail.sh new file mode 100755 index 0000000..aafeb75 --- /dev/null +++ b/avail.sh @@ -0,0 +1,246 @@ +#!/bin/bash + +[[ -e ${HOME}/.bashtools/bashtools.sh ]] && . ${HOME}/.bashtools/bashtools.sh +if [[ -n $HAVE_BASHTOOLS ]]; then + print1="csecho" + print2="inform" +else + print1="echo" + print2="echo" +fi + +function usage() { + echo "usage: $0 [-n] mode v1 [v2] ... [v3]" + echo + echo " -n: plaintext mode, no ansi colours" + echo + echo "Valid modes:" + echo " s = systems in series" + echo " v1 = system1 availability%" + echo " v2 = system1 availability%" + echo " ..." + echo " p = systems in parallel" + echo " v1 = one system's availability%" + echo " v2 = number of systems" + echo " i = individual system" + echo " v1 = mtbr (mean time between failures)" + echo " v2 = mttr (mean time to repair)" + echo "" +} + +function line() { + local len fmt out + len=$1 + cmd="printf %0.s- {1..${len}}" + out=$(bash -c "$cmd") + echo "$out" +} + + +if [[ $1 == "-n" ]]; then + HAVE_BASHTOOLS="" + shift +fi + +mode=$1 +shift + +if [[ $mode == "s" ]]; then + if [[ $# -lt 1 ]]; then + error "usage: $0 s sys1uptime sys2uptime ... sysXuptime" + exit 1 + fi + diagram="[client]\n" + diagram="${diagram} |\n" + diagram="${diagram}[^b${1}%^p uptime]\n" + eq="(${1}/100)" + shift + while [[ $# -ge 1 ]]; do + diagram="${diagram} |\n" + diagram="${diagram}[^b${1}%^p uptime]\n" + eq="${eq} * (${1}/100)" + shift + done + diagram="${diagram} |\n" + diagram="${diagram}[server]\n" + eq="${eq} * 100" + answer=$(echo "scale=9; $eq" | bc) + answershort=$(echo "$answer" | tr -dc '9.') + nines=$(echo $answershort | tr -d -C 9 | wc -c | tr -d ' ') + [[ $nines -eq 1 ]] && ess="" || ess="s" + ninestext="${nines} nine${ess}" + + + if [[ -n $HAVE_BASHTOOLS ]]; then + csecho -e "$WHITE" "^b${UNDERLINE}Architecture^p\n" + csecho "$GREEN" "${diagram}" + echo + csecho -e "$WHITE" "^b${UNDERLINE}Availability^p\n" + inform "^bEquation:^p ${eq}" + inform "^bAvailability:^p ${answershort}% (^b\"$ninestext\"^p)" + echo + else + echo "Architecture" + echo "------------" + diagram=$(echo "$diagram" | sed 's/\^.//g') + echo -e "${diagram}" + echo + echo "Availability" + echo "------------" + echo "* Equation: ${eq}" + echo "* Availability: ${answershort}% (\"$ninestext\")" + echo + fi +elif [[ $mode == "p" ]]; then + if [[ $# -ne 2 ]]; then + error "usage: $0 s sysuptime nsystems" + exit 1 + fi + sysuptime=${1} + shift + nsites=${1} + shift + + text="[${sysuptime}% uptime]" + textansi="[^b${sysuptime}%^p uptime]" + len=${#text} + biglen=$(( $len * $nsites )) + biglen=$(echo "$biglen + ($nsites * 3)" | bc) + + len=$((len + 3)) + + if [[ $((len % 2)) -eq 0 ]]; then + s1=$(( $len / 2 )) + s2=$(( $s1 - 1 )) + linktext=$(printf "%${s1}s|%${s2}s" " " " ") + else + s1=$(( $len / 2 )) + linktext=$(printf "%${s1}s|%${s1}s" " " " ") + fi + + diagram="$(line $biglen)" + diagram="${diagram}\n" + if [[ $((biglen % 2)) -eq 0 ]]; then + s1=$(( $biglen / 2 )) + s2=$(( $s1 - 1 )) + upd=$(printf "[%${s1}s%${s2}s]" "client") + else + s1=$(( $biglen / 2 )) + upd=$(printf "[%${s1}s%${s1}s]" "client") + fi + diagram="${diagram}${upd}" + diagram="${diagram}\n" + diagram="${diagram}$(line $biglen)" + diagram="${diagram}\n" + # intentially skip element 0 + for ((i=0;i