Created
September 28, 2019 12:59
-
-
Save alphajc/0081a695589ea4a9622dad83f05b727c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
_APISERVER=127.0.0.1:10085 | |
_V2CTL=/usr/bin/v2ray/v2ctl | |
apidata () { | |
local ARGS= | |
if [[ $1 == "reset" ]]; then | |
ARGS="reset: true" | |
fi | |
$_V2CTL api --server=$_APISERVER StatsService.QueryStats "${ARGS}" \ | |
| awk '{ | |
if (match($1, /name:/)) { | |
f=1; gsub(/^"|link"$/, "", $2); | |
split($2, p, ">>>"); | |
printf "%s:%s->%s\t", p[1],p[2],p[4]; | |
} | |
else if (match($1, /value:/) && f){ f = 0; printf "%.0f\n", $2; } | |
else if (match($0, /^>$/) && f) { f = 0; print 0; } | |
}' | |
} | |
print_sum() { | |
local DATA="$1" | |
local PREFIX="$2" | |
local SORTED=$(echo "$DATA" | grep "^${PREFIX}" | sort -r) | |
local SUM=$(echo "$SORTED" | awk ' | |
/->up/{us+=$2} | |
/->down/{ds+=$2} | |
END{ | |
printf "SUM->up:\t%.0f\nSUM->down:\t%.0f\nSUM->TOTAL:\t%.0f\n", us, ds, us+ds; | |
}') | |
echo -e "${SORTED}\n${SUM}" \ | |
| numfmt --field=2 --suffix=B --to=iec \ | |
| column -t | |
} | |
DATA=$(apidata $1) | |
echo "------------Inbound----------" | |
print_sum "$DATA" "inbound" | |
echo "-----------------------------" | |
echo | |
echo "-------------User------------" | |
print_sum "$DATA" "user" | |
echo "-----------------------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment