@@ -24,6 +24,8 @@ import (
2424 "net"
2525 "net/http"
2626 "os"
27+ "regexp"
28+ "strconv"
2729 "strings"
2830
2931 proxyproto "github.com/pires/go-proxyproto"
@@ -178,25 +180,52 @@ func ProxyProtoConn() (result []byte, err error) {
178180 return ioutil .ReadAll (conn )
179181}
180182
181- func GetGlobalHAProxyInfo ( ) (info GlobalHAProxyInfo , err error ) {
183+ func runtimeCommand ( command string ) (result [] byte , err error ) {
182184 kindURL := os .Getenv ("KIND_URL" )
183185 if kindURL == "" {
184186 kindURL = "127.0.0.1"
185187 }
186188 conn , err := net .Dial ("tcp" , fmt .Sprintf ("%s:%d" , kindURL , STATS_PORT ))
187189 if err != nil {
188- return info , err
190+ return
189191 }
190- _ , err = conn .Write ([]byte ("show info \n " ))
192+ _ , err = conn .Write ([]byte (command + " \n " ))
191193 if err != nil {
192- return info , err
194+ return
193195 }
194- reply := make ([]byte , 1024 )
195- _ , err = conn .Read (reply )
196+ result = make ([]byte , 1024 )
197+ _ , err = conn .Read (result )
198+ conn .Close ()
199+ return
200+ }
201+
202+ func GetHAProxyMapCount (mapName string ) (count int , err error ) {
203+ var result []byte
204+ result , err = runtimeCommand ("show map" )
196205 if err != nil {
197- return info , err
206+ return
198207 }
199- scanner := bufio .NewScanner (bytes .NewReader (reply ))
208+ scanner := bufio .NewScanner (bytes .NewReader (result ))
209+ for scanner .Scan () {
210+ line := scanner .Text ()
211+ if strings .Contains (line , mapName ) {
212+ r := regexp .MustCompile ("entry_cnt=[0-9]*" )
213+ match := r .FindString (line )
214+ nbr := strings .Split (match , "=" )[1 ]
215+ count , err = strconv .Atoi (nbr )
216+ break
217+ }
218+ }
219+ return
220+ }
221+
222+ func GetGlobalHAProxyInfo () (info GlobalHAProxyInfo , err error ) {
223+ var result []byte
224+ result , err = runtimeCommand ("show info" )
225+ if err != nil {
226+ return
227+ }
228+ scanner := bufio .NewScanner (bytes .NewReader (result ))
200229 for scanner .Scan () {
201230 line := scanner .Text ()
202231 switch {
@@ -208,6 +237,5 @@ func GetGlobalHAProxyInfo() (info GlobalHAProxyInfo, err error) {
208237 info .Pid = strings .Split (line , ": " )[1 ]
209238 }
210239 }
211- conn .Close ()
212- return info , nil
240+ return
213241}
0 commit comments