nodes_info.go 10 KB

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