Skip to content

Instantly share code, notes, and snippets.

@vpalmisano
Created February 27, 2020 12:11
Show Gist options
  • Save vpalmisano/1883173796edfeec8d52ff74b8184657 to your computer and use it in GitHub Desktop.
Save vpalmisano/1883173796edfeec8d52ff74b8184657 to your computer and use it in GitHub Desktop.
#!/bin/bash
csv=$1
read -d '' prog <<'EOT'
function get_var(array, name, convert_bps) {
for (i in array) {
if (array[i] ~ name) {
value = gensub("^"name":", "", "g", array[i])
if (convert_bps) {
suffix = gensub(".+([GMK]bps)$", "\\\\1", "g", value)
value = gensub("[GMK]bps$", "", "g", value)
if (suffix == "Gbps") {
value = value * 1000000
} else if (suffix == "Mbps") {
value = value * 1000
}
}
return value
}
}
}
{
if ($1 == "ESTAB") {
dest_ip_port = gensub(/[\\[:f]+([0-9\\.]+)\\]:([0-9]+)/, "\\\\1:\\\\2", "g", $5)
} else {
gsub(/^[ \\t]+/, "", $0)
$0=gensub(/ ([0-9\\.]+[GMKbps]+)/, ":\\\\1", "g", $0)
split($0, vars, " ")
cc = $1
split(get_var(vars, "rtt"), rttv, "/")
rtt = rttv[1]
rtt_dev = rttv[2]
rtt_min = get_var(vars, "minrtt")
cwnd = int(get_var(vars, "cwnd"))
bytes_sent = int(get_var(vars, "bytes_sent"))
bytes_retrans = int(get_var(vars, "bytes_retrans"))
bytes_received = int(get_var(vars, "bytes_received"))
delivery_rate = get_var(vars, "delivery_rate", 1)
if (bytes_sent > min_bytes_sent) {
#for (i in vars) print vars[i]
if (csv) {
out = sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s", \
cc, rtt, rtt_dev, rtt_min, cwnd, bytes_sent, bytes_retrans, bytes_received, delivery_rate)
} else {
out = sprintf("%s rtt/dev/min=%s/%s/%s cwnd=%s sent=%0.2fMB retrans=%.2fMB received=%.2fMB rate=%sKbps", \
cc, rtt, rtt_dev, rtt_min, cwnd,
bytes_sent / 1000000, bytes_retrans / 1000000, bytes_received / 1000000, delivery_rate)
}
arr[dest_ip_port] = out
}
}
} END {
if (csv) {
ts = strftime("%s", systime())
for (i in arr) {
print sprintf("%s,%s,%s", ts, i, arr[i])
}
} else{
for (i in arr) {
print i, arr[i]
}
}
}
EOT
#echo "$prog"
if [ "x$1" == "x1" ]; then
echo "ts,dest,cc,rtt,rtt_min,cwnd,sent_bytes,retrans_bytes,received_bytes,rate_kbps"
fi
while true; do
ss -Hnit | awk -v min_bytes_sent=10 -v csv=$csv "$prog"
sleep 1
done
@vpalmisano
Copy link
Author

Save as socket_stats.sh.
Launch using ./socket_stats.sh 1 for csv output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment