Skip to content

Commit

Permalink
Total Traffic
Browse files Browse the repository at this point in the history
  • Loading branch information
ReturnFI authored Aug 20, 2024
1 parent e1df03d commit 05a4783
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions core/scripts/hysteria2/server_info.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/bash

source /etc/hysteria/core/scripts/path.sh
ONLINE_API_URL='http://127.0.0.1:25413/online'

get_secret() {
if [ ! -f "$CONFIG_FILE" ]; then
Expand All @@ -19,15 +18,25 @@ get_secret() {
echo $secret
}

cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
convert_bytes() {
local bytes=$1
if (( bytes < 1048576 )); then
echo "$(echo "scale=2; $bytes / 1024" | bc) KB"
elif (( bytes < 1073741824 )); then
echo "$(echo "scale=2; $bytes / 1048576" | bc) MB"
elif (( bytes < 1099511627776 )); then
echo "$(echo "scale=2; $bytes / 1073741824" | bc) GB"
else
echo "$(echo "scale=2; $bytes / 1099511627776" | bc) TB"
fi
}

cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
total_ram=$(free -m | awk '/Mem:/ {print $2}')
used_ram=$(free -m | awk '/Mem:/ {print $3}')

secret=$(get_secret)

online_users=$(curl -s -H "Authorization: $secret" $ONLINE_API_URL)

online_user_count=$(echo $online_users | jq 'add')

if [ "$online_user_count" == "null" ] || [ "$online_user_count" == "0" ]; then
Expand All @@ -38,3 +47,24 @@ echo "CPU Usage: $cpu_usage"
echo "Total RAM: ${total_ram}MB"
echo "Used RAM: ${used_ram}MB"
echo "Online Users: $online_user_count"
echo
echo "Total Traffic: "

if [ -f "$TRAFFIC_FILE" ]; then
total_upload=0
total_download=0

while IFS= read -r line; do
upload=$(echo $line | jq -r '.upload_bytes')
download=$(echo $line | jq -r '.download_bytes')
total_upload=$(echo "$total_upload + $upload" | bc)
total_download=$(echo "$total_download + $download" | bc)
done <<< "$(jq -c '.[]' $TRAFFIC_FILE)"

total_upload_human=$(convert_bytes $total_upload)
total_download_human=$(convert_bytes $total_download)

echo "${total_upload_human} uploaded"

echo "${total_download_human} downloaded"
fi

0 comments on commit 05a4783

Please sign in to comment.