Skip to content

Instantly share code, notes, and snippets.

@bradleythughes
Created April 26, 2018 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bradleythughes/2cc91ec3f2fad4d3f0af495b9e593438 to your computer and use it in GitHub Desktop.
Save bradleythughes/2cc91ec3f2fad4d3f0af495b9e593438 to your computer and use it in GitHub Desktop.
process-turnserver-metrics.awk
#
# awk(1) script to process the turnserver's session information into interesting CloudWatch metrics. The
# output is written to stdout as JSON in the format expected by the AWS CLI.
#
# This script generates metric data for the InstanceId and AutoScalingGroupName dimensions. These values need
# to be set when calling the script:
#
# awk -v InstanceId=$instance_id -v AutoScalingGroupName=$asg_name \
# -f process-turnserver-metrics.awk input.txt > output.json
#
# Metrics generated are:
#
# - total number of sessions:
# TotalSessions
#
# - number of sessions transmitting data:
# ActiveSessions
#
# - total number of sessions by client protocol:
# ClientProtocolUDP
# ClientProtocolTCP
# ClientProtocolTLS
#
# - total number of sessions by relay protocol:
# RelayProtocolUDP
# RelayProtocolTCP
#
# - session transmission rate statistical data in bytes per second:
# ActiveSessionRate (Minimum, Maximum, Sum, SampleCount)
#
# - session age for all sessions in seconds:
# TotalSessionAge (Minimum, Maximum, Sum, SampleCount)
#
function MAX(a, b) {
return (a > b) ? a : b;
}
function MIN(a, b) {
# use -1 as a sentinel value to indicate "no value"
if (a == -1 || b == -1)
return MAX(a, b);
return (a < b) ? a : b;
}
BEGIN {
TotalSessions = 0
TotalSessionAgeMin = -1
TotalSessionAgeMax = 0
TotalSessionAgeSum = 0
ClientProtocol["UDP,"] = 0
ClientProtocol["TCP,"] = 0
# format used by turnserver v3.x
ClientProtocol["TLS,"] = 0
# formats used by turnserver v4.x (we do not report/support SCTP numbers)
ClientProtocll["SCTP,"] = 0
ClientProtocol["TLS/TCP,"] = 0
ClientProtocol["TLS/SCTP,"] = 0
RelayProtocol["UDP"] = 0
RelayProtocol["TCP"] = 0
ActiveSessions = 0
ActiveSessionRateMin = -1
ActiveSessionRateMax = 0
ActiveSessionRateSum = 0
}
#
# Sessions look like this:
#
# 1) id=000000000000075377, user <appearin:1415192116>:
# started 1279 secs ago
# expiring in 2321 secs
# client protocol TCP, relay protocol UDP
# client addr 203.0.113.0:42350, server addr 172.31.0.0:443
# relay addr 172.31.0.0:56977
# fingerprints enforced: ON
# mobile: OFF
# usage: rp=2, rb=172, sp=1, sb=120
# rate: r=0, s=0, total=0 (bytes per sec)
# peers:
# 198.51.100.0
# 172.31.0.0
#
/ id=/ {
TotalSessions++
}
/started / {
age = int($2)
TotalSessionAgeMin = MIN(TotalSessionAgeMin, age)
TotalSessionAgeMax = MAX(TotalSessionAgeMax, age)
TotalSessionAgeSum += age
}
/client protocol/ {
ClientProtocol[$3]++
RelayProtocol[$6]++
}
# Skip idle sessions. Only sessions sending data are considered active.
/rate: r=0, s=0, total=0/ { next }
/rate: / {
ActiveSessions++
# we only parse the total rate
sub(/total=/, " ", $4)
rate=int($4)
ActiveSessionRateMin = MIN(ActiveSessionRateMin, rate)
ActiveSessionRateMax = MAX(ActiveSessionRateMax, rate)
ActiveSessionRateSum = ActiveSessionRateSum + rate;
}
# Output metrics data in JSON
END {
ValueFormat="%s{\"MetricName\":\"%s\",\"Value\":%d,\"Dimensions\":[{\"Name\":\"%s\",\"Value\":\"%s\"}]}\n"
CountFormat="%s{\"MetricName\":\"%s\",\"Unit\":\"Count\",\"Value\":%d,\"Dimensions\":[{\"Name\":\"%s\",\"Value\":\"%s\"}]}\n"
StatisticValuesFormat="%s{\"MetricName\":\"%s\",\"Unit\":\"%s\",\"StatisticValues\":{\"Minimum\":%d,\"Maximum\":%d,\"Sum\":%d,\"SampleCount\":%d},\"Dimensions\":[{\"Name\":\"%s\",\"Value\":\"%s\"}]}\n"
# to be turnserver version agnostic, merge the TLS and TLS/TCP numbers
ClientProtocol["TLS,"] += ClientProtocol["TLS/TCP,"];
printf(CountFormat, "[", "TotalSessions", TotalSessions, "InstanceId", InstanceId);
printf(CountFormat, ",", "ClientProtocolUDP", ClientProtocol["UDP,"], "InstanceId", InstanceId);
printf(CountFormat, ",", "ClientProtocolTCP", ClientProtocol["TCP,"], "InstanceId", InstanceId);
printf(CountFormat, ",", "ClientProtocolTLS", ClientProtocol["TLS,"], "InstanceId", InstanceId);
printf(CountFormat, ",", "RelayProtocolUDP", RelayProtocol["UDP"], "InstanceId", InstanceId);
printf(CountFormat, ",", "RelayProtocolTCP", RelayProtocol["TCP"], "InstanceId", InstanceId);
printf(CountFormat, ",", "ActiveSessions", ActiveSessions, "InstanceId", InstanceId);
printf(StatisticValuesFormat, ",", "TotalSessionAge", "Seconds", MAX(TotalSessionAgeMin, 0), TotalSessionAgeMax, TotalSessionAgeSum, MAX(TotalSessions, 1), "InstanceId", InstanceId);
printf(StatisticValuesFormat, ",", "ActiveSessionRate", "Bytes/Second", MAX(ActiveSessionRateMin, 0), ActiveSessionRateMax, ActiveSessionRateSum, MAX(ActiveSessions, 1), "InstanceId", InstanceId);
if (length(AutoScalingGroupName) > 0) {
printf(CountFormat, ",", "TotalSessions", TotalSessions, "AutoScalingGroupName", AutoScalingGroupName);
printf(CountFormat, ",", "ClientProtocolUDP", ClientProtocol["UDP,"], "AutoScalingGroupName", AutoScalingGroupName);
printf(CountFormat, ",", "ClientProtocolTCP", ClientProtocol["TCP,"], "AutoScalingGroupName", AutoScalingGroupName);
printf(CountFormat, ",", "ClientProtocolTLS", ClientProtocol["TLS,"], "AutoScalingGroupName", AutoScalingGroupName);
printf(CountFormat, ",", "RelayProtocolUDP", RelayProtocol["UDP"], "AutoScalingGroupName", AutoScalingGroupName);
printf(CountFormat, ",", "RelayProtocolTCP", RelayProtocol["TCP"], "AutoScalingGroupName", AutoScalingGroupName);
printf(CountFormat, ",", "ActiveSessions", ActiveSessions, "AutoScalingGroupName", AutoScalingGroupName);
printf(StatisticValuesFormat, ",", "TotalSessionAge", "Seconds", MAX(TotalSessionAgeMin, 0), TotalSessionAgeMax, TotalSessionAgeSum, MAX(TotalSessions, 1), "AutoScalingGroupName", AutoScalingGroupName);
printf(StatisticValuesFormat, ",", "ActiveSessionRate", "Bytes/Second", MAX(ActiveSessionRateMin, 0), ActiveSessionRateMax, ActiveSessionRateSum, MAX(ActiveSessions, 1), "AutoScalingGroupName", AutoScalingGroupName);
}
print "]"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment