此脚本是keepalive服务中用的监控脚本。
主要的高级部分体现在事先定义好的变量中。

[root@search63 ~]# cat /home/op/scripts/ha_keepalived_monitor.sh
#!/bin/sh

[global]

SCRIPT_PATH=$(cd "$(dirname "$0")"; pwd)
SCRIPT_NAME=$(basename "$0")
PARA_NUM="$#"
PROC_PID="$$"
LOG_DIR='/var/log/monitor'

vrrp_ip="172.16.136.117"
prog_flag="$MYSQL_HOME"
log_file="${LOG_DIR}/ha_keepalived.log"
record_time=$(date '+%Y-%m-%d %H:%M:%S')
chown_user='root'
chown_group='root'

Parse args use for

parse_arguments() {
for arg do
case "$arg" in
-H|--help)
usage
;;
-D|--debug)
set -x
;;
esac
shift
done
}

cd "${SCRIPT_PATH}"
parse_arguments $@
test ! -d "${LOG_DIR}" && mkdir -p "${LOG_DIR}" && chown "${chown_user}"."${chown_group}" "${LOG_DIR}"
prog_exists=$(ps -ef | grep "${prog_flag}" | grep -v grep | wc -l)
if [ "${prog_exists}" -eq 0 ];
then
vrrp_ip_exists=$(/sbin/ip a | grep "${vrrp_ip}" | wc -l)
if [ "${vrrp_ip_exists}" -ge 1 ];
then
/etc/init.d/keepalived stop
echo "[${record_time}] ERROR Detect mysql service does not exist, stop keepalived server!" >> "${log_file}"
chown "${chown_user}"."${chown_group}" "${log_file}"
fi
fi

exit 0