diff --git a/SnowflakeTraffic.sh b/SnowflakeTraffic.sh index a1b1cbd..0cc93e4 100644 --- a/SnowflakeTraffic.sh +++ b/SnowflakeTraffic.sh @@ -1,38 +1,98 @@ -#!/bin/bash +#!/usr/bin/env bash +set -euo pipefail -docker logs snowflake-proxy 2>&1 | awk ' +# Standardwerte für die Parameter (leer = kein Filter) +START_DATE="${1:-}" +END_DATE="${2:-}" + +# Validierung der Datumseingaben +validate_date() { + local date="$1" + [[ -z "$date" ]] && return 0 + if [[ ! "$date" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then + echo "Fehler: Ungültiges Datumsformat für '$date' (erwartet: YYYY-MM-DD)" >&2 + exit 1 + fi +} + +validate_date "$START_DATE" +validate_date "$END_DATE" + +docker logs snowflake-proxy 2>&1 | awk -v start_date="$START_DATE" -v end_date="$END_DATE" ' function human_readable(kb, units, size, i) { split("KB MB GB TB PB", units) size = kb - for (i = 1; size >= 1024 && i < 5; i++) { + for (i = 1; size >= 1024 && i <= length(units); i++) { size = size / 1024 } return sprintf("%.2f %s", size, units[i]) } -{ - # Datumserkennung und Formatierung - if (split($1, date_part, "/") == 3) { - current_date = $1 - gsub(/\//, "-", current_date) # Ersetze / durch - - if (!first_date) first_date = current_date - last_date = current_date +function date_to_epoch(date_str, parts) { + if (split(date_str, parts, /[-\/]/) != 3) return -1 + if (parts[1] < 1970 || parts[2] < 1 || parts[2] > 12 || parts[3] < 1 || parts[3] > 31) return -1 + return mktime(parts[1] " " parts[2] " " parts[3] " 00 00 00") +} + +BEGIN { + if (start_date != "" && date_to_epoch(start_date) == -1) { + print "Ungültiges Startdatum Format" > "/dev/stderr" + exit 1 + } + if (end_date != "" && date_to_epoch(end_date) == -1) { + print "Ungültiges Enddatum Format" > "/dev/stderr" + exit 1 } } -/Traffic Relayed ↓ [0-9]+ KB .*↑ [0-9]+ KB/ { - for (i = 1; i <= NF; i++) { - if ($i == "↓") { down += $(i+1) } - if ($i == "↑") { up += $(i+1) } +{ + include_line = 0 # Zurücksetzen bei jeder neuen Zeile + + # Datumserkennung und Formatierung + if (match($0, /([0-9]{4}[-\/][0-9]{2}[-\/][0-9]{2})/, date_match)) { + current_date = date_match[1] + gsub(/[\/]/, "-", current_date) # Standardisiert auf YYYY-MM-DD + + if (!first_date) first_date = current_date + last_date = current_date + + current_epoch = date_to_epoch(current_date) + if (current_epoch == -1) next # Ungültiges Datum überspringen + + include_line = 1 + + if (start_date != "") { + start_epoch = date_to_epoch(start_date) + if (current_epoch < start_epoch) include_line = 0 + } + + if (end_date != "" && include_line) { + end_epoch = date_to_epoch(end_date) + if (current_epoch > end_epoch) include_line = 0 + } + } +} + +include_line && /Traffic Relayed ↓ [0-9]+ KB .*↑ [0-9]+ KB/ { + # Sicherere Extraktion der Werte mit regulären Ausdrücken + if (match($0, /↓ ([0-9]+) KB.*↑ ([0-9]+) KB/, traffic)) { + down += traffic[1] + up += traffic[2] } } END { printf "Statistik-Zeitraum:\n" printf "Erster Eintrag: %s\n", first_date - printf "Letzter Eintrag: %s\n\n", last_date + printf "Letzter Eintrag: %s\n", last_date - printf "Gesamte Datenmengen:\n" + if (start_date != "" || end_date != "") { + printf "Gefilterter Bereich: %s bis %s\n\n", + (start_date != "" ? start_date : "Anfang"), + (end_date != "" ? end_date : "Ende") + } + + printf "\nGesamte Datenmengen:\n" printf "Download: %d KB (%s)\n", down, human_readable(down) printf "Upload: %d KB (%s)\n", up, human_readable(up) }' \ No newline at end of file