MySQL keepalived high-availability monitoring script for MySQL (or other services) keepalived high-availability monitoring scripts
Development script Requirements: We know that KeepAlive is based on the survival of virtual IP to determine whether to preempt the master mechanism, but if we do the MySQL keepalived high availability, we should consider a situation, that is, if the machine card is not broken, 20 due to the instability of the MySQL service, or human error operation, resulting in service shutdown, the result is that KeepAlive does not switch, because the virtual IP of the master master does not exist, but the service has stopped, This situation if not timely switch to backup will cause problems, you can imagine, so here and everyone to share a monitoring KeepAlive Master Master Service script
Function: When the primary master machine (the machine where the VIP resides) is down, it shuts down its own keepalive service, allowing the VIP to switch to the standby so that the service can remain available. As you can see, no matter what the high availability of service with KeepAlive, as long as you listen to the port number as the object of monitoring, in fact, this is a very wide range of scripts, I hope you can write general script, so that can enhance the ability of thinking! If there is a bad place to write, please correct me.
1#!/bin/sh2#author Feifei3#date 201612124#email [email protected]5#version 1.06#function guard MySQL service7. /etc/init.d/functions8#define VAR9#usage (method of Use)10if[$# -ne1]; Then11Echo "Usage:sh $ {tcp_port}"12Exit113fi14#define VAR (using Netstat is an important basis for us to determine whether a service is viable, based on service name, or port number, preferably port number, because the port number is unique)Check= ' Netstat-lnutp|grep $|wc- L' 16 17#check If there is already has a same monitoring exit (daemon script, it must be determined if there is already an identical monitoring, or it will result in a waste of resources)Count= ' Ps-ef|grep" $ $"|grep-v"grep"|wc- L' 19 20if[$count -GT2]; Then21stEcho - e "\nerror:there is already has a same monitoring!"22Exit1 23fi24 25function DMail() {26Echo "$-$ (hostname):d own">/var/Log/ $. LOG27 Mail- S "$-$ (hostname):d own"[Email protected] </var/Log/ $. log28}29 30function Umail() {31Echo "$-switch-success">/var/Log/ $. LOG32 Mail- S "$-switch-success"[Email protected] </var/Log/ $. log33}34 35#check = ' namp 192.168.1.21 $1|grep open|wc-l ' (You can also use Nmap command to monitor a host a port number is turned on, functions like Nestat and SS)36if[$check -eq0]; Then37Echo " is not listening!pls input again!"38Exit139Else40 while true41 Do42if[' Netstat-lnutp|grep $|wc- L`-eq0]; Then43Echo044 DMail $45/etc/init.d/keepalived stop46 Sleep 547 ping-c 2-w 2 192.168.1.22 &>/dev/null48if[ $?-eq0]; Then49EchoUmail $51 Break52Else53Echo "$-swith-failed">/var/Log/ $. log54 Mail- S "$-switch-failed"[Email protected] </var/Log/ $. log55 Break56fi57fi58 Done59fi
Script Simple Description:
1. Operating conditions: The current monitoring port number must be the listening state, if not enabled will prompt to start the service
2. Principle of realization: the principle of realizing monitoring is to judge with while Loop +if
3. Failure handling mechanism: If the mind of the classmate can try, after the service down, plus some processing measures, for example, let the service attempt to restart, if the restart successfully continued monitoring, unsuccessful, then close keepalive
4. Record of the event: the success or failure of each process should be recorded to the corresponding documents, if necessary, to notify the mail (a good operation, to understand the necessary fault records, easy to summarize later)
PS. is not particularly difficult to script, heavy in the carding process, if there is a wrong place to hope that everyone correct.
MySQL keepalived high-availability monitoring script