cat_health.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2012-present 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. "context"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. )
  12. // CatHealthService returns a terse representation of the same information
  13. // as /_cluster/health.
  14. //
  15. // See https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cat-health.html
  16. // for details.
  17. type CatHealthService struct {
  18. client *Client
  19. pretty *bool // pretty format the returned JSON response
  20. human *bool // return human readable values for statistics
  21. errorTrace *bool // include the stack trace of returned errors
  22. filterPath []string // list of filters used to reduce the response
  23. headers http.Header // custom request-level HTTP headers
  24. local *bool
  25. masterTimeout string
  26. columns []string
  27. sort []string // list of columns for sort order
  28. disableTimestamping *bool
  29. }
  30. // NewCatHealthService creates a new CatHealthService.
  31. func NewCatHealthService(client *Client) *CatHealthService {
  32. return &CatHealthService{
  33. client: client,
  34. }
  35. }
  36. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  37. func (s *CatHealthService) Pretty(pretty bool) *CatHealthService {
  38. s.pretty = &pretty
  39. return s
  40. }
  41. // Human specifies whether human readable values should be returned in
  42. // the JSON response, e.g. "7.5mb".
  43. func (s *CatHealthService) Human(human bool) *CatHealthService {
  44. s.human = &human
  45. return s
  46. }
  47. // ErrorTrace specifies whether to include the stack trace of returned errors.
  48. func (s *CatHealthService) ErrorTrace(errorTrace bool) *CatHealthService {
  49. s.errorTrace = &errorTrace
  50. return s
  51. }
  52. // FilterPath specifies a list of filters used to reduce the response.
  53. func (s *CatHealthService) FilterPath(filterPath ...string) *CatHealthService {
  54. s.filterPath = filterPath
  55. return s
  56. }
  57. // Header adds a header to the request.
  58. func (s *CatHealthService) Header(name string, value string) *CatHealthService {
  59. if s.headers == nil {
  60. s.headers = http.Header{}
  61. }
  62. s.headers.Add(name, value)
  63. return s
  64. }
  65. // Headers specifies the headers of the request.
  66. func (s *CatHealthService) Headers(headers http.Header) *CatHealthService {
  67. s.headers = headers
  68. return s
  69. }
  70. // Local indicates to return local information, i.e. do not retrieve
  71. // the state from master node (default: false).
  72. func (s *CatHealthService) Local(local bool) *CatHealthService {
  73. s.local = &local
  74. return s
  75. }
  76. // MasterTimeout is the explicit operation timeout for connection to master node.
  77. func (s *CatHealthService) MasterTimeout(masterTimeout string) *CatHealthService {
  78. s.masterTimeout = masterTimeout
  79. return s
  80. }
  81. // Columns to return in the response.
  82. // To get a list of all possible columns to return, run the following command
  83. // in your terminal:
  84. //
  85. // Example:
  86. // curl 'http://localhost:9200/_cat/indices?help'
  87. //
  88. // You can use Columns("*") to return all possible columns. That might take
  89. // a little longer than the default set of columns.
  90. func (s *CatHealthService) Columns(columns ...string) *CatHealthService {
  91. s.columns = columns
  92. return s
  93. }
  94. // Sort is a list of fields to sort by.
  95. func (s *CatHealthService) Sort(fields ...string) *CatHealthService {
  96. s.sort = fields
  97. return s
  98. }
  99. // DisableTimestamping disables timestamping (default: true).
  100. func (s *CatHealthService) DisableTimestamping(disable bool) *CatHealthService {
  101. s.disableTimestamping = &disable
  102. return s
  103. }
  104. // buildURL builds the URL for the operation.
  105. func (s *CatHealthService) buildURL() (string, url.Values, error) {
  106. // Build URL
  107. path := "/_cat/health"
  108. // Add query string parameters
  109. params := url.Values{
  110. "format": []string{"json"}, // always returns as JSON
  111. }
  112. if v := s.pretty; v != nil {
  113. params.Set("pretty", fmt.Sprint(*v))
  114. }
  115. if v := s.human; v != nil {
  116. params.Set("human", fmt.Sprint(*v))
  117. }
  118. if v := s.errorTrace; v != nil {
  119. params.Set("error_trace", fmt.Sprint(*v))
  120. }
  121. if len(s.filterPath) > 0 {
  122. params.Set("filter_path", strings.Join(s.filterPath, ","))
  123. }
  124. if v := s.local; v != nil {
  125. params.Set("local", fmt.Sprint(*v))
  126. }
  127. if s.masterTimeout != "" {
  128. params.Set("master_timeout", s.masterTimeout)
  129. }
  130. if len(s.sort) > 0 {
  131. params.Set("s", strings.Join(s.sort, ","))
  132. }
  133. if v := s.disableTimestamping; v != nil {
  134. params.Set("ts", fmt.Sprint(*v))
  135. }
  136. if len(s.columns) > 0 {
  137. params.Set("h", strings.Join(s.columns, ","))
  138. }
  139. return path, params, nil
  140. }
  141. // Do executes the operation.
  142. func (s *CatHealthService) Do(ctx context.Context) (CatHealthResponse, error) {
  143. // Get URL for request
  144. path, params, err := s.buildURL()
  145. if err != nil {
  146. return nil, err
  147. }
  148. // Get HTTP response
  149. res, err := s.client.PerformRequestWithOptions(ctx, PerformRequestOptions{
  150. Method: "GET",
  151. Path: path,
  152. Params: params,
  153. Headers: s.headers,
  154. })
  155. if err != nil {
  156. return nil, err
  157. }
  158. // Return operation response
  159. var ret CatHealthResponse
  160. if err := s.client.decoder.Decode(res.Body, &ret); err != nil {
  161. return nil, err
  162. }
  163. return ret, nil
  164. }
  165. // -- Result of a get request.
  166. // CatHealthResponse is the outcome of CatHealthService.Do.
  167. type CatHealthResponse []CatHealthResponseRow
  168. // CatHealthResponseRow is a single row in a CatHealthResponse.
  169. // Notice that not all of these fields might be filled; that depends
  170. // on the number of columns chose in the request (see CatHealthService.Columns).
  171. type CatHealthResponseRow struct {
  172. Epoch int64 `json:"epoch,string"` // e.g. 1527077996
  173. Timestamp string `json:"timestamp"` // e.g. "12:19:56"
  174. Cluster string `json:"cluster"` // cluster name, e.g. "elasticsearch"
  175. Status string `json:"status"` // health status, e.g. "green", "yellow", or "red"
  176. NodeTotal int `json:"node.total,string"` // total number of nodes
  177. NodeData int `json:"node.data,string"` // number of nodes that can store data
  178. Shards int `json:"shards,string"` // total number of shards
  179. Pri int `json:"pri,string"` // number of primary shards
  180. Relo int `json:"relo,string"` // number of relocating nodes
  181. Init int `json:"init,string"` // number of initializing nodes
  182. Unassign int `json:"unassign,string"` // number of unassigned shards
  183. PendingTasks int `json:"pending_tasks,string"` // number of pending tasks
  184. MaxTaskWaitTime string `json:"max_task_wait_time"` // wait time of longest task pending, e.g. "-" or time in millis
  185. ActiveShardsPercent string `json:"active_shards_percent"` // active number of shards in percent, e.g. "100%"
  186. }