Skip to content
Snippets Groups Projects
check_haproxy_health 2.53 KiB
#!/bin/bash
# ======================================================================
#
# NAGIOS CLIENT CHECK :: haproxy healthcheck
#
# ----------------------------------------------------------------------
#
# ----------------------------------------------------------------------
# 2020-04-27  v1.0  <axel.hahn@iml.unibe.ch>
# 2020-05-04  v1.1  <axel.hahn@iml.unibe.ch>  show message if monitor-uri was not set
# 2020-12-03  v1.2  <axel.hahn@iml.unibe.ch>  loop over multiple frontend status urls
# ======================================================================

. `dirname $0`/inc_pluginfunctions

cfgfile=/etc/haproxy/haproxy.cfg
tmpfile=/tmp/check_haproxy_healthcheck_$$



# ----------------------------------------------------------------------
# pre checks
# ----------------------------------------------------------------------
ph.reqire wget

cat $cfgfile >/dev/null
if [ $? -ne 0 ]; then
  ph.abort "UNKNOWN: unable to read ha proxy config $cfgfile ... `ls -l $cfgfile`"
fi

cat $cfgfile | grep "\ mode\ .*http" >/dev/null
if [ $? -ne 0 ]; then
  ph.abort "UNKNOWN: haproxy is not in http mode"
fi


# ----------------------------------------------------------------------
# build url
# ----------------------------------------------------------------------

proto=http
auth=
host=localhost
port=80
uri=

uri=` cat $cfgfile | grep "\ monitor-uri\ " | awk '{ print $2 }'`

if [ -z "$uri" ]; then
  ph.abort "UNKNOWN: no monitor-uri setting was found in the config $cfgfile."
fi

cat $cfgfile | grep "bind\ .*\ ssl\ " >/dev/null && proto=https

# fix #4176 - take first found frontend ip only
# port=`cat $cfgfile | grep "\ bind\ "        | awk '{ print $2 }' | head -1 | cut -f 2 -d ':'`


# ----------------------------------------------------------------------
# loop over found bind ports
# ----------------------------------------------------------------------

for myport in `cat $cfgfile | grep "\ bind\ "        | awk '{ print $2 }' | cut -f 2 -d ':'`
do
  url="$proto://${host}:${myport}${uri}"

  # --- get status page
  wget --no-check-certificate -O $tmpfile $url 2>/dev/null
  if [ $? -ne 0 ]; then
    rm -f $tmpfile
    ph.abort "UNKNOWN: url $url did not respond. `wget --no-check-certificate -O - -S $url`"
  fi

  grep "200 OK" $tmpfile >/dev/null
  if [ $? -ne 0 ]; then
    ph.setStatus "error"
    ph.status "url $url did not contain 200 OK. `wget --no-check-certificate -O - -S $url`"
  else
    ph.status "HA Proxy $url is up and running."
  fi

  rm -f $tmpfile

done

ph.exit

# ----------------------------------------------------------------------