From e10958727924d7605432d7d855c93c47ad9a273c Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 8 Mar 2024 11:15:58 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20fix:=20get=20ipLocal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vngcloud_vmonitor/vngcloud_vmonitor.go | 49 ++++++------------- 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/plugins/outputs/vngcloud_vmonitor/vngcloud_vmonitor.go b/plugins/outputs/vngcloud_vmonitor/vngcloud_vmonitor.go index 6b318e626959f..12518f701e6c3 100644 --- a/plugins/outputs/vngcloud_vmonitor/vngcloud_vmonitor.go +++ b/plugins/outputs/vngcloud_vmonitor/vngcloud_vmonitor.go @@ -13,7 +13,6 @@ import ( "net/http" "net/url" "os" - "strings" "time" "github.com/influxdata/telegraf" @@ -143,14 +142,21 @@ func (h *VNGCloudvMonitor) initHTTPClient() error { return nil } -func (h *VNGCloudvMonitor) getIp(address, port string) (string, error) { - h.Log.Infof("Dial %s %s", address, port) - conn, err := net.DialTimeout("tcp", net.JoinHostPort(address, port), 5*time.Second) +// GetLocalIP returns the non loopback local IP of the host +func GetLocalIP() string { + addrs, err := net.InterfaceAddrs() if err != nil { - return "", err + return "" } - defer conn.Close() - return strings.Split(conn.LocalAddr().String(), ":")[0], nil + for _, address := range addrs { + // check the address type and if it is not a loopback the display it + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + return ipnet.IP.String() + } + } + } + return "" } func getModelNameCPU() (string, error) { @@ -162,35 +168,8 @@ func getModelNameCPU() (string, error) { } func (h *VNGCloudvMonitor) getHostInfo() (*infoHost, error) { - getHostPort := func(urlStr string) (string, error) { - u, err := url.Parse(urlStr) - if err != nil { - return "", fmt.Errorf("url invalid %s", urlStr) - } - - host, port, err := net.SplitHostPort(u.Host) - - if err != nil { - return "", err - } - - ipLocal, err := h.getIp(host, port) - if err != nil { - return "", err - } - return ipLocal, nil - } - - var ipLocal string + ipLocal := GetLocalIP() var err error - // get ip local - - ipLocal, err = getHostPort(h.URL) - - if err != nil { - return nil, fmt.Errorf("err getting ip address %s", err.Error()) - } - // get ip local gi, err := goInfo.GetInfo() if err != nil {