nodes_info.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/url"
  9. "strings"
  10. "time"
  11. "gopkg.in/olivere/elastic.v5/uritemplates"
  12. )
  13. // NodesInfoService allows to retrieve one or more or all of the
  14. // cluster nodes information.
  15. // It is documented at https://www.elastic.co/guide/en/elasticsearch/reference/5.2/cluster-nodes-info.html.
  16. type NodesInfoService struct {
  17. client *Client
  18. pretty bool
  19. nodeId []string
  20. metric []string
  21. flatSettings *bool
  22. human *bool
  23. }
  24. // NewNodesInfoService creates a new NodesInfoService.
  25. func NewNodesInfoService(client *Client) *NodesInfoService {
  26. return &NodesInfoService{
  27. client: client,
  28. nodeId: []string{"_all"},
  29. metric: []string{"_all"},
  30. }
  31. }
  32. // NodeId is a list of node IDs or names to limit the returned information.
  33. // Use "_local" to return information from the node you're connecting to,
  34. // leave empty to get information from all nodes.
  35. func (s *NodesInfoService) NodeId(nodeId ...string) *NodesInfoService {
  36. s.nodeId = append(s.nodeId, nodeId...)
  37. return s
  38. }
  39. // Metric is a list of metrics you wish returned. Leave empty to return all.
  40. // Valid metrics are: settings, os, process, jvm, thread_pool, network,
  41. // transport, http, and plugins.
  42. func (s *NodesInfoService) Metric(metric ...string) *NodesInfoService {
  43. s.metric = append(s.metric, metric...)
  44. return s
  45. }
  46. // FlatSettings returns settings in flat format (default: false).
  47. func (s *NodesInfoService) FlatSettings(flatSettings bool) *NodesInfoService {
  48. s.flatSettings = &flatSettings
  49. return s
  50. }
  51. // Human indicates whether to return time and byte values in human-readable format.
  52. func (s *NodesInfoService) Human(human bool) *NodesInfoService {
  53. s.human = &human
  54. return s
  55. }
  56. // Pretty indicates whether to indent the returned JSON.
  57. func (s *NodesInfoService) Pretty(pretty bool) *NodesInfoService {
  58. s.pretty = pretty
  59. return s
  60. }
  61. // buildURL builds the URL for the operation.
  62. func (s *NodesInfoService) buildURL() (string, url.Values, error) {
  63. // Build URL
  64. path, err := uritemplates.Expand("/_nodes/{node_id}/{metric}", map[string]string{
  65. "node_id": strings.Join(s.nodeId, ","),
  66. "metric": strings.Join(s.metric, ","),
  67. })
  68. if err != nil {
  69. return "", url.Values{}, err
  70. }
  71. // Add query string parameters
  72. params := url.Values{}
  73. if s.flatSettings != nil {
  74. params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
  75. }
  76. if s.human != nil {
  77. params.Set("human", fmt.Sprintf("%v", *s.human))
  78. }
  79. if s.pretty {
  80. params.Set("pretty", "1")
  81. }
  82. return path, params, nil
  83. }
  84. // Validate checks if the operation is valid.
  85. func (s *NodesInfoService) Validate() error {
  86. return nil
  87. }
  88. // Do executes the operation.
  89. func (s *NodesInfoService) Do(ctx context.Context) (*NodesInfoResponse, error) {
  90. // Check pre-conditions
  91. if err := s.Validate(); err != nil {
  92. return nil, err
  93. }
  94. // Get URL for request
  95. path, params, err := s.buildURL()
  96. if err != nil {
  97. return nil, err
  98. }
  99. // Get HTTP response
  100. res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
  101. if err != nil {
  102. return nil, err
  103. }
  104. // Return operation response
  105. ret := new(NodesInfoResponse)
  106. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  107. return nil, err
  108. }
  109. return ret, nil
  110. }
  111. // NodesInfoResponse is the response of NodesInfoService.Do.
  112. type NodesInfoResponse struct {
  113. ClusterName string `json:"cluster_name"`
  114. Nodes map[string]*NodesInfoNode `json:"nodes"`
  115. }
  116. type NodesInfoNode struct {
  117. // Name of the node, e.g. "Mister Fear"
  118. Name string `json:"name"`
  119. // TransportAddress, e.g. "127.0.0.1:9300"
  120. TransportAddress string `json:"transport_address"`
  121. // Host is the host name, e.g. "macbookair"
  122. Host string `json:"host"`
  123. // IP is the IP address, e.g. "192.168.1.2"
  124. IP string `json:"ip"`
  125. // Version is the Elasticsearch version running on the node, e.g. "1.4.3"
  126. Version string `json:"version"`
  127. // Build is the Elasticsearch build, e.g. "36a29a7"
  128. Build string `json:"build"`
  129. // HTTPAddress, e.g. "127.0.0.1:9200"
  130. HTTPAddress string `json:"http_address"`
  131. // HTTPSAddress, e.g. "127.0.0.1:9200"
  132. HTTPSAddress string `json:"https_address"`
  133. // Attributes of the node.
  134. Attributes map[string]interface{} `json:"attributes"`
  135. // Settings of the node, e.g. paths and pidfile.
  136. Settings map[string]interface{} `json:"settings"`
  137. // OS information, e.g. CPU and memory.
  138. OS *NodesInfoNodeOS `json:"os"`
  139. // Process information, e.g. max file descriptors.
  140. Process *NodesInfoNodeProcess `json:"process"`
  141. // JVM information, e.g. VM version.
  142. JVM *NodesInfoNodeJVM `json:"jvm"`
  143. // ThreadPool information.
  144. ThreadPool *NodesInfoNodeThreadPool `json:"thread_pool"`
  145. // Network information.
  146. Network *NodesInfoNodeNetwork `json:"network"`
  147. // Network information.
  148. Transport *NodesInfoNodeTransport `json:"transport"`
  149. // HTTP information.
  150. HTTP *NodesInfoNodeHTTP `json:"http"`
  151. // Plugins information.
  152. Plugins []*NodesInfoNodePlugin `json:"plugins"`
  153. }
  154. type NodesInfoNodeOS struct {
  155. RefreshInterval string `json:"refresh_interval"` // e.g. 1s
  156. RefreshIntervalInMillis int `json:"refresh_interval_in_millis"` // e.g. 1000
  157. AvailableProcessors int `json:"available_processors"` // e.g. 4
  158. // CPU information
  159. CPU struct {
  160. Vendor string `json:"vendor"` // e.g. Intel
  161. Model string `json:"model"` // e.g. iMac15,1
  162. MHz int `json:"mhz"` // e.g. 3500
  163. TotalCores int `json:"total_cores"` // e.g. 4
  164. TotalSockets int `json:"total_sockets"` // e.g. 4
  165. CoresPerSocket int `json:"cores_per_socket"` // e.g. 16
  166. CacheSizeInBytes int `json:"cache_size_in_bytes"` // e.g. 256
  167. } `json:"cpu"`
  168. // Mem information
  169. Mem struct {
  170. Total string `json:"total"` // e.g. 16gb
  171. TotalInBytes int `json:"total_in_bytes"` // e.g. 17179869184
  172. } `json:"mem"`
  173. // Swap information
  174. Swap struct {
  175. Total string `json:"total"` // e.g. 1gb
  176. TotalInBytes int `json:"total_in_bytes"` // e.g. 1073741824
  177. } `json:"swap"`
  178. }
  179. type NodesInfoNodeProcess struct {
  180. RefreshInterval string `json:"refresh_interval"` // e.g. 1s
  181. RefreshIntervalInMillis int `json:"refresh_interval_in_millis"` // e.g. 1000
  182. ID int `json:"id"` // process id, e.g. 87079
  183. MaxFileDescriptors int `json:"max_file_descriptors"` // e.g. 32768
  184. Mlockall bool `json:"mlockall"` // e.g. false
  185. }
  186. type NodesInfoNodeJVM struct {
  187. PID int `json:"pid"` // process id, e.g. 87079
  188. Version string `json:"version"` // e.g. "1.8.0_25"
  189. VMName string `json:"vm_name"` // e.g. "Java HotSpot(TM) 64-Bit Server VM"
  190. VMVersion string `json:"vm_version"` // e.g. "25.25-b02"
  191. VMVendor string `json:"vm_vendor"` // e.g. "Oracle Corporation"
  192. StartTime time.Time `json:"start_time"` // e.g. "2015-01-03T15:18:30.982Z"
  193. StartTimeInMillis int64 `json:"start_time_in_millis"`
  194. // Mem information
  195. Mem struct {
  196. HeapInit string `json:"heap_init"` // e.g. 1gb
  197. HeapInitInBytes int `json:"heap_init_in_bytes"`
  198. HeapMax string `json:"heap_max"` // e.g. 4gb
  199. HeapMaxInBytes int `json:"heap_max_in_bytes"`
  200. NonHeapInit string `json:"non_heap_init"` // e.g. 2.4mb
  201. NonHeapInitInBytes int `json:"non_heap_init_in_bytes"`
  202. NonHeapMax string `json:"non_heap_max"` // e.g. 0b
  203. NonHeapMaxInBytes int `json:"non_heap_max_in_bytes"`
  204. DirectMax string `json:"direct_max"` // e.g. 4gb
  205. DirectMaxInBytes int `json:"direct_max_in_bytes"`
  206. } `json:"mem"`
  207. GCCollectors []string `json:"gc_collectors"` // e.g. ["ParNew"]
  208. MemoryPools []string `json:"memory_pools"` // e.g. ["Code Cache", "Metaspace"]
  209. }
  210. type NodesInfoNodeThreadPool struct {
  211. Percolate *NodesInfoNodeThreadPoolSection `json:"percolate"`
  212. Bench *NodesInfoNodeThreadPoolSection `json:"bench"`
  213. Listener *NodesInfoNodeThreadPoolSection `json:"listener"`
  214. Index *NodesInfoNodeThreadPoolSection `json:"index"`
  215. Refresh *NodesInfoNodeThreadPoolSection `json:"refresh"`
  216. Suggest *NodesInfoNodeThreadPoolSection `json:"suggest"`
  217. Generic *NodesInfoNodeThreadPoolSection `json:"generic"`
  218. Warmer *NodesInfoNodeThreadPoolSection `json:"warmer"`
  219. Search *NodesInfoNodeThreadPoolSection `json:"search"`
  220. Flush *NodesInfoNodeThreadPoolSection `json:"flush"`
  221. Optimize *NodesInfoNodeThreadPoolSection `json:"optimize"`
  222. Management *NodesInfoNodeThreadPoolSection `json:"management"`
  223. Get *NodesInfoNodeThreadPoolSection `json:"get"`
  224. Merge *NodesInfoNodeThreadPoolSection `json:"merge"`
  225. Bulk *NodesInfoNodeThreadPoolSection `json:"bulk"`
  226. Snapshot *NodesInfoNodeThreadPoolSection `json:"snapshot"`
  227. }
  228. type NodesInfoNodeThreadPoolSection struct {
  229. Type string `json:"type"` // e.g. fixed
  230. Min int `json:"min"` // e.g. 4
  231. Max int `json:"max"` // e.g. 4
  232. KeepAlive string `json:"keep_alive"` // e.g. "5m"
  233. QueueSize interface{} `json:"queue_size"` // e.g. "1k" or -1
  234. }
  235. type NodesInfoNodeNetwork struct {
  236. RefreshInterval string `json:"refresh_interval"` // e.g. 1s
  237. RefreshIntervalInMillis int `json:"refresh_interval_in_millis"` // e.g. 1000
  238. PrimaryInterface struct {
  239. Address string `json:"address"` // e.g. 192.168.1.2
  240. Name string `json:"name"` // e.g. en0
  241. MACAddress string `json:"mac_address"` // e.g. 11:22:33:44:55:66
  242. } `json:"primary_interface"`
  243. }
  244. type NodesInfoNodeTransport struct {
  245. BoundAddress []string `json:"bound_address"`
  246. PublishAddress string `json:"publish_address"`
  247. Profiles map[string]*NodesInfoNodeTransportProfile `json:"profiles"`
  248. }
  249. type NodesInfoNodeTransportProfile struct {
  250. BoundAddress []string `json:"bound_address"`
  251. PublishAddress string `json:"publish_address"`
  252. }
  253. type NodesInfoNodeHTTP struct {
  254. BoundAddress []string `json:"bound_address"` // e.g. ["127.0.0.1:9200", "[fe80::1]:9200", "[::1]:9200"]
  255. PublishAddress string `json:"publish_address"` // e.g. "127.0.0.1:9300"
  256. MaxContentLength string `json:"max_content_length"` // e.g. "100mb"
  257. MaxContentLengthInBytes int64 `json:"max_content_length_in_bytes"`
  258. }
  259. type NodesInfoNodePlugin struct {
  260. Name string `json:"name"`
  261. Description string `json:"description"`
  262. Site bool `json:"site"`
  263. JVM bool `json:"jvm"`
  264. URL string `json:"url"` // e.g. /_plugin/dummy/
  265. }