From 5431b8b4cf41e5785772799b295337e0e2927161 Mon Sep 17 00:00:00 2001 From: caffeinated92 <155546371+caffeinated92@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:37:20 +0700 Subject: [PATCH] Get version for HaProxy (#957) --- cluster/prx_haproxy.go | 10 +++++++++- router/haproxy/runtime_api.go | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/cluster/prx_haproxy.go b/cluster/prx_haproxy.go index c9c1e2b0d..b7ef87e8a 100644 --- a/cluster/prx_haproxy.go +++ b/cluster/prx_haproxy.go @@ -274,8 +274,16 @@ func (proxy *HaproxyProxy) Refresh() error { } - result, err := haRuntime.ApiCmd("show stat") + if proxy.Version == "" { + vstring, err := haRuntime.GetVersion() + if err == nil { + if vstring != "" { + proxy.Version = vstring + } + } + } + result, err := haRuntime.ApiCmd("show stat") if err != nil { cluster.SetState("ERR00052", state.State{ErrType: "WARNING", ErrDesc: fmt.Sprintf(clusterError["ERR00052"], err), ErrFrom: "MON"}) return err diff --git a/router/haproxy/runtime_api.go b/router/haproxy/runtime_api.go index 28549a620..b7c041644 100644 --- a/router/haproxy/runtime_api.go +++ b/router/haproxy/runtime_api.go @@ -27,6 +27,25 @@ func (r *Runtime) ApiCmd(cmd string) (string, error) { return string(result), nil } +func (r *Runtime) GetVersion() (string, error) { + conn, err := net.DialTimeout("tcp", r.Host+":"+r.Port, DefaultTimeout) + if err != nil { + return "", err + } + defer conn.Close() + _, err = conn.Write([]byte("show version \n")) + if err != nil { + + return "", err + } + // cluster.LogModulePrintf(cluster.Conf.Verbose, config.ConstLogModGeneral,config.LvlErr, "haproxy entering readall stats: ") + result, err := io.ReadAll(conn) + if err != nil { + return "", err + } + return string(result), nil +} + func (r *Runtime) SetMaster(host string, port string) (string, error) { if net.ParseIP(host) == nil {