From 9a9f1b973e7bc45f9ea1f2c6fe8b38c62c41a50a Mon Sep 17 00:00:00 2001 From: "Matthew R. Kasun" Date: Thu, 19 May 2022 09:11:56 -0400 Subject: [PATCH] add checks/logs on broker connect timeout --- go.mod | 4 ++++ go.sum | 5 +++++ netclient/functions/daemon.go | 8 ++++++++ netclient/functions/mqpublish.go | 35 +++++++++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 82ee07eb..c0cb4562 100644 --- a/go.mod +++ b/go.mod @@ -41,6 +41,7 @@ require ( require ( cloud.google.com/go v0.34.0 // indirect github.com/Microsoft/go-winio v0.4.14 // indirect + github.com/cloverstd/tcping v0.1.1 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/docker/distribution v2.7.1+incompatible // indirect @@ -59,6 +60,7 @@ require ( github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff // indirect github.com/google/go-cmp v0.5.7 // indirect github.com/gorilla/websocket v1.4.2 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/josharian/native v1.0.0 // indirect github.com/kr/text v0.2.0 // indirect github.com/leodido/go-urn v1.2.1 // indirect @@ -73,6 +75,8 @@ require ( github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/seancfoley/bintree v1.0.1 // indirect github.com/spf13/afero v1.3.2 // indirect + github.com/spf13/cobra v1.0.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 // indirect github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 // indirect github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect diff --git a/go.sum b/go.sum index b5ce31d9..06791027 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cloverstd/tcping v0.1.1 h1:3Yp9nvSDI7Z63zoVQDJzVk1PUczrF9tJoOrKGV30iOk= +github.com/cloverstd/tcping v0.1.1/go.mod h1:NYXTrTDwlwuOKQ0vwksUVUbIr0sxDDsf1J6aFpScCBo= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= @@ -114,6 +116,7 @@ github.com/guumaster/logsymbols v0.3.1/go.mod h1:1M5/1js2Z7Yo8DRB3QrPURwqsXeOfgs github.com/guumaster/tablewriter v0.0.9 h1:qyswXhSCI1SWYH78MLApi8AfL8JsWZWAUkZLONNMiYI= github.com/guumaster/tablewriter v0.0.9/go.mod h1:9B1xy1BLPtcVAeYjC1EXPxcklqnzk7dU2c3ywGbUnKY= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jackmordaunt/icns v0.0.0-20181231085925-4f16af745526/go.mod h1:UQkeMHVoNcyXYq9otUupF7/h/2tmHlhrS2zw7ZVvUqc= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= @@ -212,9 +215,11 @@ github.com/spf13/afero v1.3.2 h1:GDarE4TJQI52kYSbSAmLiId1Elfj+xgSDqrUZxFhxlU= github.com/spf13/afero v1.3.2/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU= +github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= diff --git a/netclient/functions/daemon.go b/netclient/functions/daemon.go index 02558fab..aaba87ff 100644 --- a/netclient/functions/daemon.go +++ b/netclient/functions/daemon.go @@ -242,6 +242,14 @@ func setupMQTT(cfg *config.ClientConfig, publish bool) mqtt.Client { } else { err = token.Error() } + brokerErr := checkBroker(cfg.Server.Server) + if brokerErr.Error() == "dns" { + logger.FatalLog("dns lookup failed for broker ... update dns records") + } else if brokerErr.Error() == "ping" { + logger.FatalLog("unable to connect to broker ... check firewalls") + } else if brokerErr != nil { + logger.Log(0, "unable to conncet to broker") + } logger.Log(0, "could not connect to broker", cfg.Server.Server, err.Error()) if strings.Contains(err.Error(), "connectex") || strings.Contains(err.Error(), "connect timeout") { logger.Log(0, "connection issue detected.. attempt connection with new certs") diff --git a/netclient/functions/mqpublish.go b/netclient/functions/mqpublish.go index d17b5188..37c99959 100644 --- a/netclient/functions/mqpublish.go +++ b/netclient/functions/mqpublish.go @@ -5,10 +5,12 @@ import ( "encoding/json" "errors" "fmt" + "net" "os" "sync" "time" + "github.com/cloverstd/tcping/ping" "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/netclient/auth" "github.com/gravitl/netmaker/netclient/config" @@ -140,7 +142,16 @@ func publish(nodeCfg *config.ClientConfig, dest string, msg []byte, qos byte) er err = token.Error() } if err != nil { - return token.Error() + if brokerErr := checkBroker(nodeCfg.Server.Server); brokerErr != nil { + if brokerErr.Error() == "dns" { + logger.Log(0, "dns lookup failed for", nodeCfg.Server.Server, "... update dns records") + } else if brokerErr.Error() == "ping" { + logger.Log(0, "unable to connect to broker ... check firewalls") + } else { + logger.Log(0, "unknown broker connection error") + } + } + return err } } return nil @@ -161,3 +172,25 @@ func checkCertExpiry(cfg *config.ClientConfig) error { } return nil } + +func checkBroker(broker string) error { + _, err := net.LookupIP(broker) + if err != nil { + return errors.New("dns") + } + pinger := ping.NewTCPing() + pinger.SetTarget(&ping.Target{ + Protocol: ping.TCP, + Host: broker, + Port: 8883, + Counter: 3, + Interval: 1 * time.Second, + Timeout: 2 * time.Second, + }) + pingerDone := pinger.Start() + <-pingerDone + if pinger.Result().SuccessCounter == 0 { + return errors.New("ping") + } + return nil +}