Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kujiy/06c378e037f14ec83fbc213e9812d0dd to your computer and use it in GitHub Desktop.
Save kujiy/06c378e037f14ec83fbc213e9812d0dd to your computer and use it in GitHub Desktop.
zabbix mysql replication check bash script for both multiple and single replication systems
# How to Use
#
## 1. Set this file on the servers which runs zabbix-agent
### $ cp this.sh /etc/zabbix/zabbix_agentd.conf.d/
#
## 2. Set .my.cnf on the same server to login to mysql
### $ vi /var/lib/zabbix/.my.cnf
#
# [mysql]
# host = localhost
# user=YOURMYSQLUSER
# password=YOURMYSQLPASSWORD
# socket = /var/lib/mysql/mysql.sock
#
## 3. Set zabbix item
#
## For Multiple replication:
### > show slave 'REPLICATION_NAME' status;
### mysql.slave[master-db.com] # Add arg1 to replication name (CHANGE MASTER 'HERE' TO )
#### Check command(Execute on zabbix-server)
#### $ zabbix_get -s 192.168.1.2 -k "mysql.slave[master-db.com]"
#### 0 means success. Returns slave status result if detects an error.
#
## For Single(legacy) replication:
### > show slave status;
### mysql.slave[] # no args
#### Check command(Execute on zabbix-server)
#### $ zabbix_get -s 192.168.1.3 -k "mysql.slave[]"
#### 0 means success. Returns slave status result if detects an error.
UserParameter=mysql.slave[*],[[ -z "$1" ]] && TARGET= || TARGET="'$1'" && CMDRES=$(mysql --defaults-extra-file=/var/lib/zabbix/.my.cnf -e "SHOW SLAVE $TARGET STATUS\G" | sed -e 's/^\s*//g' | sed 's/: /=/g' | tail -n +2 | sort ) && for j in $CMDRES; do eval export $j; done; RES=$Slave_IO_Running$Slave_SQL_Running; [[ $RES = "YesYes" ]] && echo 0 || echo "Replication check failed. 'SHOW SLAVE $TARGET STATUS;' Result => $CMDRES"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment