Skip to content

Instantly share code, notes, and snippets.

@liyaodong
Created December 8, 2021 08:07
Show Gist options
  • Save liyaodong/ebc69418f67b200d51651976e184dc31 to your computer and use it in GitHub Desktop.
Save liyaodong/ebc69418f67b200d51651976e184dc31 to your computer and use it in GitHub Desktop.
mysql-to-cloudwatch
#!/bin/sh
max_connections=$(sh /home/bitnami/monitor/mysqlmon.sh max_connections)
max_used_connections=$(sh /home/bitnami/monitor/mysqlmon.sh max_used_connections)
threads_connected=$(sh /home/bitnami/monitor/mysqlmon.sh threads_connected)
cdate=$(date -u +%Y-%m-%dT%H:%M:00.000Z)
instanceid="xxx"
echo 'threads_connected:'
echo $threads_connected
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 MySQLMaxUsedConnections --namespace "CustomMySQLMetrics" --dimensions="InstanceId=$instanceid" --value $max_used_connections --timestamp $cdate
aws cloudwatch put-metric-data --metric-name MySQLThreadsConnected --namespace "CustomMySQLMetrics" --dimensions="InstanceId=$instanceid" --value $threads_connected --timestamp $cdate
#!/bin/sh
MYSQL_USER=xxx
MYSQL_PASS=xxx
case $1 in
max_connections)
/opt/bitnami/mysql/bin/mysql --user=$MYSQL_USER --password=$MYSQL_PASS -e "show variables like 'max_connections'" 2>/dev/null | grep -i "max_connections" | awk '{print $2}'
;;
max_used_connections)
/opt/bitnami/mysql/bin/mysql --user=$MYSQL_USER --password=$MYSQL_PASS -e "show status like 'max_used_connections'" 2>/dev/null | grep -i "max_used_connections" | awk '{print $2}'
;;
threads_connected)
/opt/bitnami/mysql/bin/mysql --user=$MYSQL_USER --password=$MYSQL_PASS -e "show status like 'threads_connected'" 2>/dev/null | grep -i "threads_connected" | awk '{print $2}'
;;
*)
echo "Please provide one of max_connections, max_used_connections, threads_connected"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment