-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NET-3860 - [Supportability] consul troubleshoot CLI for verifying por…
…ts (#19836) * NET-3860 * fix go mod * remove license * dummy commit to trigger ci * fix go mod * fix go mod tidy
- Loading branch information
1 parent
7c289d7
commit 04ef2b9
Showing
15 changed files
with
305 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
```release-note:improvement | ||
cli: Adds cli support for checking TCP connection for ports. If -ports flag is not given, it will check for | ||
default ports of consul listed here - https://developer.hashicorp.com/consul/docs/install/ports | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package ports | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"github.com/hashicorp/consul/troubleshoot/ports" | ||
"os" | ||
|
||
"github.com/hashicorp/consul/command/cli" | ||
"github.com/hashicorp/consul/command/flags" | ||
) | ||
|
||
func New(ui cli.Ui) *cmd { | ||
c := &cmd{UI: ui} | ||
c.init() | ||
return c | ||
} | ||
|
||
type cmd struct { | ||
UI cli.Ui | ||
flags *flag.FlagSet | ||
help string | ||
|
||
// flags | ||
host string | ||
ports string | ||
} | ||
|
||
func (c *cmd) init() { | ||
c.flags = flag.NewFlagSet("", flag.ContinueOnError) | ||
|
||
c.flags.StringVar(&c.host, "host", os.Getenv("CONSUL_HTTP_ADDR"), "The consul server host") | ||
|
||
c.flags.StringVar(&c.ports, "ports", "", "Custom ports to troubleshoot") | ||
|
||
c.help = flags.Usage(help, c.flags) | ||
} | ||
|
||
func (c *cmd) Run(args []string) int { | ||
|
||
if err := c.flags.Parse(args); err != nil { | ||
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err)) | ||
return 1 | ||
} | ||
|
||
if c.host == "" { | ||
c.UI.Error("-host is required. or set environment variable CONSUL_HTTP_ADDR") | ||
return 1 | ||
} | ||
|
||
if c.ports == "" { | ||
ports.TroubleshootDefaultPorts(c.host) | ||
} else { | ||
ports.TroubleShootCustomPorts(c.host, c.ports) | ||
} | ||
return 0 | ||
} | ||
|
||
func (c *cmd) Synopsis() string { | ||
return synopsis | ||
} | ||
|
||
func (c *cmd) Help() string { | ||
return c.help | ||
} | ||
|
||
const ( | ||
synopsis = "Prints open and closed ports on the Consul server" | ||
help = ` | ||
Usage: consul troubleshoot ports [options] | ||
Checks ports for TCP connectivity. Add the -ports flag to check specific ports or omit the -ports flag to check default ports. | ||
Refer to the following reference for default ports: https://developer.hashicorp.com/consul/docs/install/ports | ||
consul troubleshoot ports -host localhost | ||
or | ||
export CONSUL_HTTP_ADDR=localhost | ||
consul troubleshoot ports | ||
Use the -ports flag to check non-default ports, for example: | ||
consul troubleshoot ports -host localhost -ports 1023,1024 | ||
or | ||
export CONSUL_HTTP_ADDR=localhost | ||
consul troubleshoot ports -ports 1234,8500 | ||
` | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package ports | ||
|
||
type hostPort struct { | ||
host string | ||
port string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ports | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func TroubleshootDefaultPorts(host string) []string { | ||
// Source - https://developer.hashicorp.com/consul/docs/install/ports | ||
ports := []string{"8600", "8500", "8501", "8502", "8503", "8301", "8302", "8300"} | ||
return troubleshootRun(ports, host) | ||
} | ||
|
||
func TroubleShootCustomPorts(host string, ports string) []string { | ||
portsArr := strings.Split(ports, ",") | ||
return troubleshootRun(portsArr, host) | ||
} | ||
|
||
func troubleshootRun(ports []string, host string) []string { | ||
|
||
resultsChannel := make(chan string) | ||
defer close(resultsChannel) | ||
|
||
var counter = 0 | ||
|
||
for _, port := range ports { | ||
counter += 1 | ||
tcpTroubleShoot := troubleShootTcp{} | ||
port := port | ||
go func() { | ||
err := tcpTroubleShoot.dialPort(&hostPort{host: host, port: port}) | ||
var res string | ||
if err != nil { | ||
res = fmt.Sprintf("TCP: Port %s on %s is closed, unreachable, or the connection timed out.\n", port, host) | ||
} else { | ||
// If no error occurs, the connection was successful, and the port is open. | ||
res = fmt.Sprintf("TCP: Port %s on %s is open.\n", port, host) | ||
} | ||
resultsChannel <- res | ||
}() | ||
} | ||
|
||
resultsArr := make([]string, counter) | ||
for itr := 0; itr < counter; itr++ { | ||
res := <-resultsChannel | ||
fmt.Print(res) | ||
resultsArr[itr] = res | ||
} | ||
return resultsArr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ports | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/consul/sdk/testutil" | ||
"github.com/stretchr/testify/require" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestTroubleShootCustom_Ports(t *testing.T) { | ||
// Create a test Consul server. | ||
srv1, err := testutil.NewTestServerConfigT(t, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
results := TroubleShootCustomPorts("127.0.0.1", strings.Join([]string{ | ||
strconv.Itoa(srv1.Config.Ports.HTTP), | ||
strconv.Itoa(srv1.Config.Ports.DNS), | ||
strconv.Itoa(srv1.Config.Ports.HTTPS), | ||
strconv.Itoa(srv1.Config.Ports.GRPC), | ||
strconv.Itoa(srv1.Config.Ports.SerfLan), | ||
strconv.Itoa(srv1.Config.Ports.SerfWan), | ||
strconv.Itoa(srv1.Config.Ports.Server)}, ",")) | ||
expectedResults := []string{ | ||
fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.HTTP)), | ||
fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.GRPC)), | ||
fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.HTTPS)), | ||
fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.SerfLan)), | ||
fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.SerfWan)), | ||
fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.DNS)), | ||
fmt.Sprintf("TCP: Port %s on 127.0.0.1 is open.\n", strconv.Itoa(srv1.Config.Ports.Server)), | ||
} | ||
for _, res := range expectedResults { | ||
require.Contains(t, results, res) | ||
} | ||
defer srv1.Stop() | ||
} | ||
|
||
func TestTroubleShootCustom_Ports_Not_Reachable(t *testing.T) { | ||
results := TroubleShootCustomPorts("127.0.0.1", strings.Join([]string{"8777", "8888"}, ",")) | ||
|
||
expectedResults := []string{ | ||
fmt.Sprintf("TCP: Port 8777 on 127.0.0.1 is closed, unreachable, or the connection timed out.\n"), | ||
fmt.Sprintf("TCP: Port 8888 on 127.0.0.1 is closed, unreachable, or the connection timed out.\n"), | ||
} | ||
for _, res := range expectedResults { | ||
require.Contains(t, results, res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ports | ||
|
||
type troubleShootProtocol interface { | ||
dialPort(hostPort *hostPort) error | ||
} |
Oops, something went wrong.