Created
December 19, 2018 19:48
-
-
Save fideloper/a58a6aecbd4f35caa6d5487bd1128f29 to your computer and use it in GitHub Desktop.
Quick and dirty monitoring of mysql connections
This file contains 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 | |
# 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 |
This file contains 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
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 |
This file contains 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
[Unit] | |
Description=Monitoring Database Connections | |
[Service] | |
Restart=on-failure | |
ExecStart=/opt/dbmon.sh | |
[Install] | |
WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this checks db every 5 secs, right?