-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnics_capture_summary_all_days.sh
More file actions
187 lines (151 loc) · 5.45 KB
/
nics_capture_summary_all_days.sh
File metadata and controls
187 lines (151 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env bash
set -euo pipefail
# ============================================================
# NICS - Rolling PCAP Summary (ALL DAYS)
# - Entra en full_scenario_captures
# - Detecta carpetas YYYYMMDD
# - Genera 1 resumen TXT + 1 CSV por cada día, por separado
# - Imprime el resumen por pantalla (además de guardarlo)
#
# Uso:
# sudo bash nics_capture_summary_all_days.sh [ROLLING_ROOT]
#
# Ejemplos:
# sudo bash nics_capture_summary_all_days.sh
# sudo bash nics_capture_summary_all_days.sh /tmp/nics_captures/full_scenario_rolling
# ============================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$SCRIPT_DIR" # <-- CORRECTO si el script está en la raíz del repo
ROLLING_ROOT="${1:-$REPO_ROOT/app_core/infrastructure/ics_traffic/captures/full_scenario_captures}"
# deps mínimos
for c in find awk sort head tail uniq wc stat date tcpdump tee; do
command -v "$c" >/dev/null 2>&1 || { echo "[ERROR] Missing command: $c"; exit 1; }
done
bytes_h() {
local b="${1:-0}"
awk -v b="$b" 'BEGIN{
split("B KB MB GB TB",u);
i=1;
while(b>=1024 && i<5){b/=1024;i++}
printf "%.2f %s", b, u[i]
}'
}
pcap_first_ts() {
local f="$1"
tcpdump -tt -nn -r "$f" -c 1 2>/dev/null | awk '{print $1; exit}'
}
pcap_last_ts() {
local f="$1"
tcpdump -tt -nn -r "$f" 2>/dev/null | tail -n 1 | awk '{print $1}'
}
epoch_to_utc() {
local e="$1"
[[ -z "${e:-}" ]] && { echo ""; return; }
local sec="${e%%.*}"
[[ -z "${sec:-}" ]] && { echo ""; return; }
date -u -d "@$sec" +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || echo ""
}
summarize_day_dir() {
local DAY_DIR="$1"
local DAY_NAME
DAY_NAME="$(basename "$DAY_DIR")"
mapfile -t PCAPS < <(find "$DAY_DIR" -type f -name "*.pcap" | sort)
if [[ ${#PCAPS[@]} -eq 0 ]]; then
echo "[WARN] $DAY_NAME: no PCAPs found. Skipping."
return 0
fi
local UTC_TAG OUT_TXT OUT_CSV
UTC_TAG="$(date -u +%Y%m%d_%H%M%SZ)"
OUT_TXT="$DAY_DIR/capture_summary_${DAY_NAME}_${UTC_TAG}.txt"
OUT_CSV="$DAY_DIR/capture_summary_${DAY_NAME}_${UTC_TAG}.csv"
echo "[INFO] Day=$DAY_NAME -> $OUT_TXT / $OUT_CSV"
echo "iface,filename,bytes,mtime_utc,first_pkt_epoch,first_pkt_utc,last_pkt_epoch,last_pkt_utc,pkts_estimate" > "$OUT_CSV"
local TOTAL_BYTES=0 TOTAL_FILES=0
declare -A IF_BYTES
declare -A IF_FILES
for f in "${PCAPS[@]}"; do
TOTAL_FILES=$((TOTAL_FILES+1))
local sz
sz=$(stat -c %s "$f" 2>/dev/null || echo 0)
TOTAL_BYTES=$((TOTAL_BYTES+sz))
local rel iface
rel="${f#$DAY_DIR/}"
iface="$(echo "$rel" | awk -F/ '{print $1}')"
if [[ "$iface" == "$rel" ]]; then iface="unknown"; fi
IF_BYTES["$iface"]=$(( ${IF_BYTES["$iface"]:-0} + sz ))
IF_FILES["$iface"]=$(( ${IF_FILES["$iface"]:-0} + 1 ))
done
local SUMMARY
SUMMARY="$(
{
echo "============================================================"
echo "NICS Rolling Capture Summary"
echo "Day folder: $DAY_NAME"
echo "Generated (UTC): $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "Root: $DAY_DIR"
echo "Files: ${#PCAPS[@]}"
echo
echo "Per-interface totals"
echo "--------------------"
for iface in "${!IF_BYTES[@]}"; do
echo "${IF_BYTES[$iface]}|$iface|${IF_FILES[$iface]}"
done | sort -nr | while IFS="|" read -r b iface n; do
printf "%-18s files=%-5s bytes=%-12s (%s)\n" \
"$iface" "$n" "$b" "$(bytes_h "$b")"
done
echo
echo "Global totals"
echo "-------------"
echo "Total files: $TOTAL_FILES"
echo "Total bytes: $TOTAL_BYTES ($(bytes_h "$TOTAL_BYTES"))"
echo
echo "Per-PCAP details (sorted by mtime)"
echo "---------------------------------"
}
)"
echo "$SUMMARY" | tee "$OUT_TXT"
for f in "${PCAPS[@]}"; do
local rel iface base sz mtime_epoch mtime_utc first_e last_e first_utc last_utc pkts_est
rel="${f#$DAY_DIR/}"
iface="$(echo "$rel" | awk -F/ '{print $1}')"
if [[ "$iface" == "$rel" ]]; then iface="unknown"; fi
base="$(basename "$f")"
sz=$(stat -c %s "$f" 2>/dev/null || echo 0)
mtime_epoch=$(stat -c %Y "$f" 2>/dev/null || echo "")
mtime_utc=""
if [[ -n "$mtime_epoch" ]]; then
mtime_utc="$(date -u -d "@$mtime_epoch" +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || echo "")"
fi
first_e="$(pcap_first_ts "$f" || true)"
last_e="$(pcap_last_ts "$f" || true)"
first_utc="$(epoch_to_utc "$first_e")"
last_utc="$(epoch_to_utc "$last_e")"
pkts_est=$(( sz / 120 ))
if [[ "$sz" -lt 200 ]]; then pkts_est=0; fi
printf "%s %-18s %-45s %12s (%-10s) first=%-20s last=%-20s\n" \
"$mtime_utc" "$iface" "$base" "$sz" "$(bytes_h "$sz")" "${first_utc:-}" "${last_utc:-}" \
| tee -a "$OUT_TXT"
echo "$iface,$base,$sz,$mtime_utc,${first_e:-},${first_utc:-},${last_e:-},${last_utc:-},$pkts_est" >> "$OUT_CSV"
done
echo "[OK] Day=$DAY_NAME -> summaries written inside $DAY_DIR"
echo
}
# ---------------- main ----------------
[[ -d "$ROLLING_ROOT" ]] || { echo "[ERROR] Not a directory: $ROLLING_ROOT"; exit 1; }
mapfile -t DAY_DIRS < <(
find "$ROLLING_ROOT" -maxdepth 1 -mindepth 1 -type d -printf "%f\n" \
| awk '/^[0-9]{8}$/' \
| sort
)
if [[ ${#DAY_DIRS[@]} -eq 0 ]]; then
echo "[ERROR] No day folders (YYYYMMDD) found under: $ROLLING_ROOT"
exit 1
fi
echo "[INFO] Rolling root: $ROLLING_ROOT"
echo "[INFO] Days found: ${#DAY_DIRS[@]}"
echo
for d in "${DAY_DIRS[@]}"; do
summarize_day_dir "$ROLLING_ROOT/$d"
done
echo
echo "[DONE] All days summarized."