Skip to content
Snippets Groups Projects
check_haproxy_health 2.14 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
# 2021-12-14  v1.3  <axel.hahn@iml.unibe.ch>  use updated haproxy paser in sourced file
# 2022-04-01  v1.4  <axel.hahn@iml.unibe.ch>  use wget default params; shell fixes
# ======================================================================

. $(dirname $0)/inc_pluginfunctions
. $(dirname $0)/inc_haproxy_cfg.sh

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

# ----------------------------------------------------------------------
# pre checks
# ----------------------------------------------------------------------
ph.require 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


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

url=$( getHealthUri )
if [ -z "$url" ]; then
  ph.abort "UNKNOWN: Unable to detect url for health status."
fi

# remove password for showing url in output
safeurl=$( echo $url | sed "s#\(://\)\(.*@\)#\1#g" )

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

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

rm -f $tmpfile

ph.exit

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