Skip to content

Instantly share code, notes, and snippets.

@fideloper
Created December 19, 2018 19:48
Show Gist options
  • Save fideloper/a58a6aecbd4f35caa6d5487bd1128f29 to your computer and use it in GitHub Desktop.
Save fideloper/a58a6aecbd4f35caa6d5487bd1128f29 to your computer and use it in GitHub Desktop.
Quick and dirty monitoring of mysql connections
#!/usr/bin/env bash
# 5 minutes between alerts
SECONDS_BETWEEN_ALERTS=300
# Track when we sent the last alert
LAST_ALERT=0
while true; do
NUMBER_CONNECTIONS=$(mysql --defaults-extra-file=/data/.prod.cnf -sNe "select count(*) as connection_count from INFORMATION_SCHEMA.PROCESSLIST;")
RIGHTNOW=$(date +"%s")
if [ "$NUMBER_CONNECTIONS" -ge "750" ] && [ "`expr $RIGHTNOW - $LAST_ALERT`" -ge "$SECONDS_BETWEEN_ALERTS" ]; then
CONNECTIONS_PER_USER=$(mysql --defaults-extra-file=/data/.prod.cnf -e "select USER, HOST, count(*) as connection_count from INFORMATION_SCHEMA.PROCESSLIST group by USER order by connection_count desc;")
curl -X POST https://hooks.slack.com/services/xxx/xxx/xxx --data-urlencode "payload={\"username\": \"RDS Police\", \"text\": \"\nTOTAL CONNECTIONS: $NUMBER_CONNECTIONS\n$CONNECTIONS_PER_USER\n\", \"icon_emoji\": \":alert:\"}"
LAST_ALERT="$RIGHTNOW"
fi
sleep 5
done
description "Monitoring Database Connections"
start on filesystem or runlevel [2345]
stop on runevel [!2345]
respawn
respawn limit 5 2
script
/opt/dbmon.sh
end script
[Unit]
Description=Monitoring Database Connections
[Service]
Restart=on-failure
ExecStart=/opt/dbmon.sh
[Install]
WantedBy=multi-user.target
@matthewsuan
Copy link

this checks db every 5 secs, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment