-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
43 deletions.
There are no files selected for viewing
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
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
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 |
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