Last active
February 27, 2024 15:26
-
-
Save Opa-/619d26afafd85484e23d7e6594028efe to your computer and use it in GitHub Desktop.
endlessh winner table
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 | |
TOP=$(journalctl -u endlessh.service | grep "time=" | awk '{split($11, time, "="); split($8, ip, ":"); print ip[4], time[2]}' | awk '{arr[$1]+=$2} END {for (i in arr) {print i,arr[i]}}' | sort -n -k2) | |
format_duration() { | |
local seconds=$(printf "%.3f" $1) # Convert seconds to 3 decimal places | |
local milliseconds=$(echo "scale=0; $seconds * 1000" | bc) # Convert seconds to milliseconds | |
local milliseconds=${milliseconds%.*} # Remove decimal part | |
# Calculate days, hours, minutes, and seconds | |
local days=$((milliseconds / 86400000)) | |
local hours=$(( (milliseconds % 86400000) / 3600000 )) | |
local minutes=$(( (milliseconds % 3600000) / 60000 )) | |
local seconds=$(( (milliseconds % 60000) / 1000 )) | |
# Using printf to format the duration and capturing the output in a variable | |
local formatted_duration=$(printf "%d days, %02d:%02d:%02d\n" $days $hours $minutes $seconds) | |
echo $formatted_duration | |
} | |
TABLE="" | |
TOTAL="0" | |
while IFS= read -r WINNER; do | |
IP=$(echo $WINNER | cut -d' ' -f1) | |
TIME=$(echo $WINNER | cut -d' ' -f2) | |
LOC=$(geoiplookup $IP | cut -d':' -f2 | tr "\n" " ") | |
TIME_FMT=$(format_duration $TIME) | |
TABLE="$TABLE;$IP;$LOC;$TIME_FMT\n" | |
TOTAL=$(echo "$TOTAL + TIME" | bc) | |
done <<< "$TOP" | |
echo -e $TABLE | tail -n 30 | column --table --separator ';' | |
format_duration $TOTAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output