Monitorizar la replicación de Mysql
Imaginemos que tenemos una infraestructura Maestro/Esclavo y el esclavo lo usamos para lecturas, backup o simplemente para dar un servicio mínimo si el Maestro se cae. Imaginemos que dicha replicación lleva caída un mes y el Maestro se rompe. Cuando te das cuenta es demasiado tarde y entonces toca imaginarse como huir sin que te pillen :)
Monitorizar si la replicación es correcta es sencillo, ya que MySQL nos puede dar en segundos el desfase que existe entre un host y otro a la hora de replicar los cambios. Con "SHOW SLAVE STATUS\G" tendremos la información que necesitamos:
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.60.1.3
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000021
Read_Master_Log_Pos: 327326
Relay_Log_File: mysqld-relay-bin.000061
Relay_Log_Pos: 327463
Relay_Master_Log_File: mysql-bin.000021
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 327326
Relay_Log_Space: 327463
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Cada entrada en el log lleva un timestap indicando el momento exacto en el que es escrito en el Master. Por lo tanto, el "Seconds_behind_master" hace una simple resta, entre nuestro timestamp y el del master para saber la diferencia.
Dicho esto, la solución es tan sencilla como tirar de Bash Scripting:
#!/bin/sh
treshold=1200
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
if [ -z $1 ] ; then
echo "Usage: $0 /path/to/mysql.sock"
exit 255
fi
seconds=mysql -S $1 -N -e "show slave status\G" | grep Seconds_Behind_Master | cut -d: -f 2
if [ -z $seconds ] ; then
exit 255
fi
if [ $seconds = "NULL" ]; then
echo Replication kaput!!!
exit 255
fi
if [ $seconds -gt $treshold ]; then
echo Slave behind the master more than $treshold seconds!
fi
Pero claro, antes de realizar estas comprobaciones se deben tener en cuenta una serie de requisitos previos:
- Los equipos deben tener la hora sincronizada :)
- A poder ser, deben estar LAN. Si tenemos una WAN con mucha latencia y encima microcortes, olvídate de esto.
Si se busca en internet hay múltiples formas de hacerlo, pero esta es la más sencilla. Y si le añadimos algun "exit 0" y "exit 1" tenemos un checker para Nagios :P Y también es recomendable añadir otro grep más para comprobar si los threads SQL e IO están en funcionamiento (Slave_IO_Running, Slave_SQL_Running)...
Si Mikel, esta entrada va por ti xD
Comments
- Matias on May 18, 2012, at 10:05 PM







