#!/bin/sh
#
# chkconfig: - 85 15
#
# description: HA-Proxy is a TCP/HTTP reverse proxy which is particularly suited \
# for high availability environments.
# processname: haproxy
# config: /usr/local/haproxy/conf/haproxy.cfg
# pidfile: /var/run/haproxy.pid
#
# Script Author: liujiuling <[email protected]>
# Version: 20150901
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
else
exit 0
fi
conf=/usr/local/haproxy/conf/haproxy.cfg
if [ ! -f $conf ];then
echo -e "\033[31m config file not exist! \033[0m"
exit 1
fi
error=$(grep bind $conf | sort | uniq -c | awk '$1!=1{print $3}')
if [ ! -z "$error" ];then
echo -e "\033[31m vip is duplicate, please check: \033[0m"
for vip in $error
do
echo -e "\033[31m \t$vip \033[0m"
done
else
echo -e "\033[32m \t vip is not duplicate \033[0m"
fi
#BASENAME='haproxy'
BASENAME=`basename $0`
HAproxy_path_conf="/usr/local/haproxy/conf"
HAproxy_path_sbin="/usr/local/haproxy/sbin"
if [ -L $0 ]; then
BASENAME=`find $0 -name $BASENAME -printf %1`
BASENAME=`basename $BASENAME`
fi
[ -f $HAproxy_path_conf/$BASENAME.cfg ] || exit 1
RETVAL=0
start() {
$HAproxy_path_sbin/$BASENAME -c -q -f $HAproxy_path_conf/$BASENAME.cfg
if [ $? -ne 0 ]; then
echo "Errors found in configuration file, check it with $BASENAME check"
return 1
fi
echo -n "Starting $BASENAME:"
daemon $HAproxy_path_sbin/$BASENAME -D -f $HAproxy_path_conf/$BASENAME.cfg -p /var/run/$BASENAME.pid
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$BASENAME
return $RETVAL
}
stop() {
echo -n "Shutting down $BASENAME: "
killproc $BASENAME -USR1
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$BASENAME
[ $RETVAL -eq 0 ] && rm -f /var/run/$BASENAME.pid
return $RETVAL
}
restart() {
$HAproxy_path_sbin/$BASENAME -c -q -f $HAproxy_path_conf/$BASENAME.cfg
if [ $? -ne 0 ]; then
echo "Errors found in configuration file, check it with $BASENAME check"
return 1
fi
stop
sleep 1
start
}
reload() {
$HAproxy_path_sbin/$BASENAME -c -q -f $HAproxy_path_conf/$BASENAME.cfg
if [ $? -ne 0 ]; then
echo "Errors found in configuration file, check it with '$BASENAME check'."
return 1
fi
$HAproxy_path_sbin/$BASENAME -D -f $HAproxy_path_conf/$BASENAME.cfg -p /var/run/$BASENAME.pid -sf $(cat /var/run/$BASENAME.pid)
echo 'HAproxy is reloaded...'
}
status() {
p_count=$(ps -ef | grep haproxy | grep -v grep | wc -l)
if [ $p_count -ge 1 ]; then
if [ -f "/var/run/$BASENAME.pid" ]; then
echo 'haproxy is running...'
else
echo 'haproxy is stopping...'
fi
else
echo 'haproxy is stopping...'
fi
}
check() {
$HAproxy_path_sbin/$BASENAME -c -q -V -f $HAproxy_path_conf/$BASENAME.cfg
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
reload)
reload
;;
check)
check
;;
*)
echo $"Usage: $BASENAME {start|stop|restart|status|reload|check}"
exit 1
esac
exit $?