cluster_health.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "context"
  17. "encoding/json"
  18. "fmt"
  19. "io/ioutil"
  20. "net/http"
  21. "os"
  22. "os/signal"
  23. "time"
  24. "go.etcd.io/etcd/client"
  25. "github.com/urfave/cli"
  26. )
  27. func NewClusterHealthCommand() cli.Command {
  28. return cli.Command{
  29. Name: "cluster-health",
  30. Usage: "check the health of the etcd cluster",
  31. ArgsUsage: " ",
  32. Flags: []cli.Flag{
  33. cli.BoolFlag{Name: "forever, f", Usage: "forever check the health every 10 second until CTRL+C"},
  34. },
  35. Action: handleClusterHealth,
  36. }
  37. }
  38. func handleClusterHealth(c *cli.Context) error {
  39. forever := c.Bool("forever")
  40. if forever {
  41. sigch := make(chan os.Signal, 1)
  42. signal.Notify(sigch, os.Interrupt)
  43. go func() {
  44. <-sigch
  45. os.Exit(0)
  46. }()
  47. }
  48. tr, err := getTransport(c)
  49. if err != nil {
  50. handleError(c, ExitServerError, err)
  51. }
  52. hc := http.Client{
  53. Transport: tr,
  54. }
  55. cln := mustNewClientNoSync(c)
  56. mi := client.NewMembersAPI(cln)
  57. ms, err := mi.List(context.TODO())
  58. if err != nil {
  59. fmt.Println("cluster may be unhealthy: failed to list members")
  60. handleError(c, ExitServerError, err)
  61. }
  62. for {
  63. healthyMembers := 0
  64. for _, m := range ms {
  65. if len(m.ClientURLs) == 0 {
  66. fmt.Printf("member %s is unreachable: no available published client urls\n", m.ID)
  67. continue
  68. }
  69. checked := false
  70. for _, url := range m.ClientURLs {
  71. resp, err := hc.Get(url + "/health")
  72. if err != nil {
  73. fmt.Printf("failed to check the health of member %s on %s: %v\n", m.ID, url, err)
  74. continue
  75. }
  76. result := struct{ Health string }{}
  77. nresult := struct{ Health bool }{}
  78. bytes, err := ioutil.ReadAll(resp.Body)
  79. if err != nil {
  80. fmt.Printf("failed to check the health of member %s on %s: %v\n", m.ID, url, err)
  81. continue
  82. }
  83. resp.Body.Close()
  84. err = json.Unmarshal(bytes, &result)
  85. if err != nil {
  86. err = json.Unmarshal(bytes, &nresult)
  87. }
  88. if err != nil {
  89. fmt.Printf("failed to check the health of member %s on %s: %v\n", m.ID, url, err)
  90. continue
  91. }
  92. checked = true
  93. if result.Health == "true" || nresult.Health {
  94. fmt.Printf("member %s is healthy: got healthy result from %s\n", m.ID, url)
  95. healthyMembers++
  96. } else {
  97. fmt.Printf("member %s is unhealthy: got unhealthy result from %s\n", m.ID, url)
  98. }
  99. break
  100. }
  101. if !checked {
  102. fmt.Printf("member %s is unreachable: %v are all unreachable\n", m.ID, m.ClientURLs)
  103. }
  104. }
  105. switch healthyMembers {
  106. case len(ms):
  107. fmt.Println("cluster is healthy")
  108. case 0:
  109. fmt.Println("cluster is unavailable")
  110. default:
  111. fmt.Println("cluster is degraded")
  112. }
  113. if !forever {
  114. if healthyMembers == len(ms) {
  115. os.Exit(ExitSuccess)
  116. }
  117. os.Exit(ExitClusterNotHealthy)
  118. }
  119. fmt.Printf("\nnext check after 10 second...\n\n")
  120. time.Sleep(10 * time.Second)
  121. }
  122. }