You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

419 lines
15 KiB

  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. "time"
  12. "github.com/olivere/elastic/v7/uritemplates"
  13. )
  14. // NodesInfoService allows to retrieve one or more or all of the
  15. // cluster nodes information.
  16. // It is documented at https://www.elastic.co/guide/en/elasticsearch/reference/7.0/cluster-nodes-info.html.
  17. type NodesInfoService 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. nodeId []string
  25. metric []string
  26. flatSettings *bool
  27. }
  28. // NewNodesInfoService creates a new NodesInfoService.
  29. func NewNodesInfoService(client *Client) *NodesInfoService {
  30. return &NodesInfoService{
  31. client: client,
  32. }
  33. }
  34. // Pretty tells Elasticsearch whether to return a formatted JSON response.
  35. func (s *NodesInfoService) Pretty(pretty bool) *NodesInfoService {
  36. s.pretty = &pretty
  37. return s
  38. }
  39. // Human specifies whether human readable values should be returned in
  40. // the JSON response, e.g. "7.5mb".
  41. func (s *NodesInfoService) Human(human bool) *NodesInfoService {
  42. s.human = &human
  43. return s
  44. }
  45. // ErrorTrace specifies whether to include the stack trace of returned errors.
  46. func (s *NodesInfoService) ErrorTrace(errorTrace bool) *NodesInfoService {
  47. s.errorTrace = &errorTrace
  48. return s
  49. }
  50. // FilterPath specifies a list of filters used to reduce the response.
  51. func (s *NodesInfoService) FilterPath(filterPath ...string) *NodesInfoService {
  52. s.filterPath = filterPath
  53. return s
  54. }
  55. // Header adds a header to the request.
  56. func (s *NodesInfoService) Header(name string, value string) *NodesInfoService {
  57. if s.headers == nil {
  58. s.headers = http.Header{}
  59. }
  60. s.headers.Add(name, value)
  61. return s
  62. }
  63. // Headers specifies the headers of the request.
  64. func (s *NodesInfoService) Headers(headers http.Header) *NodesInfoService {
  65. s.headers = headers
  66. return s
  67. }
  68. // NodeId is a list of node IDs or names to limit the returned information.
  69. // Use "_local" to return information from the node you're connecting to,
  70. // leave empty to get information from all nodes.
  71. func (s *NodesInfoService) NodeId(nodeId ...string) *NodesInfoService {
  72. s.nodeId = append(s.nodeId, nodeId...)
  73. return s
  74. }
  75. // Metric is a list of metrics you wish returned. Leave empty to return all.
  76. // Valid metrics are: settings, os, process, jvm, thread_pool, network,
  77. // transport, http, and plugins.
  78. func (s *NodesInfoService) Metric(metric ...string) *NodesInfoService {
  79. s.metric = append(s.metric, metric...)
  80. return s
  81. }
  82. // FlatSettings returns settings in flat format (default: false).
  83. func (s *NodesInfoService) FlatSettings(flatSettings bool) *NodesInfoService {
  84. s.flatSettings = &flatSettings
  85. return s
  86. }
  87. // buildURL builds the URL for the operation.
  88. func (s *NodesInfoService) buildURL() (string, url.Values, error) {
  89. var nodeId, metric string
  90. if len(s.nodeId) > 0 {
  91. nodeId = strings.Join(s.nodeId, ",")
  92. } else {
  93. nodeId = "_all"
  94. }
  95. if len(s.metric) > 0 {
  96. metric = strings.Join(s.metric, ",")
  97. } else {
  98. metric = "_all"
  99. }
  100. // Build URL
  101. path, err := uritemplates.Expand("/_nodes/{node_id}/{metric}", map[string]string{
  102. "node_id": nodeId,
  103. "metric": metric,
  104. })
  105. if err != nil {
  106. return "", url.Values{}, err
  107. }
  108. // Add query string parameters
  109. params := url.Values{}
  110. if v := s.pretty; v != nil {
  111. params.Set("pretty", fmt.Sprint(*v))
  112. }
  113. if v := s.human; v != nil {
  114. params.Set("human", fmt.Sprint(*v))
  115. }
  116. if v := s.errorTrace; v != nil {
  117. params.Set("error_trace", fmt.Sprint(*v))
  118. }
  119. if len(s.filterPath) > 0 {
  120. params.Set("filter_path", strings.Join(s.filterPath, ","))
  121. }
  122. if s.flatSettings != nil {
  123. params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
  124. }
  125. return path, params, nil
  126. }
  127. // Validate checks if the operation is valid.
  128. func (s *NodesInfoService) Validate() error {
  129. return nil
  130. }
  131. // Do executes the operation.
  132. func (s *NodesInfoService) Do(ctx context.Context) (*NodesInfoResponse, error) {
  133. // Check pre-conditions
  134. if err := s.Validate(); err != nil {
  135. return nil, err
  136. }
  137. // Get URL for request
  138. path, params, err := s.buildURL()
  139. if err != nil {
  140. return nil, err
  141. }
  142. // Get HTTP response
  143. res, err := s.client.PerformRequest(ctx, PerformRequestOptions{
  144. Method: "GET",
  145. Path: path,
  146. Params: params,
  147. Headers: s.headers,
  148. })
  149. if err != nil {
  150. return nil, err
  151. }
  152. // Return operation response
  153. ret := new(NodesInfoResponse)
  154. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  155. return nil, err
  156. }
  157. return ret, nil
  158. }
  159. // NodesInfoResponse is the response of NodesInfoService.Do.
  160. type NodesInfoResponse struct {
  161. ClusterName string `json:"cluster_name"`
  162. Nodes map[string]*NodesInfoNode `json:"nodes"`
  163. }
  164. // NodesInfoNode represents information about a node in the cluster.
  165. type NodesInfoNode struct {
  166. // Name of the node, e.g. "Mister Fear"
  167. Name string `json:"name"`
  168. // TransportAddress, e.g. "127.0.0.1:9300"
  169. TransportAddress string `json:"transport_address"`
  170. // Host is the host name, e.g. "macbookair"
  171. Host string `json:"host"`
  172. // IP is the IP address, e.g. "192.168.1.2"
  173. IP string `json:"ip"`
  174. // Version is the Elasticsearch version running on the node, e.g. "1.4.3"
  175. Version string `json:"version"`
  176. // BuildHash is the Elasticsearch build bash, e.g. "36a29a7"
  177. BuildHash string `json:"build_hash"`
  178. // TotalIndexingBuffer represents the total heap allowed to be used to
  179. // hold recently indexed documents before they must be written to disk.
  180. TotalIndexingBuffer int64 `json:"total_indexing_buffer"` // e.g. 16gb
  181. // TotalIndexingBufferInBytes is the same as TotalIndexingBuffer, but
  182. // expressed in bytes.
  183. TotalIndexingBufferInBytes string `json:"total_indexing_buffer_in_bytes"`
  184. // Roles of the node, e.g. [master, ingest, data]
  185. Roles []string `json:"roles"`
  186. // Attributes of the node.
  187. Attributes map[string]string `json:"attributes"`
  188. // Settings of the node, e.g. paths and pidfile.
  189. Settings map[string]interface{} `json:"settings"`
  190. // OS information, e.g. CPU and memory.
  191. OS *NodesInfoNodeOS `json:"os"`
  192. // Process information, e.g. max file descriptors.
  193. Process *NodesInfoNodeProcess `json:"process"`
  194. // JVM information, e.g. VM version.
  195. JVM *NodesInfoNodeJVM `json:"jvm"`
  196. // ThreadPool information.
  197. ThreadPool *NodesInfoNodeThreadPool `json:"thread_pool"`
  198. // Network information.
  199. Transport *NodesInfoNodeTransport `json:"transport"`
  200. // HTTP information.
  201. HTTP *NodesInfoNodeHTTP `json:"http"`
  202. // Plugins information.
  203. Plugins []*NodesInfoNodePlugin `json:"plugins"`
  204. // Modules information.
  205. Modules []*NodesInfoNodeModule `json:"modules"`
  206. // Ingest information.
  207. Ingest *NodesInfoNodeIngest `json:"ingest"`
  208. }
  209. // HasRole returns true if the node fulfills the given role.
  210. func (n *NodesInfoNode) HasRole(role string) bool {
  211. for _, r := range n.Roles {
  212. if r == role {
  213. return true
  214. }
  215. }
  216. return false
  217. }
  218. // IsMaster returns true if the node is a master node.
  219. func (n *NodesInfoNode) IsMaster() bool {
  220. return n.HasRole("master")
  221. }
  222. // IsData returns true if the node is a data node.
  223. func (n *NodesInfoNode) IsData() bool {
  224. return n.HasRole("data")
  225. }
  226. // IsIngest returns true if the node is an ingest node.
  227. func (n *NodesInfoNode) IsIngest() bool {
  228. return n.HasRole("ingest")
  229. }
  230. // NodesInfoNodeOS represents OS-specific details about a node.
  231. type NodesInfoNodeOS struct {
  232. RefreshInterval string `json:"refresh_interval"` // e.g. 1s
  233. RefreshIntervalInMillis int `json:"refresh_interval_in_millis"` // e.g. 1000
  234. Name string `json:"name"` // e.g. Linux
  235. Arch string `json:"arch"` // e.g. amd64
  236. Version string `json:"version"` // e.g. 4.9.87-linuxkit-aufs
  237. AvailableProcessors int `json:"available_processors"` // e.g. 4
  238. AllocatedProcessors int `json:"allocated_processors"` // e.g. 4
  239. }
  240. // NodesInfoNodeProcess represents process-related information.
  241. type NodesInfoNodeProcess struct {
  242. RefreshInterval string `json:"refresh_interval"` // e.g. 1s
  243. RefreshIntervalInMillis int64 `json:"refresh_interval_in_millis"` // e.g. 1000
  244. ID int `json:"id"` // process id, e.g. 87079
  245. Mlockall bool `json:"mlockall"` // e.g. false
  246. }
  247. // NodesInfoNodeJVM represents JVM-related information.
  248. type NodesInfoNodeJVM struct {
  249. PID int `json:"pid"` // process id, e.g. 87079
  250. Version string `json:"version"` // e.g. "1.8.0_161"
  251. VMName string `json:"vm_name"` // e.g. "OpenJDK 64-Bit Server VM"
  252. VMVersion string `json:"vm_version"` // e.g. "25.161-b14"
  253. VMVendor string `json:"vm_vendor"` // e.g. "Oracle Corporation"
  254. StartTime time.Time `json:"start_time"` // e.g. "2018-03-30T11:06:36.644Z"
  255. StartTimeInMillis int64 `json:"start_time_in_millis"` // e.g. 1522407996644
  256. // Mem information
  257. Mem struct {
  258. HeapInit string `json:"heap_init"` // e.g. "1gb"
  259. HeapInitInBytes int `json:"heap_init_in_bytes"` // e.g. 1073741824
  260. HeapMax string `json:"heap_max"` // e.g. "1007.3mb"
  261. HeapMaxInBytes int `json:"heap_max_in_bytes"` // e.g. 1056309248
  262. NonHeapInit string `json:"non_heap_init"` // e.g. "2.4mb"
  263. NonHeapInitInBytes int `json:"non_heap_init_in_bytes"` // e.g. 2555904
  264. NonHeapMax string `json:"non_heap_max"` // e.g. "0b"
  265. NonHeapMaxInBytes int `json:"non_heap_max_in_bytes"` // e.g. 0
  266. DirectMax string `json:"direct_max"` // e.g. "1007.3mb"
  267. DirectMaxInBytes int `json:"direct_max_in_bytes"` // e.g. 1056309248
  268. } `json:"mem"`
  269. GCCollectors []string `json:"gc_collectors"` // e.g. ["ParNew", "ConcurrentMarkSweep"]
  270. MemoryPools []string `json:"memory_pools"` // e.g. ["Code Cache", "Metaspace", "Compressed Class Space", "Par Eden Space", "Par Survivor Space", "CMS Old Gen"]
  271. // UsingCompressedOrdinaryObjectPointers should be a bool, but is a
  272. // string in 6.2.3. We use an interface{} for now so that it won't break
  273. // when this will be fixed in later versions of Elasticsearch.
  274. UsingCompressedOrdinaryObjectPointers interface{} `json:"using_compressed_ordinary_object_pointers"`
  275. InputArguments []string `json:"input_arguments"` // e.g. ["-Xms1g", "-Xmx1g" ...]
  276. }
  277. // NodesInfoNodeThreadPool represents information about the thread pool.
  278. type NodesInfoNodeThreadPool struct {
  279. ForceMerge *NodesInfoNodeThreadPoolSection `json:"force_merge"`
  280. FetchShardStarted *NodesInfoNodeThreadPoolSection `json:"fetch_shard_started"`
  281. Listener *NodesInfoNodeThreadPoolSection `json:"listener"`
  282. Index *NodesInfoNodeThreadPoolSection `json:"index"`
  283. Refresh *NodesInfoNodeThreadPoolSection `json:"refresh"`
  284. Generic *NodesInfoNodeThreadPoolSection `json:"generic"`
  285. Warmer *NodesInfoNodeThreadPoolSection `json:"warmer"`
  286. Search *NodesInfoNodeThreadPoolSection `json:"search"`
  287. Flush *NodesInfoNodeThreadPoolSection `json:"flush"`
  288. FetchShardStore *NodesInfoNodeThreadPoolSection `json:"fetch_shard_store"`
  289. Management *NodesInfoNodeThreadPoolSection `json:"management"`
  290. Get *NodesInfoNodeThreadPoolSection `json:"get"`
  291. Bulk *NodesInfoNodeThreadPoolSection `json:"bulk"`
  292. Snapshot *NodesInfoNodeThreadPoolSection `json:"snapshot"`
  293. Percolate *NodesInfoNodeThreadPoolSection `json:"percolate"` // check
  294. Bench *NodesInfoNodeThreadPoolSection `json:"bench"` // check
  295. Suggest *NodesInfoNodeThreadPoolSection `json:"suggest"` // deprecated
  296. Optimize *NodesInfoNodeThreadPoolSection `json:"optimize"` // deprecated
  297. Merge *NodesInfoNodeThreadPoolSection `json:"merge"` // deprecated
  298. }
  299. // NodesInfoNodeThreadPoolSection represents information about a certain
  300. // type of thread pool, e.g. for indexing or searching.
  301. type NodesInfoNodeThreadPoolSection struct {
  302. Type string `json:"type"` // e.g. fixed, scaling, or fixed_auto_queue_size
  303. Min int `json:"min"` // e.g. 4
  304. Max int `json:"max"` // e.g. 4
  305. KeepAlive string `json:"keep_alive"` // e.g. "5m"
  306. QueueSize interface{} `json:"queue_size"` // e.g. "1k" or -1
  307. }
  308. // NodesInfoNodeTransport represents transport-related information.
  309. type NodesInfoNodeTransport struct {
  310. BoundAddress []string `json:"bound_address"`
  311. PublishAddress string `json:"publish_address"`
  312. Profiles map[string]*NodesInfoNodeTransportProfile `json:"profiles"`
  313. }
  314. // NodesInfoNodeTransportProfile represents a transport profile.
  315. type NodesInfoNodeTransportProfile struct {
  316. BoundAddress []string `json:"bound_address"`
  317. PublishAddress string `json:"publish_address"`
  318. }
  319. // NodesInfoNodeHTTP represents HTTP-related information.
  320. type NodesInfoNodeHTTP struct {
  321. BoundAddress []string `json:"bound_address"` // e.g. ["127.0.0.1:9200", "[fe80::1]:9200", "[::1]:9200"]
  322. PublishAddress string `json:"publish_address"` // e.g. "127.0.0.1:9300"
  323. MaxContentLength string `json:"max_content_length"` // e.g. "100mb"
  324. MaxContentLengthInBytes int64 `json:"max_content_length_in_bytes"`
  325. }
  326. // NodesInfoNodePlugin represents information about a plugin.
  327. type NodesInfoNodePlugin struct {
  328. Name string `json:"name"` // e.g. "ingest-geoip"
  329. Version string `json:"version"` // e.g. "6.2.3"
  330. ElasticsearchVersion string `json:"elasticsearch_version"`
  331. JavaVersion string `json:"java_version"`
  332. Description string `json:"description"` // e.g. "Ingest processor ..."
  333. Classname string `json:"classname"` // e.g. "org.elasticsearch.ingest.geoip.IngestGeoIpPlugin"
  334. ExtendedPlugins []string `json:"extended_plugins"`
  335. HasNativeController bool `json:"has_native_controller"`
  336. RequiresKeystore bool `json:"requires_keystore"`
  337. }
  338. // NodesInfoNodeModule represents information about a module.
  339. type NodesInfoNodeModule struct {
  340. Name string `json:"name"` // e.g. "ingest-geoip"
  341. Version string `json:"version"` // e.g. "6.2.3"
  342. ElasticsearchVersion string `json:"elasticsearch_version"`
  343. JavaVersion string `json:"java_version"`
  344. Description string `json:"description"` // e.g. "Ingest processor ..."
  345. Classname string `json:"classname"` // e.g. "org.elasticsearch.ingest.geoip.IngestGeoIpPlugin"
  346. ExtendedPlugins []string `json:"extended_plugins"`
  347. HasNativeController bool `json:"has_native_controller"`
  348. RequiresKeystore bool `json:"requires_keystore"`
  349. }
  350. // NodesInfoNodeIngest represents information about the ingester.
  351. type NodesInfoNodeIngest struct {
  352. Processors []*NodesInfoNodeIngestProcessorInfo `json:"processors"`
  353. }
  354. // NodesInfoNodeIngestProcessorInfo represents ingest processor info.
  355. type NodesInfoNodeIngestProcessorInfo struct {
  356. Type string `json:"type"` // e.g. append, convert, date etc.
  357. }