Skip to content

Instantly share code, notes, and snippets.

@tlwr
Last active October 30, 2020 12:36
Show Gist options
  • Save tlwr/bc8a078768e45598727a8b258e0440d4 to your computer and use it in GitHub Desktop.
Save tlwr/bc8a078768e45598727a8b258e0440d4 to your computer and use it in GitHub Desktop.
Strongswan VPN metrics to Cloudwatch
#!/usr/bin/env bash
set -ueo pipefail
export PATH="$PATH:/usr/sbin/"
export AWS_DEFAULT_REGION=eu-west-2
export AWS_REGION=eu-west-2
up_connections="$(ipsec statusall \
| grep ESTABLISHED | awk '{print $1}' | grep -o '.*\[' | tr -d '[' | sort -u
)"
az="$(curl 169.254.169.254/latest/meta-data/placement/availability-zone)"
for connection in $up_connections; do
stats="$(ipsec statusall | grep bytes | grep $connection)"
input_bytes="$(echo "$stats" | grep -o '[^ ]* bytes_i' | awk '{print $1}')"
output_bytes="$(echo "$stats" | grep -o '[^ ]* bytes_o' | awk '{print $1}')"
echo "$connection"
echo "$input_bytes input bytes"
echo "$output_bytes output bytes"
aws cloudwatch put-metric-data \
--metric-name TunnelUp \
--namespace Strongswan \
--unit None \
--value 1.0 \
--dimensions "Connection=$connection,AZ=$az"
aws cloudwatch put-metric-data \
--metric-name InputBytes \
--namespace Strongswan \
--unit Bytes \
--value $input_bytes \
--dimensions "Connection=$connection,AZ=$az"
aws cloudwatch put-metric-data \
--metric-name OutputBytes \
--namespace Strongswan \
--unit Bytes \
--value $output_bytes \
--dimensions "Connection=$connection,AZ=$az"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment