#!/bin/sh if [ -f /etc/default/haproxy ]; then . /etc/default/haproxy fi if [ -f /etc/sysconfig/haproxy ]; then . /etc/sysconfig/haproxy fi RETVAL=0 USER=${HAPROXY_USER:-haproxy} CFG_FILE=${HAPROXY_CONFIG:-/etc/kazoo/haproxy/haproxy.cfg} PID_FILE=${HAPROXY_PID:-/var/run/haproxy/kazoo-haproxy.pid} BIN_FILE=${HAPROXY_BIN:-/usr/sbin/haproxy} export HOME=${HAPROXY_HOME:-/var/lib/haproxy} check() { /usr/sbin/haproxy -c -V -f ${CFG_FILE} ${OPTIONS} } start() { /usr/sbin/haproxy -c -q -f ${CFG_FILE} ${OPTIONS} if [ $? -ne 0 ]; then echo "Errors in configuration file, check with haproxy check." RETVAL=1 return fi mkdir -p /var/run/haproxy chown -R ${USER} /var/run/haproxy mkdir -p /var/log/haproxy chown -R ${USER} /var/log/haproxy cd ${HOME} if [ "$(whoami)" == "${USER}" ]; then exec ${BIN_FILE} -f ${CFG_FILE} -p ${PID_FILE} ${OPTIONS} $@ else exec su -s /bin/bash -c "${BIN_FILE} -f ${CFG_FILE} -p ${PID_FILE} ${OPTIONS} $@" ${USER} fi RETVAL=$? } stop() { kill $(cat ${PID_FILE}) RETVAL=$? } restart() { /usr/sbin/haproxy -c -q -f ${CFG_FILE} ${OPTIONS} if [ $? -ne 0 ]; then echo "Errors in configuration file, check with haproxy check." RETVAL=1 return fi stop start } reload() { /usr/sbin/haproxy -c -q -f ${CFG_FILE} ${OPTIONS} if [ $? -ne 0 ]; then echo "Errors in configuration file, check with haproxy check." RETVAL=1 return fi ${BIN_FILE} -f ${CFG_FILE} -p ${PID_FILE} ${OPTIONS} -sf $(cat ${PID_FILE}) $@ retval=$? echo return $retval } status() { local STATS="pxname svname qcur qmax scur smax slim stot bin bout dreq dresp ereq econ eresp wretr wredis status weight act bck chkfail chdown lastchg downtime qlimit pid iid sid throttle lbtot tracked type rate rate_lim rate_max check_status check_code check_duration hrsp_1xx hrsp_2xx hrsp_3xx hrsp_4xx hrsp_5xx hrsp_other hanafail req_rate req_rate_max req_tot cli_abrt srv_abrt" local TABLE_HEADER="Host|25 Backend|15 Status Active Rate 1xx 2xx 3xx 4xx 5xx Ping" local TABLE_VARS="svname|25 pxname|15 status scur req_rate hrsp_1xx hrsp_2xx hrsp_3xx hrsp_4xx hrsp_5xx check_duration" if ! nc -U /tmp/haproxy.sock &> /dev/null; then echo "Unable to connect to haproxy, ensure it is running!" RETVAL=1 return 1 fi echo -n "|" for HEADER in $TABLE_HEADER; do if [[ "${HEADER}" =~ "|" ]]; then NAME="${HEADER%%|*}" SIZE=${HEADER#*|} else NAME="${HEADER%%|*}" SIZE=6 fi printf "%-${SIZE}s |" $NAME done echo echo "show stat" | nc -U /tmp/haproxy.sock \ | while IFS=',' read ${STATS}; do if [ -z "$svname" ]; then continue fi if [ "$svname" == 'svname' ]; then continue fi if [ "$svname" == 'BACKEND' ]; then continue fi if [ "$svname" == 'FRONTEND' ]; then continue fi echo -n "|" for VAR in $TABLE_VARS; do if [[ "${VAR}" =~ "|" ]]; then NAME="${VAR%%|*}" SIZE=${VAR#*|} else NAME="${VAR%%|*}" SIZE=6 fi eval VALUE=\$$NAME if [ "${VAR}" == 'check_duration' ]; then VALUE="${VALUE}ms" fi printf "%-${SIZE}s |" ${VALUE:-0} done echo done } case "$1" in start|stop|restart|reload) shift $1 "-D $@" ;; foreground) shift start "$@" ;; check) check ;; status) status ;; *) echo $"Usage: $0 {start|stop|restart|foreground|reload|check|status}" RETVAL=1 esac exit ${RETVAL}