HAProxy Enterprise Documentation 1.7r2

Agent Health Checks

An agent health check is one where HAProxy Enterprise connects to an agent program running on a backend server. In response to the health check probe, the agent program sends back a string of ASCII text that triggers a change in the load balancer. The change could be:

  • put the server into the down state

  • put the server into the up state

  • put the server into maintenance mode

  • take the server out of maintenance mode

  • change the server's weight

  • change the server's maxconn setting

Create an agent program

An agent program can be written in any programming language, as long as it allows you to listen on a TCP port. When the program detects that HAProxy Enterprise has connected, it should return a string of ASCII text that makes a change to the load balancer or keeps it at its current state.

In the following example, the program is written in the Go programming language. It returns the string 50%\n if the server's CPU idle time is less than 10, which would indicate it is near to maxing out its CPU. Otherwise, it returns the string 100%\n. Notice that the string should end with a line feed character (\n):

package main

import (
    "fmt"
    "time"
    "github.com/firstrow/tcp_server"
    "github.com/mackerelio/go-osstat/cpu"
)

func main() {
    server := tcp_server.New(":9999")

    server.OnNewClient(func(c *tcp_server.Client) {
        fmt.Println("Client connected")
        cpuIdle, err := getIdleTime()

        if err != nil {
            fmt.Println(err)
            c.Close()
            return
        }

        if cpuIdle < 10 {
            // Set server weight to half
            c.Send("50%\n")
        } else {
            c.Send("100%\n")
        }

        c.Close()
    })

    server.Listen()
}

func getIdleTime() (float64, error) {
    before, err := cpu.Get()
    if err != nil {
        return 0, err
    }
    time.Sleep(time.Duration(1) * time.Second)
    after, err := cpu.Get()
    if err != nil {
        return 0, err
    }
    total := float64(after.Total - before.Total)
    cpuIdle := float64(after.Idle-before.Idle) / total * 100
    return cpuIdle, nil
}

Here are more examples of text the program could send back and its effect:

Example text

Effect on the load balancer

down\n

Marks the server as down.

up\n

Marks the server as up.

maint\n

Puts the server into maintenance mode.

ready\n

Takes the server out of maintenance mode.

50%\n

Changes the server's weight to half its current value.

maxconn:30\n

Changes the server's maxconn value to 30.

Configure the servers

In the following example, HAProxy Enterprise sends a health check probe every five seconds to the agent program listening at 192.168.0.10 at port 8080:

backend servers
   server server1 192.168.0.10:80 check  weight 100  agent-check  agent-addr 192.168.0.10  agent-port 8080  agent-inter 5s  agent-send ping\n

It uses these parameters on the server line:

Parameter

Description

check

Enables health checking.

agent-check

Enables agent health checks for the server.

agent-addr

Identifies the IP address where the agent is listening.

agent-port

Identifies the port where the agent is listening.

agent-inter

Defines the interval between health checks.

agent-send

A string that HAProxy Enterprise sends to the agent upon connection. Be sure to end it with a newline character.


Next up

LDAP Health Checks