cluster_health.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2012-2015 Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "net/url"
  9. "strings"
  10. "github.com/olivere/elastic/uritemplates"
  11. )
  12. // ClusterHealthService allows to get the status of the cluster.
  13. // It is documented at http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.4/cluster-health.html.
  14. type ClusterHealthService struct {
  15. client *Client
  16. pretty bool
  17. indices []string
  18. waitForStatus string
  19. level string
  20. local *bool
  21. masterTimeout string
  22. timeout string
  23. waitForActiveShards *int
  24. waitForNodes string
  25. waitForRelocatingShards *int
  26. }
  27. // NewClusterHealthService creates a new ClusterHealthService.
  28. func NewClusterHealthService(client *Client) *ClusterHealthService {
  29. return &ClusterHealthService{client: client, indices: make([]string, 0)}
  30. }
  31. // Index limits the information returned to a specific index.
  32. func (s *ClusterHealthService) Index(index string) *ClusterHealthService {
  33. s.indices = make([]string, 0)
  34. s.indices = append(s.indices, index)
  35. return s
  36. }
  37. // Indices limits the information returned to specific indices.
  38. func (s *ClusterHealthService) Indices(indices ...string) *ClusterHealthService {
  39. s.indices = make([]string, 0)
  40. s.indices = append(s.indices, indices...)
  41. return s
  42. }
  43. // MasterTimeout specifies an explicit operation timeout for connection to master node.
  44. func (s *ClusterHealthService) MasterTimeout(masterTimeout string) *ClusterHealthService {
  45. s.masterTimeout = masterTimeout
  46. return s
  47. }
  48. // Timeout specifies an explicit operation timeout.
  49. func (s *ClusterHealthService) Timeout(timeout string) *ClusterHealthService {
  50. s.timeout = timeout
  51. return s
  52. }
  53. // WaitForActiveShards can be used to wait until the specified number of shards are active.
  54. func (s *ClusterHealthService) WaitForActiveShards(waitForActiveShards int) *ClusterHealthService {
  55. s.waitForActiveShards = &waitForActiveShards
  56. return s
  57. }
  58. // WaitForNodes can be used to wait until the specified number of nodes are available.
  59. func (s *ClusterHealthService) WaitForNodes(waitForNodes string) *ClusterHealthService {
  60. s.waitForNodes = waitForNodes
  61. return s
  62. }
  63. // WaitForRelocatingShards can be used to wait until the specified number of relocating shards is finished.
  64. func (s *ClusterHealthService) WaitForRelocatingShards(waitForRelocatingShards int) *ClusterHealthService {
  65. s.waitForRelocatingShards = &waitForRelocatingShards
  66. return s
  67. }
  68. // WaitForStatus can be used to wait until the cluster is in a specific state.
  69. // Valid values are: green, yellow, or red.
  70. func (s *ClusterHealthService) WaitForStatus(waitForStatus string) *ClusterHealthService {
  71. s.waitForStatus = waitForStatus
  72. return s
  73. }
  74. // Level specifies the level of detail for returned information.
  75. func (s *ClusterHealthService) Level(level string) *ClusterHealthService {
  76. s.level = level
  77. return s
  78. }
  79. // Local indicates whether to return local information. If it is true,
  80. // we do not retrieve the state from master node (default: false).
  81. func (s *ClusterHealthService) Local(local bool) *ClusterHealthService {
  82. s.local = &local
  83. return s
  84. }
  85. // buildURL builds the URL for the operation.
  86. func (s *ClusterHealthService) buildURL() (string, url.Values, error) {
  87. // Build URL
  88. path, err := uritemplates.Expand("/_cluster/health/{index}", map[string]string{
  89. "index": strings.Join(s.indices, ","),
  90. })
  91. if err != nil {
  92. return "", url.Values{}, err
  93. }
  94. // Add query string parameters
  95. params := url.Values{}
  96. if s.waitForRelocatingShards != nil {
  97. params.Set("wait_for_relocating_shards", fmt.Sprintf("%d", *s.waitForRelocatingShards))
  98. }
  99. if s.waitForStatus != "" {
  100. params.Set("wait_for_status", s.waitForStatus)
  101. }
  102. if s.level != "" {
  103. params.Set("level", s.level)
  104. }
  105. if s.local != nil {
  106. params.Set("local", fmt.Sprintf("%v", *s.local))
  107. }
  108. if s.masterTimeout != "" {
  109. params.Set("master_timeout", s.masterTimeout)
  110. }
  111. if s.timeout != "" {
  112. params.Set("timeout", s.timeout)
  113. }
  114. if s.waitForActiveShards != nil {
  115. params.Set("wait_for_active_shards", fmt.Sprintf("%d", *s.waitForActiveShards))
  116. }
  117. if s.waitForNodes != "" {
  118. params.Set("wait_for_nodes", s.waitForNodes)
  119. }
  120. return path, params, nil
  121. }
  122. // Validate checks if the operation is valid.
  123. func (s *ClusterHealthService) Validate() error {
  124. return nil
  125. }
  126. // Do executes the operation.
  127. func (s *ClusterHealthService) Do() (*ClusterHealthResponse, error) {
  128. // Check pre-conditions
  129. if err := s.Validate(); err != nil {
  130. return nil, err
  131. }
  132. // Get URL for request
  133. path, params, err := s.buildURL()
  134. if err != nil {
  135. return nil, err
  136. }
  137. // Get HTTP response
  138. res, err := s.client.PerformRequest("GET", path, params, nil)
  139. if err != nil {
  140. return nil, err
  141. }
  142. // Return operation response
  143. resp := new(ClusterHealthResponse)
  144. if err := json.Unmarshal(res.Body, resp); err != nil {
  145. return nil, err
  146. }
  147. return resp, nil
  148. }
  149. // ClusterHealthResponse is the response of ClusterHealthService.Do.
  150. type ClusterHealthResponse struct {
  151. ClusterName string `json:"cluster_name"`
  152. Status string `json:"status"`
  153. TimedOut bool `json:"timed_out"`
  154. NumberOfNodes int `json:"number_of_nodes"`
  155. NumberOfDataNodes int `json:"number_of_data_nodes"`
  156. ActivePrimaryShards int `json:"active_primary_shards"`
  157. ActiveShards int `json:"active_shards"`
  158. RelocatingShards int `json:"relocating_shards"`
  159. InitializedShards int `json:"initialized_shards"`
  160. UnassignedShards int `json:"unassigned_shards"`
  161. }