Skip to content
This repository was archived by the owner on May 31, 2021. It is now read-only.

Commit 496f03e

Browse files
committed
1 parent 46db411 commit 496f03e

File tree

5 files changed

+445
-1
lines changed

5 files changed

+445
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ on the command line.
231231

232232
- [x] Health
233233
- [x] State
234-
- [ ] Stats
234+
- [x] Stats
235235
- [ ] Pending cluster tasks
236236
- [ ] Cluster reroute
237237
- [ ] Cluster update settings

client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,11 @@ func (c *Client) ClusterState() *ClusterStateService {
12131213
return NewClusterStateService(c)
12141214
}
12151215

1216+
// ClusterStats retrieves cluster statistics.
1217+
func (c *Client) ClusterStats() *ClusterStatsService {
1218+
return NewClusterStatsService(c)
1219+
}
1220+
12161221
// NodesInfo retrieves one or more or all of the cluster nodes information.
12171222
func (c *Client) NodesInfo() *NodesInfoService {
12181223
return NewNodesInfoService(c)

cluster_state.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ type ClusterStateMetadata struct {
174174
}
175175

176176
type ClusterStateNode struct {
177+
Name string `json:"name"`
178+
TransportAddress string `json:"transport_address"`
179+
Attributes map[string]interface{} `json:"attributes"`
180+
181+
// TODO(oe) are these still valid?
177182
State string `json:"state"`
178183
Primary bool `json:"primary"`
179184
Node string `json:"node"`

cluster_stats.go

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

0 commit comments

Comments
 (0)