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