Last active
January 12, 2025 02:25
-
-
Save tlwr/bc8a078768e45598727a8b258e0440d4 to your computer and use it in GitHub Desktop.
Strongswan VPN metrics to Cloudwatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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