In the documentation for agent-check
(https://cbonte.github.io/haproxy-dconv/1.9/configuration.html#agent-check)
it says that the string returned by the agent may be optionally
terminated by '\r' or '\n'. However, in my tests, it was mandatory to
end the response with this. Should the word "optionally" be removed from
the docs?
haproxy.cfg:
backend apiservers
balance roundrobin
server server1 192.168.50.3:80 check weight 100 agent-check
agent-inter 5s agent-addr 192.168.50.3 agent-port 9999
My agent code in Golang, which updates the weight of servers if CPU Idle
falls below a threshold:
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
}
Removing "\n" from the "c.Send()" lines causes the checks to hang.