Skip to content
Snippets Groups Projects
check_haproxy_health 3.34 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
# 2022-10-21  v1.5  <axel.hahn@unibe.ch>      remove grep: warning: stray \ before white space
# 2023-07-28  v1.6  <axel.hahn@unibe.ch>      add help page
# 2023-08-23  v1.7  <axel.hahn@unibe.ch>      fix wrong exitcode to "critical"
# ======================================================================

. $(dirname $0)/inc_pluginfunctions

export self_APPVERSION=1.7

. $(dirname $0)/inc_haproxy_cfg.sh


tmpfile=/tmp/check_haproxy_healthcheck_$$

# ----------------------------------------------------------------------
# functions
# ----------------------------------------------------------------------

function showHelp(){
    local _self; _self=$(basename $0)
cat <<EOF
$( ph.showImlHelpHeader )

Check HA Proxy health.
The plugin reads $HAPROXYcfgfile to detect required status url. 
It requires wget to handle the http request.

Non OK values occur:
  UNKNOWN - if $HAPROXYcfgfile cannot be read
          - haproxy is not in http mode
          - the url wasn't detected
          - the detected status url doesn't send a response
  ERROR - the detected status url did not respond with Http status 
          code 200

SYNTAX:
$_self [-h]

OPTIONS:

    -h or --help   show this help.

EXAMPLE:

    $_self

EOF
}

# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

# --- check param -h
case "$1" in
    "--help"|"-h")
        showHelp
        exit 0
        ;;
    *)
esac

ph.require wget

if [ ! -f $HAPROXYcfgfile ]; then
  ph.abort "UNKNOWN: config file does not exist: $HAPROXYcfgfile"
fi

if ! cat $HAPROXYcfgfile >/dev/null 2>&1; then
  ph.abort "UNKNOWN: unable to read ha proxy config $HAPROXYcfgfile ... $(ls -l $HAPROXYcfgfile)"
fi

if ! cat $HAPROXYcfgfile | grep " mode .*http" >/dev/null; 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 "critical"
  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

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