diff --git a/etcdctl/ctlv3/command/ep_command.go b/etcdctl/ctlv3/command/ep_command.go index dab6d20dfd9..fb7229f69b2 100644 --- a/etcdctl/ctlv3/command/ep_command.go +++ b/etcdctl/ctlv3/command/ep_command.go @@ -61,9 +61,17 @@ The items in the lists are endpoint, ID, version, db size, is leader, raft term, } } +type epHealth struct { + Ep string `json:"endpoint"` + Health bool `json:"health"` + Took string `json:"took"` + Error string `json:"error,omitempty"` +} + // epHealthCommandFunc executes the "endpoint-health" command. func epHealthCommandFunc(cmd *cobra.Command, args []string) { flags.SetPflagsFromEnv("ETCDCTL", cmd.InheritedFlags()) + initDisplayFromCmd(cmd) endpoints, err := cmd.Flags().GetStringSlice("endpoints") if err != nil { ExitWithError(ExitError, err) @@ -82,7 +90,7 @@ func epHealthCommandFunc(cmd *cobra.Command, args []string) { } var wg sync.WaitGroup - + hch := make(chan epHealth, len(cfgs)) for _, cfg := range cfgs { wg.Add(1) go func(cfg *v3.Config) { @@ -90,7 +98,7 @@ func epHealthCommandFunc(cmd *cobra.Command, args []string) { ep := cfg.Endpoints[0] cli, err := v3.New(*cfg) if err != nil { - fmt.Printf("%s is unhealthy: failed to connect: %v\n", ep, err) + hch <- epHealth{Ep: ep, Health: false, Error: err.Error()} return } st := time.Now() @@ -99,16 +107,25 @@ func epHealthCommandFunc(cmd *cobra.Command, args []string) { ctx, cancel := commandCtx(cmd) _, err = cli.Get(ctx, "health") cancel() + eh := epHealth{Ep: ep, Health: false, Took: time.Since(st).String()} // permission denied is OK since proposal goes through consensus to get it if err == nil || err == rpctypes.ErrPermissionDenied { - fmt.Printf("%s is healthy: successfully committed proposal: took = %v\n", ep, time.Since(st)) + eh.Health = true } else { - fmt.Printf("%s is unhealthy: failed to commit proposal: %v\n", ep, err) + eh.Error = err.Error() } + hch <- eh }(cfg) } wg.Wait() + close(hch) + + healthList := []epHealth{} + for h := range hch { + healthList = append(healthList, h) + } + display.EndpointHealth(healthList) } type epStatus struct { diff --git a/etcdctl/ctlv3/command/printer.go b/etcdctl/ctlv3/command/printer.go index 9926030471d..ea0340ff829 100644 --- a/etcdctl/ctlv3/command/printer.go +++ b/etcdctl/ctlv3/command/printer.go @@ -42,6 +42,7 @@ type printer interface { MemberUpdate(id uint64, r v3.MemberUpdateResponse) MemberList(v3.MemberListResponse) + EndpointHealth([]epHealth) EndpointStatus([]epStatus) Alarm(v3.AlarmResponse) @@ -142,6 +143,7 @@ func newPrinterUnsupported(n string) printer { return &printerUnsupported{printerRPC{nil, f}} } +func (p *printerUnsupported) EndpointHealth([]epHealth) { p.p(nil) } func (p *printerUnsupported) EndpointStatus([]epStatus) { p.p(nil) } func (p *printerUnsupported) DBStatus(dbstatus) { p.p(nil) } @@ -163,6 +165,19 @@ func makeMemberListTable(r v3.MemberListResponse) (hdr []string, rows [][]string return } +func makeEndpointHealthTable(healthList []epHealth) (hdr []string, rows [][]string) { + hdr = []string{"endpoint", "health", "took", "error"} + for _, h := range healthList { + rows = append(rows, []string{ + h.Ep, + fmt.Sprintf("%v", h.Health), + h.Took, + h.Error, + }) + } + return hdr, rows +} + func makeEndpointStatusTable(statusList []epStatus) (hdr []string, rows [][]string) { hdr = []string{"endpoint", "ID", "version", "db size", "is leader", "raft term", "raft index"} for _, status := range statusList { diff --git a/etcdctl/ctlv3/command/printer_fields.go b/etcdctl/ctlv3/command/printer_fields.go index d68ebbe56bf..dd847657fac 100644 --- a/etcdctl/ctlv3/command/printer_fields.go +++ b/etcdctl/ctlv3/command/printer_fields.go @@ -133,6 +133,16 @@ func (p *fieldsPrinter) MemberList(r v3.MemberListResponse) { } } +func (p *fieldsPrinter) EndpointHealth(hs []epHealth) { + for _, h := range hs { + fmt.Printf("\"Endpoint\" : %q\n", h.Ep) + fmt.Println(`"Health" :`, h.Health) + fmt.Println(`"Took" :`, h.Took) + fmt.Println(`"Error" :`, h.Error) + fmt.Println() + } +} + func (p *fieldsPrinter) EndpointStatus(eps []epStatus) { for _, ep := range eps { p.hdr(ep.Resp.Header) diff --git a/etcdctl/ctlv3/command/printer_json.go b/etcdctl/ctlv3/command/printer_json.go index d5d884e5921..b0f4a348352 100644 --- a/etcdctl/ctlv3/command/printer_json.go +++ b/etcdctl/ctlv3/command/printer_json.go @@ -28,6 +28,7 @@ func newJSONPrinter() printer { } } +func (p *jsonPrinter) EndpointHealth(r []epHealth) { printJSON(r) } func (p *jsonPrinter) EndpointStatus(r []epStatus) { printJSON(r) } func (p *jsonPrinter) DBStatus(r dbstatus) { printJSON(r) } diff --git a/etcdctl/ctlv3/command/printer_simple.go b/etcdctl/ctlv3/command/printer_simple.go index b14d3ea228a..1c456ea8ac4 100644 --- a/etcdctl/ctlv3/command/printer_simple.go +++ b/etcdctl/ctlv3/command/printer_simple.go @@ -16,6 +16,7 @@ package command import ( "fmt" + "os" "strings" v3 "github.com/coreos/etcd/clientv3" @@ -128,6 +129,16 @@ func (s *simplePrinter) MemberList(resp v3.MemberListResponse) { } } +func (s *simplePrinter) EndpointHealth(hs []epHealth) { + for _, h := range hs { + if h.Error == "" { + fmt.Fprintf(os.Stderr, "%s is healthy: successfully committed proposal: took = %v\n", h.Ep, h.Took) + } else { + fmt.Fprintf(os.Stderr, "%s is unhealthy: failed to commit proposal: %v", h.Ep, h.Error) + } + } +} + func (s *simplePrinter) EndpointStatus(statusList []epStatus) { _, rows := makeEndpointStatusTable(statusList) for _, row := range rows { diff --git a/etcdctl/ctlv3/command/printer_table.go b/etcdctl/ctlv3/command/printer_table.go index 5bcf8763d0c..d6b314db55b 100644 --- a/etcdctl/ctlv3/command/printer_table.go +++ b/etcdctl/ctlv3/command/printer_table.go @@ -33,6 +33,16 @@ func (tp *tablePrinter) MemberList(r v3.MemberListResponse) { } table.Render() } +func (tp *tablePrinter) EndpointHealth(r []epHealth) { + hdr, rows := makeEndpointHealthTable(r) + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader(hdr) + for _, row := range rows { + table.Append(row) + } + table.SetAlignment(tablewriter.ALIGN_RIGHT) + table.Render() +} func (tp *tablePrinter) EndpointStatus(r []epStatus) { hdr, rows := makeEndpointStatusTable(r) table := tablewriter.NewWriter(os.Stdout)