Skip to content

Commit

Permalink
no more slave
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyclenerd committed Oct 8, 2022
1 parent ee4254e commit 74a6124
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 43 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ They were often quickly hacked and poorly tested.

🚨🚨🚨 No warranty or support! 🚨🚨🚨

## MySQL

* `check_replication_status.sh` - Check MySQL 8.0 replication status and send email on error
* `check_replication_status_hc.sh` - Check MySQL 8.0 replication status and ping [healthchecks.io](https://healthchecks.io/)

Used in Ansible Playbook:
```yml
# Check MySQL 8.0 replication and ping to healthchecks.io
- name: Script - Check status and ping healthchecks.io
ansible.builtin.get_url:
url: https://raw.githubusercontent.com/Cyclenerd/toolbox/master/check_replication_status_hc.sh
dest: /root/check_replication_status_hc.sh
mode: '0755'
owner: root
group: root
- name: Script - Change healthchecks.io UUID
ansible.builtin.lineinfile:
path: /root/check_replication_status_hc.sh
regexp: '^MY_HC_ID'
line: "MY_HC_ID='{{ healthchecks_uuid }}'"
```
## License
GNU Public License version 3.
Expand Down
49 changes: 19 additions & 30 deletions check_replication_status.sh
Original file line number Diff line number Diff line change
@@ -1,55 +1,44 @@
#!/bin/bash
#!/usr/bin/env bash

# check_replication_status.sh - Check MySQL Replication
# check_replication_status.sh
#
# Check MySQL 8.0 replication status and send email on error
#
# Author: Nils Knieling - https://github.com/Cyclenerd
# Source: https://handyman.dulare.com/mysql-replication-status-alerts-with-bash-script/

#
# Send alert to this email (root@localhost it always CC)
#
MY_MAIL_TO='nils@localhost'
# Maximum number of seconds behind master
MY_MAX_SEC_BEHIND=30

#
# Set the maximum number of seconds behind master that will be ignored.
# If the slave is be more than MY_MAX_SEC_BEHIND, an email will be sent.
#
MY_MAX_SEC_BEHIND=300

#
# Checking MySQL replication status
# Set username and password in .my.cnf
if mysql -e 'SHOW SLAVE STATUS \G' | grep 'Running:\|Master:\|Error:' > "/tmp/mysql_replication_status.txt"; then
echo "Login successful"
else
if ! mysql -e 'SHOW REPLICA STATUS \G' | grep 'Running:\|Source:\|Error:' > "/tmp/mysql_replication_status.txt"; then
echo "Login failed" > "/tmp/mysql_replication_status.txt"
fi

#
# displaying results, just in case you want to see them
#
echo "Results:"
cat "/tmp/mysql_replication_status.txt"

#
# checking parameters
#
slaveRunning=$(grep -c "Slave_IO_Running: Yes" "/tmp/mysql_replication_status.txt")
slaveSQLRunning=$(grep -c "Slave_SQL_Running: Yes" "/tmp/mysql_replication_status.txt")
secondsBehind="$(grep "Seconds_Behind_Master" "/tmp/mysql_replication_status.txt" | tr -dc '0-9')"
MY_REPLICA_IO_RUNNING=$(grep -c "Replica_IO_Running: Yes" "/tmp/mysql_replication_status.txt")
MY_REPLICA_SQL_RUNNING=$(grep -c "Replica_SQL_Running: Yes" "/tmp/mysql_replication_status.txt")
MY_SECONDS_BEHIND_SOURCE=$(grep "Seconds_Behind_Source" "/tmp/mysql_replication_status.txt" | tr -dc '0-9')

echo
echo "slaveRunning : $slaveRunning"
echo "slaveSQLRunning : $slaveSQLRunning"
echo "secondsBehind : $secondsBehind"
echo "Replica_IO_Running : $MY_REPLICA_IO_RUNNING"
echo "Replica_SQL_Running : $MY_REPLICA_SQL_RUNNING"
echo "Seconds_Behind_Source : $MY_SECONDS_BEHIND_SOURCE"
echo

#
# Sending email if needed
#
if [[ $slaveRunning != 1 || $slaveSQLRunning != 1 || $secondsBehind -gt $MY_MAX_SEC_BEHIND ]]; then
echo ""
echo "Sending email"
if [[ $MY_REPLICA_IO_RUNNING != 1 || $MY_REPLICA_SQL_RUNNING != 1 || $MY_SECONDS_BEHIND_SOURCE -gt $MY_MAX_SEC_BEHIND ]]; then
# Error
echo "Error"
mutt -s "MySQL: Replication Error - $(hostname)" -c "root@localhost" "$MY_MAIL_TO" < "/tmp/mysql_replication_status.txt"
else
echo ""
# OK
echo "Replication looks fine."
fi
28 changes: 15 additions & 13 deletions check_replication_status_hc.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#!/bin/bash
#!/usr/bin/env bash

# check_replication_status_hc.sh - Check MySQL Replication and send status to healthchecks.io
# check_replication_status_hc.sh
#
# Check MySQL 8.0 replication status and ping healthchecks.io
#
# Author: Nils Knieling - https://github.com/Cyclenerd
# Source: https://handyman.dulare.com/mysql-replication-status-alerts-with-bash-script/

# healthchecks.io UUID
MY_HC_ID=''

MY_HC_ID=""
# Maximum number of seconds behind master
MY_MAX_SEC_BEHIND=300
MY_MAX_SEC_BEHIND=30

# Checking MySQL replication status
# Set username and password in .my.cnf
if ! mysql -e 'SHOW SLAVE STATUS \G' | grep 'Running:\|Master:\|Error:' > "/tmp/mysql_replication_status_hc.txt"; then
if ! mysql -e 'SHOW REPLICA STATUS \G' | grep 'Running:\|Source:\|Error:' > "/tmp/mysql_replication_status_hc.txt"; then
echo "Login failed" > "/tmp/mysql_replication_status_hc.txt"
fi

Expand All @@ -21,18 +23,18 @@ echo "Results:"
cat "/tmp/mysql_replication_status_hc.txt"

# checking parameters
slaveRunning=$(grep -c "Slave_IO_Running: Yes" "/tmp/mysql_replication_status_hc.txt")
slaveSQLRunning=$(grep -c "Slave_SQL_Running: Yes" "/tmp/mysql_replication_status_hc.txt")
secondsBehind="$(grep "Seconds_Behind_Master" "/tmp/mysql_replication_status_hc.txt" | tr -dc '0-9')"
MY_REPLICA_IO_RUNNING=$(grep -c "Replica_IO_Running: Yes" "/tmp/mysql_replication_status_hc.txt")
MY_REPLICA_SQL_RUNNING=$(grep -c "Replica_SQL_Running: Yes" "/tmp/mysql_replication_status_hc.txt")
MY_SECONDS_BEHIND_SOURCE=$(grep "Seconds_Behind_Source" "/tmp/mysql_replication_status_hc.txt" | tr -dc '0-9')

echo
echo "slaveRunning : $slaveRunning"
echo "slaveSQLRunning : $slaveSQLRunning"
echo "secondsBehind : $secondsBehind"
echo "Replica_IO_Running : $MY_REPLICA_IO_RUNNING"
echo "Replica_SQL_Running : $MY_REPLICA_SQL_RUNNING"
echo "Seconds_Behind_Source : $MY_SECONDS_BEHIND_SOURCE"
echo

# Send status to healthchecks.io
if [[ $slaveRunning != 1 || $slaveSQLRunning != 1 || $secondsBehind -gt $MY_MAX_SEC_BEHIND ]]; then
if [[ $MY_REPLICA_IO_RUNNING != 1 || $MY_REPLICA_SQL_RUNNING != 1 || $MY_SECONDS_BEHIND_SOURCE -gt $MY_MAX_SEC_BEHIND ]]; then
# Error
echo "Error"
curl -fsS -m 10 --retry 5 -o /dev/null "https://hc-ping.com/$MY_HC_ID/fail"
Expand Down

0 comments on commit 74a6124

Please sign in to comment.