Skip to content

Instantly share code, notes, and snippets.

@tangoslee
Created September 8, 2022 18:15
Show Gist options
  • Save tangoslee/7da9cbfdbf97e8a9dd77fe085849bdb5 to your computer and use it in GitHub Desktop.
Save tangoslee/7da9cbfdbf97e8a9dd77fe085849bdb5 to your computer and use it in GitHub Desktop.
[AWS] Simple MySQL connection monitoring for CloudWatch
#!/bin/sh
MYSQL="mysql"
max_connections=$(mysql -e "show variables like 'max_connections'" 2>/dev/null | grep -i "max_connections" | awk '{print $2}')
threads_connected=$(mysqladmin extended-status | grep -i 'threads_connected' | awk '{ print $4}')
cdate=$(date -u +%Y-%m-%dT%H:%M:00.000Z)
instanceid="${INSTANCE_ID}"
aws cloudwatch put-metric-data --metric-name MySQLMaxConnections --namespace "CustomMySQLMetrics" --dimensions="InstanceId=$instanceid" --value $max_connections --timestamp $cdate
aws cloudwatch put-metric-data --metric-name MySQLThreadsConnected --namespace "CustomMySQLMetrics" --dimensions="InstanceId=$instanceid" --value $threads_connected --timestamp $cdate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment