ep_command.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "fmt"
  17. "os"
  18. "sync"
  19. "time"
  20. v3 "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/pkg/flags"
  22. "github.com/spf13/cobra"
  23. )
  24. var (
  25. healthCheckKey string
  26. )
  27. // NewEndpointCommand returns the cobra command for "endpoint".
  28. func NewEndpointCommand() *cobra.Command {
  29. ec := &cobra.Command{
  30. Use: "endpoint <subcommand>",
  31. Short: "Endpoint related commands",
  32. }
  33. ec.AddCommand(newEpHealthCommand())
  34. ec.AddCommand(newEpStatusCommand())
  35. return ec
  36. }
  37. func newEpHealthCommand() *cobra.Command {
  38. cmd := &cobra.Command{
  39. Use: "health",
  40. Short: "Checks the healthiness of endpoints specified in `--endpoints` flag",
  41. Run: epHealthCommandFunc,
  42. }
  43. cmd.Flags().StringVar(&healthCheckKey, "health-check-key", "health", "The key used to perform the health check. Makes sure the user can access the key.")
  44. return cmd
  45. }
  46. func newEpStatusCommand() *cobra.Command {
  47. return &cobra.Command{
  48. Use: "status",
  49. Short: "Prints out the status of endpoints specified in `--endpoints` flag",
  50. Long: `When --write-out is set to simple, this command prints out comma-separated status lists for each endpoint.
  51. The items in the lists are endpoint, ID, version, db size, is leader, raft term, raft index.
  52. `,
  53. Run: epStatusCommandFunc,
  54. }
  55. }
  56. // epHealthCommandFunc executes the "endpoint-health" command.
  57. func epHealthCommandFunc(cmd *cobra.Command, args []string) {
  58. flags.SetPflagsFromEnv("ETCDCTL", cmd.InheritedFlags())
  59. endpoints, err := cmd.Flags().GetStringSlice("endpoints")
  60. if err != nil {
  61. ExitWithError(ExitError, err)
  62. }
  63. sec := secureCfgFromCmd(cmd)
  64. dt := dialTimeoutFromCmd(cmd)
  65. auth := authCfgFromCmd(cmd)
  66. cfgs := []*v3.Config{}
  67. for _, ep := range endpoints {
  68. cfg, err := newClientCfg([]string{ep}, dt, sec, auth)
  69. if err != nil {
  70. ExitWithError(ExitBadArgs, err)
  71. }
  72. cfgs = append(cfgs, cfg)
  73. }
  74. var wg sync.WaitGroup
  75. for _, cfg := range cfgs {
  76. wg.Add(1)
  77. go func(cfg *v3.Config) {
  78. defer wg.Done()
  79. ep := cfg.Endpoints[0]
  80. cli, err := v3.New(*cfg)
  81. if err != nil {
  82. fmt.Printf("%s is unhealthy: failed to connect: %v\n", ep, err)
  83. return
  84. }
  85. st := time.Now()
  86. // get a random key. As long as we can get the response without an error, the
  87. // endpoint is health.
  88. ctx, cancel := commandCtx(cmd)
  89. _, err = cli.Get(ctx, healthCheckKey)
  90. cancel()
  91. if err != nil {
  92. fmt.Printf("%s is unhealthy: failed to commit proposal: %v\n", ep, err)
  93. } else {
  94. fmt.Printf("%s is healthy: successfully committed proposal: took = %v\n", ep, time.Since(st))
  95. }
  96. }(cfg)
  97. }
  98. wg.Wait()
  99. }
  100. type epStatus struct {
  101. Ep string `json:"Endpoint"`
  102. Resp *v3.StatusResponse `json:"Status"`
  103. }
  104. func epStatusCommandFunc(cmd *cobra.Command, args []string) {
  105. c := mustClientFromCmd(cmd)
  106. statusList := []epStatus{}
  107. var err error
  108. for _, ep := range c.Endpoints() {
  109. ctx, cancel := commandCtx(cmd)
  110. resp, serr := c.Status(ctx, ep)
  111. cancel()
  112. if serr != nil {
  113. err = serr
  114. fmt.Fprintf(os.Stderr, "Failed to get the status of endpoint %s (%v)\n", ep, serr)
  115. continue
  116. }
  117. statusList = append(statusList, epStatus{Ep: ep, Resp: resp})
  118. }
  119. display.EndpointStatus(statusList)
  120. if err != nil {
  121. os.Exit(ExitError)
  122. }
  123. }