cluster_health.go 3.3 KB

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