nodes_info.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // Roles of the node, e.g. [master, ingest, data]
  134. Roles []string `json:"roles"`
  135. // Attributes of the node.
  136. Attributes map[string]interface{} `json:"attributes"`
  137. // Settings of the node, e.g. paths and pidfile.
  138. Settings map[string]interface{} `json:"settings"`
  139. // OS information, e.g. CPU and memory.
  140. OS *NodesInfoNodeOS `json:"os"`
  141. // Process information, e.g. max file descriptors.
  142. Process *NodesInfoNodeProcess `json:"process"`
  143. // JVM information, e.g. VM version.
  144. JVM *NodesInfoNodeJVM `json:"jvm"`
  145. // ThreadPool information.
  146. ThreadPool *NodesInfoNodeThreadPool `json:"thread_pool"`
  147. // Network information.
  148. Network *NodesInfoNodeNetwork `json:"network"`
  149. // Network information.
  150. Transport *NodesInfoNodeTransport `json:"transport"`
  151. // HTTP information.
  152. HTTP *NodesInfoNodeHTTP `json:"http"`
  153. // Plugins information.
  154. Plugins []*NodesInfoNodePlugin `json:"plugins"`
  155. }
  156. type NodesInfoNodeOS struct {
  157. RefreshInterval string `json:"refresh_interval"` // e.g. 1s
  158. RefreshIntervalInMillis int `json:"refresh_interval_in_millis"` // e.g. 1000
  159. AvailableProcessors int `json:"available_processors"` // e.g. 4
  160. // CPU information
  161. CPU struct {
  162. Vendor string `json:"vendor"` // e.g. Intel
  163. Model string `json:"model"` // e.g. iMac15,1
  164. MHz int `json:"mhz"` // e.g. 3500
  165. TotalCores int `json:"total_cores"` // e.g. 4
  166. TotalSockets int `json:"total_sockets"` // e.g. 4
  167. CoresPerSocket int `json:"cores_per_socket"` // e.g. 16
  168. CacheSizeInBytes int `json:"cache_size_in_bytes"` // e.g. 256
  169. } `json:"cpu"`
  170. // Mem information
  171. Mem struct {
  172. Total string `json:"total"` // e.g. 16gb
  173. TotalInBytes int `json:"total_in_bytes"` // e.g. 17179869184
  174. } `json:"mem"`
  175. // Swap information
  176. Swap struct {
  177. Total string `json:"total"` // e.g. 1gb
  178. TotalInBytes int `json:"total_in_bytes"` // e.g. 1073741824
  179. } `json:"swap"`
  180. }
  181. type NodesInfoNodeProcess struct {
  182. RefreshInterval string `json:"refresh_interval"` // e.g. 1s
  183. RefreshIntervalInMillis int `json:"refresh_interval_in_millis"` // e.g. 1000
  184. ID int `json:"id"` // process id, e.g. 87079
  185. MaxFileDescriptors int `json:"max_file_descriptors"` // e.g. 32768
  186. Mlockall bool `json:"mlockall"` // e.g. false
  187. }
  188. type NodesInfoNodeJVM struct {
  189. PID int `json:"pid"` // process id, e.g. 87079
  190. Version string `json:"version"` // e.g. "1.8.0_25"
  191. VMName string `json:"vm_name"` // e.g. "Java HotSpot(TM) 64-Bit Server VM"
  192. VMVersion string `json:"vm_version"` // e.g. "25.25-b02"
  193. VMVendor string `json:"vm_vendor"` // e.g. "Oracle Corporation"
  194. StartTime time.Time `json:"start_time"` // e.g. "2015-01-03T15:18:30.982Z"
  195. StartTimeInMillis int64 `json:"start_time_in_millis"`
  196. // Mem information
  197. Mem struct {
  198. HeapInit string `json:"heap_init"` // e.g. 1gb
  199. HeapInitInBytes int `json:"heap_init_in_bytes"`
  200. HeapMax string `json:"heap_max"` // e.g. 4gb
  201. HeapMaxInBytes int `json:"heap_max_in_bytes"`
  202. NonHeapInit string `json:"non_heap_init"` // e.g. 2.4mb
  203. NonHeapInitInBytes int `json:"non_heap_init_in_bytes"`
  204. NonHeapMax string `json:"non_heap_max"` // e.g. 0b
  205. NonHeapMaxInBytes int `json:"non_heap_max_in_bytes"`
  206. DirectMax string `json:"direct_max"` // e.g. 4gb
  207. DirectMaxInBytes int `json:"direct_max_in_bytes"`
  208. } `json:"mem"`
  209. GCCollectors []string `json:"gc_collectors"` // e.g. ["ParNew"]
  210. MemoryPools []string `json:"memory_pools"` // e.g. ["Code Cache", "Metaspace"]
  211. }
  212. type NodesInfoNodeThreadPool struct {
  213. Percolate *NodesInfoNodeThreadPoolSection `json:"percolate"`
  214. Bench *NodesInfoNodeThreadPoolSection `json:"bench"`
  215. Listener *NodesInfoNodeThreadPoolSection `json:"listener"`
  216. Index *NodesInfoNodeThreadPoolSection `json:"index"`
  217. Refresh *NodesInfoNodeThreadPoolSection `json:"refresh"`
  218. Suggest *NodesInfoNodeThreadPoolSection `json:"suggest"`
  219. Generic *NodesInfoNodeThreadPoolSection `json:"generic"`
  220. Warmer *NodesInfoNodeThreadPoolSection `json:"warmer"`
  221. Search *NodesInfoNodeThreadPoolSection `json:"search"`
  222. Flush *NodesInfoNodeThreadPoolSection `json:"flush"`
  223. Optimize *NodesInfoNodeThreadPoolSection `json:"optimize"`
  224. Management *NodesInfoNodeThreadPoolSection `json:"management"`
  225. Get *NodesInfoNodeThreadPoolSection `json:"get"`
  226. Merge *NodesInfoNodeThreadPoolSection `json:"merge"`
  227. Bulk *NodesInfoNodeThreadPoolSection `json:"bulk"`
  228. Snapshot *NodesInfoNodeThreadPoolSection `json:"snapshot"`
  229. }
  230. type NodesInfoNodeThreadPoolSection struct {
  231. Type string `json:"type"` // e.g. fixed
  232. Min int `json:"min"` // e.g. 4
  233. Max int `json:"max"` // e.g. 4
  234. KeepAlive string `json:"keep_alive"` // e.g. "5m"
  235. QueueSize interface{} `json:"queue_size"` // e.g. "1k" or -1
  236. }
  237. type NodesInfoNodeNetwork struct {
  238. RefreshInterval string `json:"refresh_interval"` // e.g. 1s
  239. RefreshIntervalInMillis int `json:"refresh_interval_in_millis"` // e.g. 1000
  240. PrimaryInterface struct {
  241. Address string `json:"address"` // e.g. 192.168.1.2
  242. Name string `json:"name"` // e.g. en0
  243. MACAddress string `json:"mac_address"` // e.g. 11:22:33:44:55:66
  244. } `json:"primary_interface"`
  245. }
  246. type NodesInfoNodeTransport struct {
  247. BoundAddress []string `json:"bound_address"`
  248. PublishAddress string `json:"publish_address"`
  249. Profiles map[string]*NodesInfoNodeTransportProfile `json:"profiles"`
  250. }
  251. type NodesInfoNodeTransportProfile struct {
  252. BoundAddress []string `json:"bound_address"`
  253. PublishAddress string `json:"publish_address"`
  254. }
  255. type NodesInfoNodeHTTP struct {
  256. BoundAddress []string `json:"bound_address"` // e.g. ["127.0.0.1:9200", "[fe80::1]:9200", "[::1]:9200"]
  257. PublishAddress string `json:"publish_address"` // e.g. "127.0.0.1:9300"
  258. MaxContentLength string `json:"max_content_length"` // e.g. "100mb"
  259. MaxContentLengthInBytes int64 `json:"max_content_length_in_bytes"`
  260. }
  261. type NodesInfoNodePlugin struct {
  262. Name string `json:"name"`
  263. Description string `json:"description"`
  264. Site bool `json:"site"`
  265. JVM bool `json:"jvm"`
  266. URL string `json:"url"` // e.g. /_plugin/dummy/
  267. }