Script

File: http://svn.grimore.org/configuration-templates/monit/debian/templates/haproxy.sh -

#!/bin/bash
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2023 - License: GNU GPLv3      ##
###########################################################################
# This script matches the PID reported by HAproxy via its internal stats  #
# interface to the PID of the process from the process table and iff. the #
# two match then the script returns 0.                                    #
#                                                                         #
# Requirements:                                                           #
#   * socket                                                              #
#                                                                         #
###########################################################################
 
###########################################################################
#                           CONFIGURATION                                 #
###########################################################################
 
# HAProxy needs the socket for stats to be exposed.
# These settings added to the global section should enable the socket:
#   stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
#   stats timeout 30s
SOCKET_PATH="/run/haproxy/admin.sock"
PID_FILE="/run/haproxy.pid"
 
###########################################################################
#                             INTERNALS                                   #
###########################################################################
 
PID=$(cat <<EOF | \
    socat "$SOCKET_PATH" stdio | \
    grep ^Pid | \
    awk -F': ' '{ print $2 }'
show info
EOF
)
 
[ "$PID" != `cat "$PID_FILE"` ] || exit 1
 
exit 0 

Template

File: http://svn.grimore.org/configuration-templates/monit/debian/templates/haproxy -

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2023 - License: GNU GPLv3      ##
###########################################################################
 
check program haproxy with path "/etc/monit/templates/haproxy.sh"
   start program  "/bin/systemctl start haproxy"
   stop program  "/bin/systemctl stop haproxy"
   if status != 0 then restart