cluster_stats.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. "gopkg.in/olivere/elastic.v5/uritemplates"
  11. )
  12. // ClusterStatsService is documented at
  13. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/cluster-stats.html.
  14. type ClusterStatsService struct {
  15. client *Client
  16. pretty bool
  17. nodeId []string
  18. flatSettings *bool
  19. human *bool
  20. }
  21. // NewClusterStatsService creates a new ClusterStatsService.
  22. func NewClusterStatsService(client *Client) *ClusterStatsService {
  23. return &ClusterStatsService{
  24. client: client,
  25. nodeId: make([]string, 0),
  26. }
  27. }
  28. // NodeId is documented as: A comma-separated list of node IDs or names to limit the returned information; use `_local` to return information from the node you're connecting to, leave empty to get information from all nodes.
  29. func (s *ClusterStatsService) NodeId(nodeId []string) *ClusterStatsService {
  30. s.nodeId = nodeId
  31. return s
  32. }
  33. // FlatSettings is documented as: Return settings in flat format (default: false).
  34. func (s *ClusterStatsService) FlatSettings(flatSettings bool) *ClusterStatsService {
  35. s.flatSettings = &flatSettings
  36. return s
  37. }
  38. // Human is documented as: Whether to return time and byte values in human-readable format..
  39. func (s *ClusterStatsService) Human(human bool) *ClusterStatsService {
  40. s.human = &human
  41. return s
  42. }
  43. // Pretty indicates that the JSON response be indented and human readable.
  44. func (s *ClusterStatsService) Pretty(pretty bool) *ClusterStatsService {
  45. s.pretty = pretty
  46. return s
  47. }
  48. // buildURL builds the URL for the operation.
  49. func (s *ClusterStatsService) buildURL() (string, url.Values, error) {
  50. // Build URL
  51. var err error
  52. var path string
  53. if len(s.nodeId) > 0 {
  54. path, err = uritemplates.Expand("/_cluster/stats/nodes/{node_id}", map[string]string{
  55. "node_id": strings.Join(s.nodeId, ","),
  56. })
  57. if err != nil {
  58. return "", url.Values{}, err
  59. }
  60. } else {
  61. path, err = uritemplates.Expand("/_cluster/stats", map[string]string{})
  62. if err != nil {
  63. return "", url.Values{}, err
  64. }
  65. }
  66. // Add query string parameters
  67. params := url.Values{}
  68. if s.pretty {
  69. params.Set("pretty", "1")
  70. }
  71. if s.flatSettings != nil {
  72. params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
  73. }
  74. if s.human != nil {
  75. params.Set("human", fmt.Sprintf("%v", *s.human))
  76. }
  77. return path, params, nil
  78. }
  79. // Validate checks if the operation is valid.
  80. func (s *ClusterStatsService) Validate() error {
  81. return nil
  82. }
  83. // Do executes the operation.
  84. func (s *ClusterStatsService) Do(ctx context.Context) (*ClusterStatsResponse, error) {
  85. // Check pre-conditions
  86. if err := s.Validate(); err != nil {
  87. return nil, err
  88. }
  89. // Get URL for request
  90. path, params, err := s.buildURL()
  91. if err != nil {
  92. return nil, err
  93. }
  94. // Get HTTP response
  95. res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
  96. if err != nil {
  97. return nil, err
  98. }
  99. // Return operation response
  100. ret := new(ClusterStatsResponse)
  101. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  102. return nil, err
  103. }
  104. return ret, nil
  105. }
  106. // ClusterStatsResponse is the response of ClusterStatsService.Do.
  107. type ClusterStatsResponse struct {
  108. Timestamp int64 `json:"timestamp"`
  109. ClusterName string `json:"cluster_name"`
  110. ClusterUUID string `json:"uuid"`
  111. Status string `json:"status"`
  112. Indices *ClusterStatsIndices `json:"indices"`
  113. Nodes *ClusterStatsNodes `json:"nodes"`
  114. }
  115. type ClusterStatsIndices struct {
  116. Count int `json:"count"`
  117. Shards *ClusterStatsIndicesShards `json:"shards"`
  118. Docs *ClusterStatsIndicesDocs `json:"docs"`
  119. Store *ClusterStatsIndicesStore `json:"store"`
  120. FieldData *ClusterStatsIndicesFieldData `json:"fielddata"`
  121. FilterCache *ClusterStatsIndicesFilterCache `json:"filter_cache"`
  122. IdCache *ClusterStatsIndicesIdCache `json:"id_cache"`
  123. Completion *ClusterStatsIndicesCompletion `json:"completion"`
  124. Segments *ClusterStatsIndicesSegments `json:"segments"`
  125. Percolate *ClusterStatsIndicesPercolate `json:"percolate"`
  126. }
  127. type ClusterStatsIndicesShards struct {
  128. Total int `json:"total"`
  129. Primaries int `json:"primaries"`
  130. Replication float64 `json:"replication"`
  131. Index *ClusterStatsIndicesShardsIndex `json:"index"`
  132. }
  133. type ClusterStatsIndicesShardsIndex struct {
  134. Shards *ClusterStatsIndicesShardsIndexIntMinMax `json:"shards"`
  135. Primaries *ClusterStatsIndicesShardsIndexIntMinMax `json:"primaries"`
  136. Replication *ClusterStatsIndicesShardsIndexFloat64MinMax `json:"replication"`
  137. }
  138. type ClusterStatsIndicesShardsIndexIntMinMax struct {
  139. Min int `json:"min"`
  140. Max int `json:"max"`
  141. Avg float64 `json:"avg"`
  142. }
  143. type ClusterStatsIndicesShardsIndexFloat64MinMax struct {
  144. Min float64 `json:"min"`
  145. Max float64 `json:"max"`
  146. Avg float64 `json:"avg"`
  147. }
  148. type ClusterStatsIndicesDocs struct {
  149. Count int `json:"count"`
  150. Deleted int `json:"deleted"`
  151. }
  152. type ClusterStatsIndicesStore struct {
  153. Size string `json:"size"` // e.g. "5.3gb"
  154. SizeInBytes int64 `json:"size_in_bytes"`
  155. ThrottleTime string `json:"throttle_time"` // e.g. "0s"
  156. ThrottleTimeInMillis int64 `json:"throttle_time_in_millis"`
  157. }
  158. type ClusterStatsIndicesFieldData struct {
  159. MemorySize string `json:"memory_size"` // e.g. "61.3kb"
  160. MemorySizeInBytes int64 `json:"memory_size_in_bytes"`
  161. Evictions int64 `json:"evictions"`
  162. Fields map[string]struct {
  163. MemorySize string `json:"memory_size"` // e.g. "61.3kb"
  164. MemorySizeInBytes int64 `json:"memory_size_in_bytes"`
  165. } `json:"fields"`
  166. }
  167. type ClusterStatsIndicesFilterCache struct {
  168. MemorySize string `json:"memory_size"` // e.g. "61.3kb"
  169. MemorySizeInBytes int64 `json:"memory_size_in_bytes"`
  170. Evictions int64 `json:"evictions"`
  171. }
  172. type ClusterStatsIndicesIdCache struct {
  173. MemorySize string `json:"memory_size"` // e.g. "61.3kb"
  174. MemorySizeInBytes int64 `json:"memory_size_in_bytes"`
  175. }
  176. type ClusterStatsIndicesCompletion struct {
  177. Size string `json:"size"` // e.g. "61.3kb"
  178. SizeInBytes int64 `json:"size_in_bytes"`
  179. Fields map[string]struct {
  180. Size string `json:"size"` // e.g. "61.3kb"
  181. SizeInBytes int64 `json:"size_in_bytes"`
  182. } `json:"fields"`
  183. }
  184. type ClusterStatsIndicesSegments struct {
  185. Count int64 `json:"count"`
  186. Memory string `json:"memory"` // e.g. "61.3kb"
  187. MemoryInBytes int64 `json:"memory_in_bytes"`
  188. IndexWriterMemory string `json:"index_writer_memory"` // e.g. "61.3kb"
  189. IndexWriterMemoryInBytes int64 `json:"index_writer_memory_in_bytes"`
  190. IndexWriterMaxMemory string `json:"index_writer_max_memory"` // e.g. "61.3kb"
  191. IndexWriterMaxMemoryInBytes int64 `json:"index_writer_max_memory_in_bytes"`
  192. VersionMapMemory string `json:"version_map_memory"` // e.g. "61.3kb"
  193. VersionMapMemoryInBytes int64 `json:"version_map_memory_in_bytes"`
  194. FixedBitSet string `json:"fixed_bit_set"` // e.g. "61.3kb"
  195. FixedBitSetInBytes int64 `json:"fixed_bit_set_memory_in_bytes"`
  196. }
  197. type ClusterStatsIndicesPercolate struct {
  198. Total int64 `json:"total"`
  199. // TODO(oe) The JSON tag here is wrong as of ES 1.5.2 it seems
  200. Time string `json:"get_time"` // e.g. "1s"
  201. TimeInBytes int64 `json:"time_in_millis"`
  202. Current int64 `json:"current"`
  203. MemorySize string `json:"memory_size"` // e.g. "61.3kb"
  204. MemorySizeInBytes int64 `json:"memory_sitze_in_bytes"`
  205. Queries int64 `json:"queries"`
  206. }
  207. // ---
  208. type ClusterStatsNodes struct {
  209. Count *ClusterStatsNodesCount `json:"count"`
  210. Versions []string `json:"versions"`
  211. OS *ClusterStatsNodesOsStats `json:"os"`
  212. Process *ClusterStatsNodesProcessStats `json:"process"`
  213. JVM *ClusterStatsNodesJvmStats `json:"jvm"`
  214. FS *ClusterStatsNodesFsStats `json:"fs"`
  215. Plugins []*ClusterStatsNodesPlugin `json:"plugins"`
  216. }
  217. type ClusterStatsNodesCount struct {
  218. Total int `json:"total"`
  219. Data int `json:"data"`
  220. CoordinatingOnly int `json:"coordinating_only"`
  221. Master int `json:"master"`
  222. Ingest int `json:"ingest"`
  223. }
  224. type ClusterStatsNodesOsStats struct {
  225. AvailableProcessors int `json:"available_processors"`
  226. Mem *ClusterStatsNodesOsStatsMem `json:"mem"`
  227. CPU []*ClusterStatsNodesOsStatsCPU `json:"cpu"`
  228. }
  229. type ClusterStatsNodesOsStatsMem struct {
  230. Total string `json:"total"` // e.g. "16gb"
  231. TotalInBytes int64 `json:"total_in_bytes"`
  232. }
  233. type ClusterStatsNodesOsStatsCPU struct {
  234. Vendor string `json:"vendor"`
  235. Model string `json:"model"`
  236. MHz int `json:"mhz"`
  237. TotalCores int `json:"total_cores"`
  238. TotalSockets int `json:"total_sockets"`
  239. CoresPerSocket int `json:"cores_per_socket"`
  240. CacheSize string `json:"cache_size"` // e.g. "256b"
  241. CacheSizeInBytes int64 `json:"cache_size_in_bytes"`
  242. Count int `json:"count"`
  243. }
  244. type ClusterStatsNodesProcessStats struct {
  245. CPU *ClusterStatsNodesProcessStatsCPU `json:"cpu"`
  246. OpenFileDescriptors *ClusterStatsNodesProcessStatsOpenFileDescriptors `json:"open_file_descriptors"`
  247. }
  248. type ClusterStatsNodesProcessStatsCPU struct {
  249. Percent float64 `json:"percent"`
  250. }
  251. type ClusterStatsNodesProcessStatsOpenFileDescriptors struct {
  252. Min int64 `json:"min"`
  253. Max int64 `json:"max"`
  254. Avg int64 `json:"avg"`
  255. }
  256. type ClusterStatsNodesJvmStats struct {
  257. MaxUptime string `json:"max_uptime"` // e.g. "5h"
  258. MaxUptimeInMillis int64 `json:"max_uptime_in_millis"`
  259. Versions []*ClusterStatsNodesJvmStatsVersion `json:"versions"`
  260. Mem *ClusterStatsNodesJvmStatsMem `json:"mem"`
  261. Threads int64 `json:"threads"`
  262. }
  263. type ClusterStatsNodesJvmStatsVersion struct {
  264. Version string `json:"version"` // e.g. "1.8.0_45"
  265. VMName string `json:"vm_name"` // e.g. "Java HotSpot(TM) 64-Bit Server VM"
  266. VMVersion string `json:"vm_version"` // e.g. "25.45-b02"
  267. VMVendor string `json:"vm_vendor"` // e.g. "Oracle Corporation"
  268. Count int `json:"count"`
  269. }
  270. type ClusterStatsNodesJvmStatsMem struct {
  271. HeapUsed string `json:"heap_used"`
  272. HeapUsedInBytes int64 `json:"heap_used_in_bytes"`
  273. HeapMax string `json:"heap_max"`
  274. HeapMaxInBytes int64 `json:"heap_max_in_bytes"`
  275. }
  276. type ClusterStatsNodesFsStats struct {
  277. Path string `json:"path"`
  278. Mount string `json:"mount"`
  279. Dev string `json:"dev"`
  280. Total string `json:"total"` // e.g. "930.7gb"`
  281. TotalInBytes int64 `json:"total_in_bytes"`
  282. Free string `json:"free"` // e.g. "930.7gb"`
  283. FreeInBytes int64 `json:"free_in_bytes"`
  284. Available string `json:"available"` // e.g. "930.7gb"`
  285. AvailableInBytes int64 `json:"available_in_bytes"`
  286. DiskReads int64 `json:"disk_reads"`
  287. DiskWrites int64 `json:"disk_writes"`
  288. DiskIOOp int64 `json:"disk_io_op"`
  289. DiskReadSize string `json:"disk_read_size"` // e.g. "0b"`
  290. DiskReadSizeInBytes int64 `json:"disk_read_size_in_bytes"`
  291. DiskWriteSize string `json:"disk_write_size"` // e.g. "0b"`
  292. DiskWriteSizeInBytes int64 `json:"disk_write_size_in_bytes"`
  293. DiskIOSize string `json:"disk_io_size"` // e.g. "0b"`
  294. DiskIOSizeInBytes int64 `json:"disk_io_size_in_bytes"`
  295. DiskQueue string `json:"disk_queue"`
  296. DiskServiceTime string `json:"disk_service_time"`
  297. }
  298. type ClusterStatsNodesPlugin struct {
  299. Name string `json:"name"`
  300. Version string `json:"version"`
  301. Description string `json:"description"`
  302. URL string `json:"url"`
  303. JVM bool `json:"jvm"`
  304. Site bool `json:"site"`
  305. }